Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Cache-Control HTTP Header for S3 Objects from PHP AWS SDK

I using the Amazon SDK for PHP and trying to set Cache-control Header on the image. When I try to add it via MetaData = array("Cache-Control") it changes it to be x-amz-meta-cache-control when I login to the S3 bucket, and when I download the file, there is no Cache-control set. But if I manually change this setting, the Cache-control works perfectly. Is there some parameter I missing that I can use to set HTTP Request Headers programmatically on upload? I'm using the PutObject method. I believe the AWS SDK is from 2013.

like image 348
user814584 Avatar asked Oct 16 '13 22:10

user814584


People also ask

How do I add a cache-control header?

If you want to enable Cache-Control for all files, add Header set line outside the filesMatch block. As you can see, we set the Cache-Control header's max-age to 3600 seconds and to public for the listed files.

What should you add to a cache-control response header to specify that a response?

private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.


2 Answers

If you want to add the CacheControl header to an item already in your bucket, use the SDK's copyObject method. Set the MetadataDirective param to REPLACE to make the item overwrite itself.

I noticed one weird thing: I had to set the ContentType header too, even though it was already set. Otherwise the image would not display inline in the browser but be offered as a download.

$result = $s3->copyObject(array(
    'ACL' => 'public-read',
    'Bucket' => $bucket, // target bucket
    'CacheControl' => 'public, max-age=86400',
    'ContentType' => 'image/jpeg', // !!
    'CopySource' => urlencode($bucket . '/' . $key),
    'Key' => $key, // target file name
    'MetadataDirective' => 'REPLACE'
));
like image 67
RonaldPK Avatar answered Sep 20 '22 19:09

RonaldPK


The cache control isn't set via the "MetaData" index, "CacheControl" is at the same level as "MetaData", not contained within it.

http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_putObject

You'd use something like this as your configuration array for the putObject() method...

$s3client->putObject(array(
  'Bucket' => '...',
  'key' => '...',
  'body' => '...',
  'CacheControl' => 'max-age=172800',
  'MetaData' => array(
    'metaKey' => 'metaValue',
    'metaKey' => 'metaValue'
)));

For the upload() method...

$s3client->upload(
  'bucket',
  'key',
  fopen('sourcefile','r'),
  'public-read',
  array('params' => array(
    'CacheControl' => 'max-age=172800',
    'Metadata' => array(
      'metaKey' => 'metaValue',
      'metaKey' => 'metaValue'
))));

Also, it's worth pointing out that upload() will wrap putObject() for files of 5MB in size, otherwise it will initiate a multipart upload request.

like image 41
Scuzzy Avatar answered Sep 19 '22 19:09

Scuzzy