簡體   English   中英

通過 base64 字符串上傳到 Amazon s3 的圖像損壞

[英]Broken image from image upload to Amazon s3 via base64 string

在發送 base64 字符串(轉換為圖像時大約 2.43MB)后,我在從亞馬遜 s3 取回完整圖像時遇到問題。 在此處輸入圖像描述 如果我通過https://compressnow.com/壓縮此圖像並上傳,則可以正常工作並且我得到完整圖像。

我可以在發送到 Amazon s3 之前壓縮 base64 字符串嗎?

這是上傳到亞馬遜 s3 的邏輯

await bucket
  .upload({
    Bucket: "test",
    Key: "test",
    Body: "test",
    ContentEncoding: 'base64',
    Metadata: { MimeType: "png },
  })

類似的問題節點 base64 上傳到 AWS S3 存儲桶使圖像損壞

The ContentEncoding parameter specifies the header that S3 should send along with the HTTP response, not the encoding of the object as far as what is passed to the AWS SDK. 根據文檔Body參數只是“對象數據”。 換句話說,您可能應該只刪除ContentEncoding參數,除非您有特殊需要並傳遞原始字節:

const fs = require('fs');
var AWS = require('aws-sdk');
s3 = new AWS.S3({apiVersion: '2006-03-01'});

// Read the contents of a local file
const buf = fs.readFileSync('source_image.jpg')
// Or, if the contents are base64 encoded, then decode them into buffer of raw data:
// const buf = new Buffer.from(fs.readFileSync('source_image.b64', 'utf-8'), 'base64')

var params = {
    Bucket: '-example-bucket-',
    Key: "path/to/example.jpg",
    ContentType: `image/jpeg`,
    ACL: 'public-read',
    Body: buf,
    ContentLength: buf.length,
};

s3.putObject(params, function(err, data){
    if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data);
    } else {
        console.log('succesfully uploaded the image!');
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM