Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update metadata using Amazon S3 SDK

I am using the PHP version of Amazon's AWS SDK. I have a bunch of files with an Expires header; I want to delete that header and add a Cache-control header instead. The update_object function lets me add headers but not remove them.

The answers on this question suggest you can update a file's metadata when you copy it, but I have tried it and it doesn't work. Here is what I used:

$response = $s3->copy_object(
    array(
        'bucket' => $bucket,
        'filename' => $file,
    ),
    array(
        'bucket' => $bucket,
        'filename' => $file2,
    ),
    array(
        'acl' => AmazonS3::ACL_PUBLIC,
        'headers' => array(
            'Content-Type' => 'image/jpeg',
            'Cache-Control' => 'public,max-age=30240000',
        ),
        'meta' => array(
            'x-fake-header' => 'something awesome is happening',
        ),
    )
);

However, the copied object has the exact same headers as the original object (Expires and Content-Type only). I've tried all manner of combinations of the above (with and without Content-Type, Cache-control, meta, etc) and get the same result.

How do I reset the metadata?

like image 664
DisgruntledGoat Avatar asked Aug 07 '12 13:08

DisgruntledGoat


People also ask

How do I update AWS metadata?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . Navigate to your Amazon S3 bucket or folder, and select the check box to the left of the names of the objects with metadata you want to edit. On the Actions menu, choose Edit actions, and choose Edit metadata.

Can we update data in S3?

Amazon S3 is an object-storage service. Unlike a file on your local disk, which you could open in an editor and change one byte, any updates to an object in Amazon S3 require the whole object to be replaced.

What is held as metadata in an S3 object?

For each object stored in a bucket, Amazon S3 maintains a set of system metadata. Amazon S3 processes this system metadata as needed. For example, Amazon S3 maintains object creation date and size metadata and uses this information as part of object management.

Are S3 tags metadata?

If you want to add your own custom metadata to an object in S3, you can add metadata instead of tags. Tags are not the same thing as object metadata in S3. Metadata applies only to that object in S3 and cannot be searched on, as you can search with tags.


2 Answers

I've just found that copying to object to itself actually does change the headers properly. I was copying it to a second file for testing purposes to avoid overwriting the original.

But for some strange reason copying to a different file doesn't change the headers, but copying to the same file does.

like image 97
DisgruntledGoat Avatar answered Sep 22 '22 05:09

DisgruntledGoat


Based on Jeffin P S's answer (which works great), I'm proposing a version that creates the new metadata object by cloning the original one. This way, the AWS-specific (non-user) metadata do not get converted into user metadata. Not sure if that would be a real issue, but seems more legit this way.

ObjectMetadata metadataCopy = amazonS3Client.getObjectMetadata(bucketName, fileKey).clone();
metadataCopy.addUserMetadata("yourKey", "updateValue");
metadataCopy.addUserMetadata("otherKey", "newValue");

CopyObjectRequest request = new CopyObjectRequest(bucketName, fileKey, bucketName, fileKey)
      .withSourceBucketName(bucketName)
      .withSourceKey(fileKey)
      .withNewObjectMetadata(metadataCopy);

amazonS3Client.copyObject(request);
like image 43
Alex Rosiu Avatar answered Sep 20 '22 05:09

Alex Rosiu