[英]How to upload file to aws s3 with input file?
假设从客户端input
获取file
在服务器 function 下面。
console.log(file) 是
{
filename: '20200608_110520.jpg',
mimetype: 'image/jpeg',
encoding: '7bit',
createReadStream: [Function: createReadStream]
}
现在我想将此文件上传到 aws s3。 我混淆了上面的文件是什么。 错误消息file
应该是type string
, an instsance of Buffer
, ArrayBuffer
之一。 但上面只是一个 object。 正确的? 那是什么? 是Blob
吗? 或File
? 还是只是一个Object
? 我怎样才能将其转换为正确的?
const imageUpload = async (file) => {
...
const s3 = new AWS.S3()
const params = {
Bucket: S3_BUCKET,
Key: 'CoolFileName',
Body: file,
}
const { Location, Key } = await s3.upload(params).promise()
// TypeError [ERR_INVALID_ARG_TYPE]:
// The first argument must be of type string or
// an instance of Buffer, ArrayBuffer, or Array
// or an Array-like Object. Received an instance of Object**
}
我正在使用 GraphQL ......我认为它让这变得复杂......
解决了。
我只是使用createReadStream()
const imageUpload = async (file) => {
...
const s3 = new AWS.S3()
const { createReadStream, filename, mimetype, encoding } = await file
const params = {
Bucket: S3_BUCKET,
Key: 'CoolFileName',
Body: createReadStream(),
}
const { Location, Key } = await s3.upload(params).promise()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.