Amit-A / formidable-serverless

Enables use of formidable (node.js module for parsing form data, especially file uploads) in serverless environments.
https://www.npmjs.com/package/formidable-serverless
MIT License
35 stars 3 forks source link

Failed to upload image to S3 bucket. #7

Closed kiecooboi closed 3 years ago

kiecooboi commented 3 years ago

I am trying to upload image to s3 bucket with aws-sdk and formidable-serverless, but I got error when uploading the image from frontend, here is my code in use:

const aws = require("aws-sdk")
const uuid = require("uuid")
import formidable from "formidable-serverless"

const Bucket = "bucket"

const s3 = new aws.S3({
    accessKeyId: "xxx",
    secretAccessKey: "xxx",
    region: "xxx",
    endpoint: "s3.xxx",
    signatureVersion: "v4",
})

export default async (req, res) => {
    const form = new formidable.IncomingForm()
    form.parse(req, (err, fields, files) => {
        const file = {
            Bucket: Bucket,
            Key: uuid.v4() + " - " + files.file.name,
            Body: files.file,
            ContentType: files.file.type,
        }
        console.log(file)
        s3.putObject(file, function (err, data) {
            if (err) console.log("Error: " + err)
            else res.send(data)
        })
    })
}

Here is the error I can't handle with:

NetworkingError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of File

console.log(file) shows:

{
  Bucket: 'bucket',
  Key: 'd5f53b62-dbc3-4206-99c4-482970d0f16a - test.png',
  Body: File {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    size: 89865,
    path: '/var/folders/7_/8fbg7my57lx3z46ccfwjddj80000gn/T/upload_3463316247a7d167eadba05a7c0d01b3',
    name: 'test.png',
    type: 'image/png',
    hash: null,
    lastModifiedDate: 2021-04-22T14:20:11.945Z,
    _writeStream: WriteStream {
      _writableState: [WritableState],
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      path: '/var/folders/7_/8fbg7my57lx3z46ccfwjddj80000gn/T/upload_3463316247a7d167eadba05a7c0d01b3',
      fd: 57,
      flags: 'w',
      mode: 438,
      start: undefined,
      autoClose: true,
      pos: undefined,
      bytesWritten: 89865,
      closed: false,
      [Symbol(kFs)]: [Object],
      [Symbol(kCapture)]: false,
      [Symbol(kIsPerformingIO)]: false
    },
    [Symbol(kCapture)]: false
  },
  ContentType: 'image/png'
}

When I set Body to Body: toString(files.file) then I can upload but the uploaded image can not be opened.

Amit-A commented 3 years ago

This doesn't look like an issue with formidable-serverless...

You might need to look into handling streams / piping them into S3. Or check the type of files.file and make sure it matches what's expected by the S3 client for the Body input. It looks odd that you're using files.file.name and then using files.file - you're feeding the object to Body instead of the file body (are you sure the body is at files.file?)

Anyways, this is beyond the scope of formidable-serverless.