Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for successful S3 copy operation?

Tags:

java

amazon-s3

I'm trying to implement a move operation using the Amazon S3 Java API.

The problem I am having is that the CopyObjectResult object returned by the AmazonS3Client.copyObject method doesn't seem to have a clear indicator about wiether the operation was successful or not.

Given that after this operation I am going to be deleting a file, I'd want to make sure that the operation was successful.

Any suggestions?

like image 654
James McMahon Avatar asked Jul 10 '13 21:07

James McMahon


Video Answer


2 Answers

This is what I ended up doing,

def s3 = createS3Client(credentials)
def originalMeta = s3.getObjectMetadata(originalBucketName, key)
s3.copyObject(originalBucketName, key, newBucketName, newKey)
def newMeta = s3.getObjectMetadata(newBucketName, newKey)

// check that md5 matches to confirm this operation was successful
return originalMeta.contentMD5 == newMeta.contentMD5

Note that this is Groovy code, but it is extremely similar to how the Java code would work.

I don't like having to make two additional operations to check the metadata, so if there is anyway to do this more efficiently let me know.

like image 122
James McMahon Avatar answered Oct 16 '22 20:10

James McMahon


I'm pretty sure you can just use CopyObjectResult object's getETag method to confirm that, after created, it has a valid ETag, as was made in the CopyObjectRequest setETag method.

getETag

public String getETag() Gets the ETag value for the new object that was created in the associated CopyObjectRequest. Returns: The ETag value for the new object. See Also: setETag(String) setETag

public void setETag(String etag) Sets the ETag value for the new object that was created from the associated copy object request. Parameters: etag - The ETag value for the new object. See Also: getETag()

Always trust the data.

Been a year since I did a similar function with the Amazon PhP SDK a couple years ago, but it should work.

like image 42
jdero Avatar answered Oct 16 '22 20:10

jdero