将文件上传到S3时,如何设置内容md5? [英] How can I set the content md5 when I upload a file to S3?

查看:2444
本文介绍了将文件上传到S3时,如何设置内容md5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在将文件上传到S3时设置content-MD5值。我可以看到md5哈希字符串并将其传递到 metadata.setContentMD5()但是在上传文件之后,我无法在Web控制台中看到此值,并且我无法通过java代码检索它。

I am trying to set the content-MD5 value when I upload a file to S3. I can see the md5 hash string and am passing that into metadata.setContentMD5() but after the file is uploaded, I can't see this value in the web console, and I can't retrieve it via java code.

我开始认为我很可能误解了内容MD5 get / set方法的目标。他们是否习惯让aws服务器验证收到的文件内容是否与我发送的内容一致?如果是这种情况,那么我应该在上传时使用 setContentMD5(my_md5)发送一个值,但是我应该只比较 getETag()的值我以后尝试从S3下载该对象时使用我计算的md5十六进制字符串?

I've come to think that it's likely I'm misunderstanding the goal of the content MD5 get/set methods. Are they used to let the aws server validate that the received file content is consistent with what I am sending? If that's the case then I should send in a value with setContentMD5(my_md5) when uploading, but should I then just compare the value of getETag() with my calculated md5 hex string when I later try to download that object from S3?

我在尝试设置这个md5时做错​​了什么值?

Am I doing something wrong in 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()能够拥有下载文件的值?

Am I calculating the MD5 string incorrectly? If I pass in a random string, I do get an error message, so it seems like S3 is happy with what I am sending via the above code. And if the MD5 string is correct, why can't I retrieve it later when using the client.getContentMD5() method? I understand that ETag should be the MD5 hex string, and I can also calculate that for my uploaded file (and get the same string that S3 calculates), so is it the case that I shouldn't expect the getContentMD5() to ever have a value for a downloaded file?

推荐答案

我认为你是对的: getContentMD5()只是相应的getter。setContentMD5() 1 。它告诉您请求的被叫方认为MD5哈希是什么。如果你想知道AWS认为散列是什么,你应该使用ETag。

I think you are correct: getContentMD5() is just the corresponding getter for setContentMD5() 1. It tells you what the callee side of the request thinks the MD5 hash is. If you want to know what AWS thinks the hash is, you should use the ETag.


getContentMD5



此字段表示在呼叫方侧计算的对象内容的base64编码的128位MD5摘要摘要。 ETag元数据字段表示由Amazon S3计算的十六进制编码的128位MD5摘要。

返回:base64编码的MD5哈希值相关对象的内容。 如果尚未设置内容的MD5哈希,则返回null。

Returns: The base64 encoded MD5 hash of the content for the associated object. Returns null if the MD5 hash of the content hasn't been set.

最后一部分可能意味着:除非您之前调用过 setContentMD5()

That last part presumably means: Returns null unless you have previously called setContentMD5()

这篇关于将文件上传到S3时,如何设置内容md5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆