开发者

AmazonS3, how to check if the upload succeeded?

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开发者_C百科 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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜