Keeping metadata when overwriting file in S3

Viewed 1176

When I overwrite a file to S3, I am losing the metadata since it is treated as a new object. I am trying to keep the existing original metadata of a file when overwriting the file in S3. I know from the docs that you cannot modify the metadata of an object in a S3 bucket, but how do I "copy" the existing metadata onto the new overwritten object?

For example, I have this method below:

public void uploadToS3(File file, String key, String bucketName) {
    // original metadata
    ObjectMetadata objMetadata = client.getObjectMetadata(bucketName, key);
    // How do I set the new overwritten object to have this metadata above??

    try {
        client.putObject(new PutObjectRequest(bucketName, key, file));
    } catch (AmazonServiceException e) {
        e.printstacktrace();
    } catch(AmazonClientException e) {
        e.printstacktrace();
    } 

How do I "point" the new object to have the same metadata as the object that was just overwritten?

1 Answers
Related