node.js - 上传到 S3 的节点 Js 错误:TypeError [ERR_INVALID_ARG_TYPE]
问题描述
我正在尝试将图像上传到我的 amazon s3 存储桶,但我不断收到错误消息
TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或 Array-like Object 类型之一。
所以基本上我知道我必须将其更改为缓冲区,经过一番搜索后,我尝试了一些解决方案,但没有一个有效,有人可以帮我指出我出了什么问题吗?
const s3Client = s3.s3Client;
const params = s3.uploadParams;
//const fileContent = fs.readFileSync(req.file); <-- tried this and still gave the same error
var fileContent = Buffer.from(req.file, 'base64')
params.Key = req.file.filename
params.Body = fileContent
console.log(req.file)
s3Client.upload(params, (err, data) => {
if (err) {
res.status(500).json({error:"Error -> " + err});
}
res.json({message: 'File uploaded successfully! -> keyname = ' + req.file.originalname});
});
这是图像本身,也许我转换错误?详细信息取自 req.file 控制台日志
{
fieldname: 'image',
originalname: 'Kimmi.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './images/',
filename: 'c6b46e20-4efc-11ea-8b6a-1b4d698f6335.jpeg',
path: 'images\\c6b46e20-4efc-11ea-8b6a-1b4d698f6335.jpeg',
size: 35920
}
解决方案
我不太确定,但我认为您正在使用 multer ?如果您使用 multer,您基本上可以使用 req.file.buffer。
如果图像是大流示例,我的建议是使用流而不是缓冲区
推荐阅读
- java - JMeter,线程组之间传值
- node.js - 从 mongo 向 find() 添加字段后无法检索数据
- algorithm - 寻找 Apple Watch 气泡坐标的算法
- node.js - 如何通过 HTML(脚本标签)获取 Electron 应用程序对象?
- python - 如何将一个熊猫文本列拆分为固定数量的列
- reactjs - 如何在 React Hook Form v7 中使用单选按钮?
- mongodb - 执行 $map 后,如何在 $addFields 阶段将 $toObjectId 用于 id 数组?
- jquery - 如何从 jQuery 数据表中过滤逗号分隔值和精确值?
- json - 如何将数据从控制器 (JSON) 加载到 View 中的 ListBox?
- c++ - C++ 只调用了一个构造函数