Created
March 6, 2015 07:56
-
-
Save snipe/aee3b4f5793cfe0c3d39 to your computer and use it in GitHub Desktop.
Laravel Artisan command to upload fils to S3 with cache-control headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Console\Command; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputArgument; | |
class MoveToS3 extends Command { | |
/** | |
* The console command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'moveto:s3'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'This command traverses the file system and copies files to S3'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function fire() | |
{ | |
// | |
/* | |
Get optional options | |
*/ | |
$delete = $this->option('delete'); | |
$type = $this->option('type'); | |
$aws_path = '/assets'; | |
if (App::environment('local')) { | |
$webroot = '/Users/agianotto/Sites/massmosaic/massmosaic/public'; | |
} else { | |
$webroot = '/var/www/html/public'; | |
} | |
$root = $webroot.$aws_path; | |
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($objects as $name => $object) { | |
$path_parts = pathinfo($name); | |
$filename = $path_parts['filename']; | |
if (($filename!='.') && ($filename!='..') && ($filename!='') && (!is_dir($name))) { | |
$new_aws = (str_replace($webroot,'',$name)); | |
if (isset($path_parts['extension'])) { | |
$s3_filename = $filename.'.'.$path_parts['extension']; | |
} else { | |
$s3_filename = $filename; | |
} | |
if ((!isset($type)) || ($type==$path_parts['extension'])) { | |
$this->info($name); | |
$this->info('- moved to '.Config::get('app.aws_bucket').$new_aws); | |
$s3 = AWS::get('s3'); | |
$s3->putObject(array( | |
'Bucket' => Config::get('app.aws_bucket'), | |
'Key' => $new_aws, | |
'SourceFile' => $name, | |
'CacheControl' => 'max-age=172800', | |
"Expires" => gmdate("D, d M Y H:i:s T", | |
strtotime("+3 years")) | |
)); | |
} | |
if ($delete=='true') { | |
if (isset($path_parts['extension'])) { | |
@unlink($name); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Get the console command options. | |
* | |
* @return array | |
*/ | |
protected function getOptions() | |
{ | |
return array( | |
array('type', null, InputOption::VALUE_OPTIONAL, 'Extension type of files to move (default is all files)', null), | |
array('delete', null, InputOption::VALUE_OPTIONAL, 'Whether to delete local versions (default is false)', null), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment