Multer S3 i want to upload image and then upload a resized version of it

72 Views Asked by At

This is my code as you can see I have a middleware of multer upload uploading the file after that I am saving the file path to my database so I was wondering how can I upload the resized image afterward I figured out how to resize it after receiving a buffer from sharp I now need to upload it I cant find out a way to upload since it's a middleware thing

Note I am using @aws-sdk/client-s3 The AWS SDK for JavaScript v3

My issue was fixed apparently i found a way i added the newly created code incase anyone needs it apparently there is a PutObjectCommand in the client sdk that i can use to upload the newly created buffer

router.post("/uploadImage", upload.single("file"), SavePhotoPath);
   
const config = {
credentials: {
  accessKeyId: process.env.S3ACCESSKEYID,
  secretAccessKey: process.env.S3SECRETACCESSKEY,
},
region: process.env.S3REGION,
};

// Instantiate a new s3 client
const s3 = new S3Client(config);    

var upload = multer({
  storage: multerS3({
    s3: s3,`enter code here`
    acl: "public-read",
    bucket: process.env.S3BUCKETNAME
    key: function (req, file, cb) {
      try {
        cb(null, "metals/" + file.originalname);  
      } catch (er) {}
    },
  }),
});

//sharp resize 

var bufferResizedImage = await sharp(image)
                                          .resize(800, 800)
                                          .toBuffer()
                                          
                                          
// solution
const uploadThubnailImage = async (name, buffer) =>
  await s3.send(
    new PutObjectCommand({
      Bucket: process.env.S3BUCKETNAME,
      Key: "metals/" + getfileName(name),
      Body: buffer,
      ACL: "public-read",
    })
  );

0

There are 0 best solutions below