Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda