7

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
James McMahon
  • 48,506
  • 64
  • 207
  • 283

3 Answers3

6

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.

James McMahon
  • 48,506
  • 64
  • 207
  • 283
  • This is pretty similar to what I recently did with the PHP API. I'd capture the originals etag, copy, compare original etag to copied etag, if match, delete original. – Dan Jul 16 '13 at 21:36
  • Historical note, following up on Dan's comment: It appears that the system for generating etags changes when a multipart upload is used as opposed to a regular upload, so comparison by MD5 might be the better approach. – Shotgun Ninja Mar 25 '14 at 15:37
  • 2
    Since S3 doesn't have immediate consistency, wouldn't this method occasionally show that the object hasn't been copied even though it has? – Aleksandr Dubinsky Jul 20 '14 at 16:20
1

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.

jdero
  • 1,797
  • 3
  • 21
  • 36
0

AWS documentation says

The source and destination ETag is identical for a successfully copied

enter image description here

Anuruddha
  • 1,367
  • 6
  • 19
  • 38