![](/img/trans.png)
[英]Syntax error: unexpected token in Javascript / posting data to amazon s3
[英]Sending binary data to Amazon S3 (Javascript)
Amazon S3將我的二進制數據解釋為非UTF-8並在寫入存儲桶時對其進行修改。
使用官方s3 Javascript客戶端的示例:
var png_file = new Buffer( "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", "base64" ).toString( "binary" );
s3.putObject( {
Bucket: bucket,
Key: prefix + file,
ContentType: "image/png;charset=utf-8",
CacheControl: "public, max-age=31536000",
Body: png_file
// , ContentLength: png_file.length
}, function( e ){
if ( e ) {
console.log( e );
} else {
s3.getObject( {
Bucket: bucket,
Key: prefix + file
}, function( e, v ) {
if ( e ) {
console.log( e )
} else {
console.log( v.ContentLength );
}
} );
}
} );
當原始png_file
為85
返回105
。 S3以某種方式修改了我的文件,我認為它與字符集有關。
如果我取消注釋Content-Length
行, The Content-MD5 you specified did not match what we received
在putObject()
上收到400錯誤: The Content-MD5 you specified did not match what we received
。
如果我使用ContentMD5: crypto.createHash("md5").update(png_file).digest("base64")
自己計算MD5哈希(而不是讓S3庫執行),則會得到相同的結果ContentMD5: crypto.createHash("md5").update(png_file).digest("base64")
。 這似乎確認了我發送的數據與S3接收的數據之間的差異。
我讀過一個標題相似的問題 ,但是並沒有解決問題。
S3 putObject()
假定為Buffer或UTF-8字符串。 我應該發送二進制文件,而不是“二進制字符串”,這意味着使用new Buffer(...)
而不是new Buffer(...).toString("binary")
。
S3似乎不太可能實際修改您要上傳的內容。 似乎不太可能在下載時被錯誤地解釋,因為這對於png似乎無效:
ContentType: "text/data;charset=utf-8",
這對於png文件是不正確的。 我建議您要這樣做:
ContentType: "image/png",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.