链接到在S3存储桶中上传的图像不显示图像

时间:2018-09-22 18:48:13

标签: node.js amazon-web-services amazon-s3 aws-lambda

您好,我是AWS lambda和S3的新手。我正在尝试创建一个允许我上传图像的API。我有以下lambda代码来上传文件。上传后,我看到文件大小正确但文件已损坏。

let encodedImage = event.body;
console.log(encodedImage);
let decodedImage = Buffer.from(encodedImage, "binary");
console.log(decodedImage.length);

const filePath = `${Date.now()}.jpg`;
const params = {
    Bucket: "manufacturer-theme-assets",
    Key: filePath,
    "Body": decodedImage,
    ContentType: "image/jpeg",
    ACL: "public-read"
};
s3.putObject(params, (err, data) => {
    if (err) {
        callback(err, null);
    } else {
        let response = {
            statusCode: 200,
            "body": JSON.stringify(data)
            "isBase64Encoded": false
        };
        callback(null, response);
    }
}); 

1 个答案:

答案 0 :(得分:0)

确保图像使用的是相关的内容类型,请共享S3中损坏的图像链接或打开文件时出现的错误

否则尝试这个第一名并检查:

const filePath = `${Date.now()}.jpg`;    
var params = {
      ACL: "public-read", 
      Body: "decodedImage", 
      Bucket: "manufacturer-theme-assets", 
      Key: filePath
     };
s3.putObject(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
相关问题