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

amazon-s3amazon-web-servicesphp

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.

Best Solution

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.

Related Question