Home > Back-end >  multiple kind of files for AWS express -fileupload
multiple kind of files for AWS express -fileupload

Time:09-22

so i've been trying to understand something with this. until now i could post images, but now i would like to add PDF's aswell. i don't get if i can just add 'pdf' to my content Type or if i have to do another new config?


  import AWS from "aws-sdk";

const s3 = new AWS.S3({
  accessKeyId: process.env.S3_ACCESS_KEY,
  secretAccessKey: process.env.S3_SECRET_KEY,
});

const buildObject = (key: string, body: string) => {
  return {
    Bucket: process.env.S3_BUCKET_NAME,
    Key: `${key}`,
    Body: body,
    ACL: "public-read",
    ContentEncoding: "base64",
    ContentType: "image/jpeg",
  };
};

export const upload = (fileData: string, options = { type: null }) => {
  let { type = "image/png" } = options;

  if (fileData.includes("base64") || !options.type) type = "image/jpeg";

  const file = `${new Date().getTime()}.${type.split("/")[1]}`;

  const object = buildObject(file, fileData);

  return new Promise((resolve, reject) => {
    s3.upload(object, (err, data) => {
      if (err) reject(err);
      else resolve(data.Location);
    });
  });
};



CodePudding user response:

I believe you should use the Content-Type: "application/pdf".

I would advise using one lambda for images and another for pdfs to give it single responsibility.

You will need one type of request for images and another for pdfs.

  • Related