我使用以下格式上载图像文件:

var body = fs.createReadStream(tempPath).pipe(zlib.createGzip());
var s3obj = new AWS.S3({params: {Bucket: myBucket, Key: myKey}});
var params = {
  Body: body,
  ACL: 'public-read',
  ContentType: 'image/png'
};

s3obj.upload(params, function(err, data) {
  if (err) console.log("An error occurred with S3 fig upload: ", err);
  console.log("Uploaded the image file at: ", data.Location);
});

图像成功地上传到我的s3 bucket(没有错误消息,我在s3控制台中看到),但是当我试图在我的网站上显示它时,它会返回一个损坏的img图标。当我使用s3控制台文件下载程序下载图像时,我无法打开它,错误是文件“已损坏或损坏”。
如果我使用s3控制台手动上传一个文件,我可以在我的网站上正确显示它,所以我很确定我上传的方式有问题。
怎么了?

最佳答案

我终于找到了我问题的答案。我需要再发布一个参数,因为文件是gzip的(使用var body =..zlib.createGzip())。这解决了我的问题:

var params = {
  Body: body,
  ACL: 'public-read',
  ContentType: 'image/png',
  ContentEncoding: 'gzip'
};

08-05 08:18
查看更多