Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
multiple kind of files for AWS + express -fileupload

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);
    });
  });
};



about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

the end result after many days of searching, it worked:

    export const uploadFileForTechnialReport = async (req, res) => {
      const { file } = req.files;
      const { mimetype } = file;
      const allowedTypes = ["jpeg", "jpg", "pdf"];

      try {
        if (!allowedTypes.some(type => mimetype.includes(type))) {
          return res.status(422).jsonp("Invalid format try with jpeg, jpg or PDF");
        }

        let url;

        console.log("start uploading to s3");
        switch (mimetype.split("/")[1]) {
          case "pdf":
            url = await uploadPDF(file.data);
            break;

          default:
            url = await upload(file.data);
            break;
        }
        console.log("upload to s3 done");

        return res.status(200).send(url);
      } catch (err) {
        return res.status(400).send(`AN_ERROR_OCCURED_${err}`);
      }
    };







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);
    });
  });
};

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

export const uploadPDF = async (fileData: string, options = { type: null }) => {
  const file = `${new Date().getTime()}.pdf`;

  const object = buildObjectPDF(file, fileData);

  const { Location } = await s3.upload(object).promise();

  return Location;
};

export const deleteFile = () => {};

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!