【问题标题】:How to upload file to aws s3 with input file?如何使用输入文件将文件上传到 aws s3?
【发布时间】:2021-05-14 04:29:51
【问题描述】:

假设从客户端input 获取file 在下面的服务器函数中。

console.log(file) 是

{
  filename: '20200608_110520.jpg',
  mimetype: 'image/jpeg',
  encoding: '7bit',
  createReadStream: [Function: createReadStream]
}

现在我想将此文件上传到 aws s3。我混淆了上面的文件是什么。错误消息file 应该是type stringan instsance of BufferArrayBuffer 之一。但上面只是一个对象。正确的?那是什么?是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...我认为它使这变得复杂...

【问题讨论】:

    标签: node.js file amazon-s3 buffer


    【解决方案1】:

    解决了。

    我只是使用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()
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 1970-01-01
      • 2020-04-28
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多