且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Api Gateway(Lambda函数)将图像上传到S3存储桶

更新时间:2022-11-07 09:21:37

将应在Web浏览器中打开的对象上传到S3时,需要设置正确的内容类型.当S3为您的对象提供服务时,您设置的内容类型将作为HTTP标头发送.

When uploading objects to S3 that should be opened in a web browser, you need to set the correct content type. When S3 serves your object, the content type you set will be sent as a HTTP header.

Web浏览器使用 MIME类型确定如何处理URL,而不是文件扩展名.

Web browsers use MIME types to determine how to process URLs, not file extensions.

在您的情况下,传递给s3.upload的参数中缺少内容类型.而且,密钥不正确.

In your case, the content type is missing from the parameters your are passing to s3.upload. Moreover, the key is incorrect.

应该是:

const params = {
  Bucket: 'bucket-name',
  Key: fileFullName // must be a path within your bucket and not an url like you have in fileFullPath,
  Body: buffer,
  ContentType: fileMime.mime // Sets the content type header, without this your browser cannot infer the type (browsers use mime types, not file extensions)
};