S3 randomly gives me "BadDigest" errors
I have a node.js app that pushes some data to Amazon S3 periodically. I am using a Put query to move the buffer to S3.
I know that the "content-md5" parameter for the S3 request must be a base64 encoded Md5 hash of the content I am pushing. What confuses me is that 90% of the time, my queries succeed. The other 10% of the time, without changing my hash method, Amazon returns me a "badDigest" error:
{ [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' } }
Has anyone else experienced this randomness from S3 before? Am I missing something?
Thank!
source to share
You probably forgot to specify 'utf8'
as a parameter for update
.
var status = 'काक्नोम्यत्क्नोम्यत्चं शक्नोम्यत्तुमतुम् ।तुम् ।् । नोपहिनस्ति माम् ॥';
var contentMd5 = crypto
.createHash('md5')
.update(status, 'utf8')
.digest('base64');
Not working in most cases, but not if your string contains multibyte characters.
source to share
aws-sdk will automatically calculate the ContentMD5 and ContentLength values for you. If you have a UTF-8 string and you are using the length ".्". To set the ContentLength value, the S3 value will return a BadDigest error. So the solution in my case just allowed aws-sdk to compute the ContentMD5 and ContentLength values.
source to share