Type 'S3' is missing the following properties from type 'S3Client': destroy, middlewareStack, sendts(2739)

Viewed 75

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 }
1 Answers

So the problem is that you are creating an s3 client using the version 2 of the s3 ask 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 }
Related