【发布时间】:2021-03-12 02:27:58
【问题描述】:
我尝试将本地图像上传到 s3 存储桶并不断获取
Error: Unsupported body payload object
图像可以是jpg、jpeg 或png。我读过图像只能作为base64 上传到s3,所以我使用readAsDataURL 将文件作为base64 字符串读取,然后用它创建一个缓冲区。
const reader = new FileReader()
reader.readAsDataURL(file)
const base64str = reader.result.replace(/^data:image\/\w+;base64,/, "");
const fileContent = Buffer.from(base64str,'base64')
上面的代码在 react 前端执行,fileContent 通过钩子设置为一个变量,然后该变量被 PUT 到我的服务器上
static uploadImage = (id, fileContent) => {
return axios.put(ROOT_URL + "/images/" + id, fileContent);
}
然后上传
await s3.upload({
Bucket: BUCKET_NAME,
Key: KEY,
Body: fileContent,
ContentType: TYPE,
}).promise();
我尝试了许多在此网站上找到的不同解决方案,但仍然收到错误消息。不知道我错过了什么。
编辑:这是我发现有用的线程之一,但没有修复错误。 Uploading base64 encoded Image to Amazon S3 via Node.js
【问题讨论】:
标签: javascript node.js reactjs amazon-web-services amazon-s3