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);
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.
HTTP 200 code indicates a successful write to S3.
I've looked the source code of AWS and debugged it and discovered the following:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With