3

I am getting this error while working on a node backend using typescript and this is the function for file upload to aws S3. Im new to using typescript so can anyone help me on this.

import AWS from "aws-sdk";
import multer from "multer";
import multerS3 from "multer-s3";

let S3 = new AWS.S3({
    accessKeyId: process.env.AWS_KEY,
    secretAccessKey: process.env.AWS_SECRET
})

const upload = multer({
    storage: multerS3({
        s3:S3, //error here
        bucket: 'bucket-name',
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, Date.now().toString())
        }
    })
})


export { upload }
sauravscode
  • 61
  • 1
  • 5

1 Answers1

7

So the problem is that you're creating an s3 client using the version 2 of the sdk instead of the v3 sdk (@aws-sdk/client-s3). The aws-sdk provides the version 2 client. @aws-sdk/client-s3 is part of the V3 javascript SDK.

Make sure to install npm i @aws-sdk/client-s3.

You can read about the client s3 sdk here

import AWS from "aws-sdk";
import { S3Client } from '@aws-sdk/client-s3';
import multer from "multer";
import multerS3 from "multer-s3";

const s3Config = new S3Client({
   region: 'us-west-1',
   credentials:{
      accessKeyId:'',
      secretAccessKey:''
  }
})

const upload = multer({
    storage: multerS3({
        s3: s3Config,
        bucket: 'bucket-name',
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, Date.now().toString())
        }
    })
})


export { upload }
user
  • 1,022
  • 2
  • 8
  • 30