Java – Amazon S3, how to check whether the upload is successful?

I wrote a short test code in Java to upload the PDF file generated in memory In this test code, I only use a virtual byte array, but in practice, I will place a generated PDF (up to 2-3 pages) in the byte array Everything is normal: file upload and set permissions

However, since I have returned putobjectresult, I wonder how I should check it Or is it sufficient to find amazonclientexception and amazonserviceexception exceptions?

In other words: how to check whether the upload is successful and does not damage 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,CannedAccessControlList.PublicRead);

Solution

I checked and debugged the source code of AWS and found the following:

>If MD5 is not provided, calculate it (applicable to real files and InputStream) > after uploading, the MD5 client and server will be compared. If they are different, Amazon clientexception will be thrown [Amazon s3client 1.19 line 1188]

In other words, in order to answer my own questions, it is sufficient to listen for exceptions, because MD5 of uploaded data is also checked, so if there is corruption, an exception will be thrown

Amazon clientexception and Amazon serviceexception are unchecked exceptions, so it's important to remember to listen to them because the compiler won't force you to do so

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>