anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 190 forks source link

Getting Missing required key 'Key' in params Error, #184

Open binjahid opened 2 years ago

binjahid commented 2 years ago

I have copied the exact code from the official documentation but still getting this error.

I have tried by installing asw-sdk and aws-sdk-client both give same error.

const upload = multer({ storage: multerS3({ s3, bucket: bucketName, acl: "public-read", metadata: (req, file, cb) => { cb(null, { fieldName: file.fieldname }); }, key: (req, file, cb) => { cb(null, file.originalname); }, }), });

Here clearly i have declared the key but getting the same error. Could anyone please tell me a solution.?

accounts-alphasophia commented 2 years ago

I am receiving exactly the same error, it's very strange

LinusU commented 2 years ago

Are you using the @aws-sdk/client-s3 package?

accounts-alphasophia commented 2 years ago

I am!

My multer configuration looks as such:

const multerStorageS3 = multerS3({
  s3: getS3Client(),
  bucket: process.env.awsS3Bucket as string,
  key: multerFileName
});

With the key-CB function:

export const multerFileName = (
  req: Request,
  file: Express.Multer.File,
  callback: (error: Error | null, filename: string) => void
) => {
  const ext: string = ".csv";
  crypto.randomBytes(16, (err: Error | null, raw: Buffer) => {
    if (!err && raw) {
      callback(null, raw.toString("hex") + ext);
    } else {
      callback(err, file.filename);
    }
  });
};

and the S3 client from:

import { S3Client, S3ClientConfig, GetObjectCommand } from "@aws-sdk/client-s3";

// Defines S3 client
const s3Config: S3ClientConfig = { region: "us-east-1" }
const s3Client: S3Client = new S3Client(s3Config);

export const getS3Client = (): S3Client => {
  return s3Client;
}
accounts-alphasophia commented 2 years ago

I have tried to trace down the problem. When executon reaches line 192 in the index.js of the multer-s3 package (executing the S3Storage.prototype._handleFile) to create the params dictionary the Key variable is set as you can see on the picture below. The error must be happening later downstream after creating the Upload object.. It's strange Screenshot 2022-08-01 at 15 54 07

Edit: Turns out, the files are committed to S3! The error is thrown on the "way back", not on the way there. The error is thown by @aws-sdk/client-s3/dist-cjs/protocols/Aws_restXml.js.. Still bad though, as it crashes my entire tool