how to set expiry date while uploading image to s3 using nodejs

2.6k views Asked by At

I want to upload an image/video and set expiry date as well in nodejs sdk. Can any one let me know how to set expiry date while uploading image to s3?

2

There are 2 answers

1
Alexandru Olaru On

You can use multer-s3 middleware for express, and then you can configure the headers that you want by providing the Expires, Cache-Control headers.

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    expires: 'Wed, 21 Oct 2020 07:28:00 GMT',
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})


app.post('/upload', upload.array('photos', 3), function(req, res, next) {
  res.send('Successfully uploaded ' + req.files.length + ' files!')
})
0
Aviel Vaknin On

You can set an expiry on the s3 bucket with the right permissions. In your bucket go to Management and then Lifecycle, start a new lifecycle and set the expiration (stage 3) to as many days as you want.

Though it is not an sdk solution I hope it can help.