Upload to S3 using multer without a middleware function

0 votes

5

2

I am using multer to upload media to my s3 bucket. I am using multer-s3 as a middleware to upload media like:

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: myBucket,
    key: function (req, file, cb) {
      cb(null, new Date().getTime() + '_' + file.originalname)
    }
  })
});

and calling it in route as:

router.post("/media",upload.single("media"))

This works great. But a scenario that is not working for me is: Let say i upload one image and i want to store multiple version of it by resizing it before uploading. I am not able to call the upload function like a normal one. I want to do something like:

let thumbnail = myFunctionToReturnImageFile(req.file);
upload(thumbnail);
Feb 24, 2022 in AWS by Rahul
• 2,120 points
3,321 views

1 answer to this question.

0 votes

Use multer-s3-transform middleware to handle S3 upload with file transformation like image resizing using sharp

const multer= require('multer')
const multerS3= require('multer-s3-transform')
const sharp= require('sharp')
const AWS= require('aws-sdk')
const S3= new AWS.S3({
accessKeyId= ...
secretAccessKey: ...
})
const upload= multer ({
storage multerS3{{
s3:S3;
bucket:....,
shouldTransform: true ,
transforms: [
{
id: 'original',
key=(req,file,cb)=> cb( null new Date().getTime() + '_' + req.file.originalname),
        transform: (req, file, cb) => cb(null, sharp().jpg())
      },
      {
        id: 'large',
        key: (req, file, cb) => cb(null, new Date().getTime() + '_large_' + req.file.originalname),
        transform: (req, file, cb) => cb(null, sharp().resize(1200, 900).jpg())
      },
      {
        id: 'small',
        key: (req, file, cb) => cb(null, new Date().getTime() + '_small_' + req.file.originalname),
        transform: (req, file, cb) => cb(null, sharp().resize(400, 300).jpg())
      }
    ]
  })
})

router.post('/media', upload.single('media'))
answered Feb 24, 2022 by Korak
• 5,820 points

Related Questions In AWS

0 votes
1 answer

How to upload a file to Amazon S3 without passing it my server?

This article pretty much explains the entire ...READ MORE

answered Aug 14, 2018 in AWS by Archana
• 4,170 points
3,272 views
0 votes
0 answers

How to upload a file in to aws s3 by using programmatically??

Sep 13, 2019 in AWS by anonymous

closed Sep 16, 2019 by Kalgi 3,901 views
+1 vote
1 answer
0 votes
1 answer

Unziiping a tar.gz file in aws s3 bucket and upload it back to s3 using lambda

Hi@khyati, You can do your task using lambda. ...READ MORE

answered Dec 3, 2020 in AWS by MD
• 95,460 points
18,753 views
+1 vote
4 answers

Can a URL be directly uploaded to S3 using POST?

You can read this blog and get ...READ MORE

answered Oct 25, 2018 in AWS by chamunda
3,294 views
0 votes
1 answer
0 votes
1 answer

Lambda & DynamoDB: "The parameter cannot be converted to a numeric value"

It cannot be converted because you are ...READ MORE

answered Feb 23, 2022 in AWS by Korak
• 5,820 points
4,177 views
0 votes
1 answer

Not able to get_item from AWS dynamodb using python?

You are mixing resource and client objects ...READ MORE

answered Feb 24, 2022 in AWS by Korak
• 5,820 points
1,718 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP