Skip to content

Instantly share code, notes, and snippets.

@Remiii
Last active September 18, 2015 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Remiii/316d52b0fc4558a02cc6 to your computer and use it in GitHub Desktop.
Save Remiii/316d52b0fc4558a02cc6 to your computer and use it in GitHub Desktop.
Update headers assets on S3 (CloudFront)

Update headers assets on S3 (CloudFront)

Update your Header of your Assets on AWS S3 (+CloudFront)

<?php
// src/remiii/UtilsBundle/Command/Assets/AssetsUpdateHeadersCommand.php
namespace remiii\UtilsBundle\Command\Assets ;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
use Aws\Common\Aws ;
use Aws\S3\S3Client ;
class AssetsUpdateHeadersCommand extends ContainerAwareCommand
{
protected $bucketName ;
protected $filename ;
protected function configure ( )
{
$this
-> setname ( 'remiii:utils:assets:update-headers' )
-> setDescription ( 'Utils: Update assets headers on S3 storage' ) ;
}
/**
* @see Command
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
// AWS Doc: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html
// Confirmation part
$dialog = $this -> getHelperSet ( ) -> get ( 'dialog' ) ;
if ( ! $dialog -> askConfirmation (
$output ,
'<error>This command do fat stuff on your assets S3 storage! Are you sure to continue?</error>' ,
false
) )
{
return ;
}
$output -> writeln ( '<info>Update assets headers on S3 storage</info>' ) ;
$this -> bucketName = $this -> getContainer ( ) -> getParameter ( 'aws_assets_bucket_name' ) ;
$s3Service = $this -> getContainer ( ) -> get ( 'remiii.amazon.s3' ) ;
$s3Client = $s3Service -> getRawClient ( ) ;
$iterator = $s3Client -> getIterator ( 'ListObjects' , array (
'Bucket' => $this -> bucketName
) ) ;
foreach ( $iterator as $object )
{
$output -> writeln ( '<info>Update ' . $object [ 'Key' ] . '</info>' ) ;
$objectInfos = $s3Client -> getObject ( array (
'Bucket' => $this -> bucketName ,
'Key' => $object [ 'Key' ]
) ) ;
$s3Client -> copyObject ( array (
'Bucket' => $this -> bucketName ,
'Key' => $object [ 'Key' ] ,
'CopySource' => $this -> bucketName . '/' . $object [ 'Key' ] ,
'ACL' => 'public-read' ,
'ContentType' => $objectInfos [ 'ContentType' ] ,
'CacheControl' => 'public, max-age=604800',
'MetadataDirective' => 'REPLACE'
) ) ;
}
}
}
<?php
// src/remiii/GlobalBundle/Amazon/StreamWrapperS3.php
namespace remiii\GlobalBundle\Amazon ;
use Aws\Common\Aws ;
// Source
// http://stackoverflow.com/questions/8163717/dump-symfony2-assets-to-amazon-s3
class StreamWrapperS3
{
protected $s3 ;
public function __construct ( $key, $secret, $region )
{
$aws = array (
'key' => $key ,
'secret' => $secret ,
'region' => $region
) ;
$this -> s3 = Aws::factory ( $aws ) -> get ( 's3' ) ;
}
public function registerStreamWrapper ( )
{
$this -> s3 -> registerStreamWrapper ( ) ;
}
public function getRawClient ( )
{
return $this -> s3 ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment