简体   繁体   中英

How to generate PUT presigned URL for an object when bucket has object lock enabled using AWS SDK 2 for java

I have to generate presigned url to upload object on the bucket using AWS SDK 2. Up-until I am doing this with bucket with object lock disabled it is working fine but when I am doing it with bucket with object lock enabled it is throwing error that I should send MD5 encryption. My question here is at the time of creation when I don't know about the file then how am I supposed to generate its md5 encryption and create the presigned url with it. Anybody please help me in understanding this and how this can be implemented.

I used the simple code that is given on official website to generate the presigned url nothing new.

For SDK Version 2 , there is an open feature request: https://github.com/aws/aws-sdk-java-v2/issues/2155

For SDK Version 1 , you could use Md5Utils.md5AsBase64(file) to determine the MD5 of the file. See Closed Bug: https://github.com/aws/aws-sdk-java/issues/2634

MD5Utils also exists in Version 2

https://github.com/aws/aws-sdk-java-v2/blob/master/utils/src/main/java/software/amazon/awssdk/utils/Md5Utils.java


Note

If you configure a default retention period on a bucket, requests to upload objects in such a bucket must include the Content-MD5 header. For more information, see Put Object in the Amazon Simple Storage Service API Reference.

It seems that the Content-MD5 header restriction is applied only if you have a ( Retention Period set ).

So if you do have a Retention Period, then yes you must provide Content-MD5 Header.

https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lock-overview.html


Note

To successfully complete the PutObject request, you must have the s3:PutObject in your IAM permissions.

To successfully change the objects acl of your PutObject request, you must have the s3:PutObjectAcl in your IAM permissions.

To successfully set the tag-set with your PutObject request, you must have the s3:PutObjectTagging in your IAM permissions.

The Content-MD5 header is required for any request to upload an object with a retention period configured using Amazon S3 Object Lock. For more information about Amazon S3 Object Lock, see Amazon S3 Object Lock Overview in the Amazon S3 User Guide.

https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM