简体   繁体   English

在上传到 AWS S3 时计算 Node.js 中本地文件的 ETag

[英]Calculating the ETag for a local file in Node.js while uploading to AWS S3

I'm currently trying to implement a system that verifies the integrity of files I upload to S3.我目前正在尝试实施一个系统来验证我上传到 S3 的文件的完整性。 Through the AWS SDK for JavaScript, I am able to retrieve the ETag of the remote object, however, the MD5 hash for my local object doesn't match the ETag.通过适用于 JavaScript 的 AWS 开发工具包,我能够检索远程对象的 ETag,但是,我的本地对象的 MD5 哈希与 ETag 不匹配。 I am currently using crypto for generating it, as follows:我目前正在使用crypto来生成它,如下所示:

var hash = crypto.createHash('md5');
stream = fs.createReadStream(localfile);

stream.on('data', function (data) {
  hash.update(data, 'utf8')
})

stream.on('end', function () {
  md5 = hash.digest('hex');

  < rest of the code >
});

According to all the results I've found while looking for answers, the ETags for S3 objects should be simple MD5 hashes: eg the AWS documentation (emphasis mine):根据我在寻找答案时发现的所有结果,S3 对象的 ETag 应该是简单的 MD5 哈希:例如AWS 文档(重点是我的):

To ensure that data is not corrupted traversing the network, use the Content-MD5 header.为确保数据在通过网络时不会损坏,请使用 Content-MD5 标头。 When you use this header, Amazon S3 checks the object against the provided MD5 value and, if they do not match, returns an error.当您使用此标头时,Amazon S3 会根据提供的 MD5 值检查对象,如果它们不匹配,则返回错误。 Additionally, you can calculate the MD5 while putting an object to Amazon S3 and compare the returned ETag to the calculated MD5 value.此外,您可以在将对象放入 Amazon S3 时计算 MD5,并将返回的 ETag 与计算的 MD5 值进行比较。

However, my local hash doesn't match the remote ETag.但是,我的本地哈希与远程 ETag 不匹配。 Am I missing anything here?我在这里错过了什么吗?

Thanks for your time!谢谢你的时间!

Clarification: I'm only uploading small plain text files, and I'm not dealing with multi-part uploads.澄清:我只上传小的纯文本文件,我不处理多部分上传。 I've seen that a lot of questions around the web are in regards to multi-part uploads, but this is just for simple uploads.我已经看到网络上的很多问题都与分段上传有关,但这仅适用于简单上传。 Thanks!谢谢!

Problem Exists Between Keyboard And Chair.键盘和椅子之间存在问题。 The issue was with the actual file uploads and not with the md5 sum.问题在于实际文件上传而不是 md5 总和。 See the comment section in the original question.请参阅原始问题中的评论部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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