我正在尝试使用JavaScript fetch API、AWS API Gateway、AWS Lambda和AWS S3创建允许用户上传和下载媒体的服务。服务器正在使用 NodeJs 8.10;浏览器是 Google Chrome 版本 69.0.3497.92(官方版本)(64 位)。
从长远来看,允许的媒体包括音频、视频和图像。现在,我很乐意让图像正常工作。
我遇到的问题:我的浏览器端客户端(使用 fetch 实现)能够通过 API Gateway 和 Lambda 将 JPEG 上传到 S3。我可以使用curl
S3 控制台从我的 S3 存储桶下载 JPEG,然后在图像查看器中查看图像就好了。
但是,如果我尝试通过浏览器端客户端下载图像并获取,我将无法在浏览器中显示任何内容。
以下是浏览器端客户端的代码:
fetch(
'path/to/resource',
{
method: 'post',
mode: "cors",
body: an_instance_of_file_from_an_html_file_input_tag,
headers: {
Authorization: user_credentials,
'Content-Type': 'image/jpeg',
},
}
).then((response) => {
return response.blob();
}).then((blob) => {
const img = new Image();
img.src = URL.createObjectURL(blob);
document.body.appendChild(img);
}).catch((error) => {
console.error('upload failed',error);
});
这是使用Claudia.js的服务器端代码:
const AWS = require('aws-sdk');
const ApiBuilder = require('claudia-api-builder');
const api = new ApiBuilder();
api.corsOrigin(allowed_origin);
api.registerAuthorizer('my authorizer', {
providerARNs: ['arn of my cognito user pool']
});
api.get(
'/media',
(request) => {
'use strict';
const s3 = new AWS.S3();
const params = {
Bucket: 'name of my bucket',
Key: 'name of an object that is confirmed to exist in the bucket and to be properly encoded as and readable as a JPEG',
};
return s3.getObject(params).promise().then((response) => {
return response.Body;
})
;
}
);
module.exports = api;
以下是OPTION
Chrome 网络面板中的初始请求和响应标头:
这是随之而来的GET
请求和响应标头:
令我感兴趣的是,图像大小在 S3 控制台中报告为 699873(没有单位),但 GET 事务的响应正文在 Chrome 中报告为大约 2.5 MB(同样,没有单位)。
生成的图像是 16x16 方形和死链接。我在浏览器的控制台或 CloudWatch 中没有收到任何错误或警告。
我尝试了很多东西;很想听听外面的人能想出什么。
提前致谢。
编辑:在 Chrome 中: