Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AmazonS3, how to check if the upload succeeded?

Tags:

java

amazon-s3

I wrote a short test code in Java to upload a PDF file generated in memory. In this test code I just use a dummy byte array, but in the real use I will put a generated PDF (max 2-3 pages) in that byte array. Everything works: the file gets uploaded and the permissions set.

However since I've a PutObjectResult returned, I was wondering how I'm supposed to check it. Or is it enough to look for the exceptions AmazonClientException and AmazonServiceException?

In other words: How to check that the upload succeded and didn't corrupt my data?

    String bucket = "mybucket.example.com";
    String fileName = "2011/test/test.pdf";
    AmazonS3 client = new AmazonS3Client(new BasicAWSCredentials(
        "accessKey", "secretKey"));
    byte[] contents = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    InputStream stream = new ByteArrayInputStream(contents);
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentLength(contents.length);
    meta.setContentType("application/pdf");
    PutObjectResult res = client.putObject(bucket, fileName, stream, meta);
    client.setObjectAcl(bucket, fileName, CannedAccessControlList.PublicRead);
like image 971
stivlo Avatar asked May 07 '11 17:05

stivlo


People also ask

How can I check the integrity of an object uploaded to Amazon S3?

Verify the integrity of the uploaded object When you use PutObject to upload objects to Amazon S3, pass the Content-MD5 value as a request header. Amazon S3 checks the object against the provided Content-MD5 value. If the values do not match, you receive an error.

Which HTTP code indicates a successful upload of an object to Amazon S3?

HTTP 200 code indicates a successful write to S3.


1 Answers

I've looked the source code of AWS and debugged it and discovered the following:

  • If MD5 is not provided it's calculated (works either for real files and InputStream)
  • When the upload is finished md5 client side and server side are compared and if they differ an AmazonClientException is thrown. [line 1188 of AmazonS3Client 1.19]

In other words, to answer my own question, it's enough to listen for the exceptions because also the MD5 of the data uploaded is checked, so if there was a corruption an exception would be raised.

AmazonClientException and AmazonServiceException are unchecked exceptions, so it's important to remember to listen for them since the compiler won't force you to.

like image 83
stivlo Avatar answered Sep 24 '22 19:09

stivlo