How to configure md5 content when uploading a file to S3?

I am trying to set the content-MD5 value when uploading a file to S3. I see the md5 hash line and pass this to metadata.setContentMD5(), but after downloading the file, I do not see this value in the web console, and I can not get it through the Java code.

I came to the conclusion that I probably do not understand the purpose of using the MD5 get / set methods. Are they used so that the aws server confirms that the received contents of the file match what I'm sending? If in this case I have to send the value with setContentMD5(my_md5)at boot, but should I just compare the value getETag()with my calculated md5 hexadecimal string when I try to load this object from S3 later?

Am I doing something wrong trying to set this md5 value?

String access_key = "myaccesskey";
String secret_key = "mysecretkey";
String bucket_name = "mybucketname";
String destination_key = "md5_test.txt";
String file_path = "C:\\my-text-file.txt";

BasicAWSCredentials creds = new BasicAWSCredentials(access_key, secret_key);
AmazonS3Client client = new AmazonS3Client(creds);
client.setRegion(RegionUtils.getRegion("us-east-1"));

File file = new File(file_path);

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
metadata.setContentLength(file.length());

FileInputStream fis = new FileInputStream(file);
byte[] content_bytes = IOUtils.toByteArray(fis);
String md5 = new String(Base64.encodeBase64(DigestUtils.md5(content_bytes)));
metadata.setContentMD5(md5);

PutObjectRequest req = new PutObjectRequest(bucket_name, destination_key, file).withMetadata(metadata);
PutObjectResult result = client.putObject(req);

GetObjectMetadataRequest mreq = new GetObjectMetadataRequest(bucket_name, destination_key);
ObjectMetadata retrieved_metadata = client.getObjectMetadata(mreq);

// I think I expected getContentMD5 below to show the string I passed in
// during the upload, but the below prints "md5:null"
System.out.println("md5:" + retrieved_metadata.getContentMD5());

MD5? , , , S3 , . MD5 , client.getContentMD5()? , ETag MD5, , ( , S3 ), , getContentMD5() - ?

+4
2

, : getContentMD5() setContentMD5() 1. , , MD5. , AWS , ETag.

getContentMD5

128- MD5 base64 , . ETag 128- MD5-, Amazon S3.

: MD5, base64, . null, MD5.

: null, setContentMD5()

+2

MD5, , Amazon , .

MD5 , , . .

API , , , setter.

+2

Source: https://habr.com/ru/post/1628747/


All Articles