AWS S3 – Setting Metadata with the PHP SDK2


I am attempting to change the metadata of all of the objects in a particular bucket on S3 using the AWS PHP SDK2. I’ve had trouble finding a specific example using the new SDK, but have pieced together the following:

The foreach loop successfully loops through the first 10 items in the given $bucket, but I get a 403 error on the copyObject() operation:

I am not sure if this is due to incorrect values being passed in to copyObject, or some setting in S3. Note that I have yet to create a rights-restricted account in IAM and am using the base account that should have all rights on the objects.

Any help appreciated.


Ok, figured this out – my syntax was incorrect in two ways.

First, I was using the incorrect value for CopySource. From the documentation:

CopySource – (string) – The name of the source bucket and key name of the source object, separated by a slash (/). Must be URL-encoded.

So in my case, instead of using just 'CopySource' => $key,, it should be 'CopySource' => urlencode($bucket . '/' . $key),. This explains the 403 errors, as I was essentially telling the API that my source file was in a {bucket} / {key} of just {key}.

The second issue relates to the specific headers – specifying the Expires and Cache-Control headers in the Metadata field results in the creation of Amazon-specific meta values, with keys prefixed with x-amz-meta-. Instead I am now using the Expires and CacheControl arguments. My final working code:

Leave a Reply