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.
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.
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.
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'
));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With