S3随机给我“BadDigest”错误

时间:2012-04-08 03:04:41

标签: node.js amazon-s3

我有一个node.js应用程序,定期将一些数据推送到Amazon S3。我正在使用Put请求将缓冲区推送到S3。

我知道S3请求的“content-md5”参数需要是我正在推送的内容的base64编码Md5哈希。 让我感到困惑的是90%的时间,我的请求成功。其他10%的时间,没有我的哈希方法发生变化,亚马逊给了我“badDigest”错误:

{ [Error: API error with HTTP Code: 400]
  headers: 
   { 
     'content-type': 'application/xml',
     'transfer-encoding': 'chunked',
     date: 'Fri, 06 Apr 2012 02:20:14 GMT',
     connection: 'close',
     server: 'AmazonS3' },
  code: 400,
  document: 
   { Code: 'BadDigest',
     Message: 'The Content-MD5 you specified did not match what we received.',
     ExpectedDigest: 'fPRrmxapcSHmI2gljme1Fg==',
     CalculatedDigest: 'w6PoDxh2ty478+Mw2UwTrA==',
     RequestId: '1018E7A80A8B0B00',
     HostId: 'W/SK/OovQHlsi593DJ154pkHdOrUk3oMWmIGNdOKj3WaHa8cBknhB+7H5IdZLUjt' } }

有没有其他人在S3之前经历过这种随机性?我错过了一些明显的东西吗

谢谢!

2 个答案:

答案 0 :(得分:2)

您可能忘记将'utf8'指定为update的参数。

var status = 'काक्नोम्यत्क्नोम्यत्चं शक्नोम्यत्तुमतुम् ।तुम् ।् । नोपहिनस्ति माम् ॥';
var contentMd5 = crypto
                  .createHash('md5')
                  .update(status, 'utf8')
                  .digest('base64');

如果没有它适用于大多数情况,但不适用于您的字符串包含多字节字符的情况。

答案 1 :(得分:1)

aws-sdk会自动为您计算ContentMD5和ContentLength值。如果你有一个UTF-8字符串并且你使用'。''。length来设置ContentLength值,S3将返回BadDigest错误。所以我的案例中的解决方案就是让aws-sdk计算ContentMD5和ContentLength值。

相关问题