简体   繁体   English

使用nodejs将图像上传到s3时如何设置到期日期

[英]how to set expiry date while uploading image to s3 using nodejs

I want to upload an image/video and set expiry date as well in nodejs sdk.我想上传图像/视频并在 nodejs sdk 中设置到期日期。 Can any one let me know how to set expiry date while uploading image to s3?任何人都可以让我知道如何在将图像上传到 s3 时设置到期日期?

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.您可以使用multer-s3中间件进行 express,然后您可以通过提供ExpiresCache-Controlmulter-s3配置所需的标头。

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!')
})

You can set an expiry on the s3 bucket with the right permissions.您可以使用正确的权限在 s3 存储桶上设置到期时间。 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.在您的存储桶中,依次转到管理和生命周期,开始一个新的生命周期并将到期(阶段 3)设置为您想要的任意天数。

Though it is not an sdk solution I hope it can help.虽然它不是 sdk 解决方案,但我希望它能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM