0

OVERVIEW:

I'm using S3 to let users upload images. I'm working on an update route using a PUT method, which takes the url of a given image stored on a temp folder in S3 bucket and removes it to an images folder.

here is my upload object:

  const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'my-bucket',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    acl: 'public-read',
    key: function (request, file, cb) {
      cb(null, folder + Date.now() + file.originalname);
    }
  }),
  limits:{
    fileSize: 1024 * 1024 * 5
  },
  fileFilter:fileFilter
});

and in the route file i'm using:

upload.single('the_key_of_the_file')

PROBLEM:

this object works when uploading a new file, however not for updating it (renaming the file).

Community
  • 1
  • 1
ACEY
  • 1
  • 1
  • 2

2 Answers2

0

The problem is in below line:

key: function (request, file, cb) {
      cb(null, folder + Date.now() + file.originalname);
    }

Actully key is the name/id of the file in aws-s3.you are providing a new key so s3 will mark as new file.

You can change the logic here as per the name you want and it should be a string.

For example:

key: function (req, file, cb) {
    console.log(file);
    cb(null, `${Date.now().toString()}${file.originalname}`);
}
Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19
0

may be it's not the key object you need to pass in storage mine works for this

 filename: function (req, file, cb) {
  cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
}
Sanjay
  • 838
  • 1
  • 14
  • 21