Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to upload image to S3 bucket. #7

Closed
kiecooboi opened this issue Apr 22, 2021 · 1 comment
Closed

Failed to upload image to S3 bucket. #7

kiecooboi opened this issue Apr 22, 2021 · 1 comment

Comments

@kiecooboi
Copy link

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
Copy link
Owner

Amit-A commented Apr 22, 2021

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.

@Amit-A Amit-A closed this as completed Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants