0

I would like to send my file to amazon S3 that I've taken from client side as multipart/form-data.

In the original doc of s3.upload it expect body as stream like below. enter image description here

And for a stream I need to store file into file system and give path. enter image description here

Here the question comes. I have my file object that I've taken from client side as multipart/form-data

destination:"uploads"
encoding:"7bit"
fieldname:"file"
filename:"test.zip"
mimetype:"application/zip"
originalname:"test.zip"
path:"uploads\test.zip"
size:4440

Is there any way that I send file directly without storing file system?

2 Answers2

1

You didn't mention form-data method that you're using on nodejs side. if you're using form-data npm module then you can convert file data into buffer directly using form.getBuffer()

Checkout more ref here

In case you're using request module to download file do as below:

Use request-promise npm module.

let rp = require('request-promise');
const fileUrl = 'http://sample.com/file';
const downloadedFile  = await rp(fileUrl);
const fileStreamData = Buffer.from(downloadedFile, 'utf-8');
// Use fileStreamData in S3 upload as Body parameter.
Dipten
  • 1,031
  • 8
  • 16
  • actually I didn't use form-data, I get the file from request and save to file system with multer. after that give the path to buffer and send s3. Are you saying that I can use form data library insted of buffer ? – ahmedsalihh Mar 09 '20 at 07:47
  • Modified answer for request based approach. – Dipten Mar 09 '20 at 11:50
  • I was using multer for getting the multipart file and configured storage as default like below. `var storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, 'uploads'); }, filename: (req, file, cb) => { cb(null, file.originalname); }, }); var upload = multer({ storage: storage });` but I realized when you do like that it doesn't give you the original file. So I've deleted the storage conf and got the original file, after that I was able to access buffer of file than sent `var fileStreamData = Buffer.from(file.buffer, 'utf-8');` thanks btw :) – ahmedsalihh Mar 10 '20 at 12:31
1

If you use clean express - you need to read the file input from user - you can use some middleware which will do that for you. Then you will have the binary Buffer and you can directly send it like in example code you provided for S3.upload.

Seti
  • 2,169
  • 16
  • 26