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

118
Vistas
How to set the Content-Type of a stream before uploading to S3 using NodeJS pipe?

In my NodeJS application, I have an axios response that returns a stream. I'd like to upload this stream to my S3 bucket, but I also need to know the ContentType to prevent S3's default assignment of application/octet-stream.

So far I've developed a function that uploads a stream using Stream.PassThrough:

streamUpload(key: string, acl: string) {

  const pass = new stream.PassThrough();

  const upload = new Upload({
    client: this.s3Client,
    params: {
      Key: key,
      Body: pass,
      Bucket: `${process.env.BUCKET}`,
      ACL: acl,
      ContentType: "" // <--- How can I find this from a stream without storing the file in memory??
    },
  })

  return {
    writeStream: pass,
    upload,
  };

}

As a result, I can use this function to pipe the stream data from my axios response to the PassThrough and then await using the upload.done() while the file is uploading like this:

// data is a stream!
const { data } = await axios.post('http://watermark:4006', {}, {
    responseType: "stream",
}); 

const { writeStream: uploadWS, upload } = s3API.streamUpload(audioEnding, 'public-read');

// pipe the stream to my `PassThrough`
// Can I add a custom Transform to extract the content-type from the stream before
// passing it to my uploadWriteStream?
pipeline(data, uploadWS, (e) => {
    if (!!e) {
        console.log(e)
    }
})

// The S3 library allows you to wait while data is being uploaded
await upload.done()

I discovered a library called file-type that allows retrieving the file type via stream using fileTypeFromStream, however I'm not sure how to add this to my pipeline to extract the file type before running the next step of my pipeline because it is a function that you must pass a stream argument that must be awaited before receiving the file type.

Would love to hear any suggestions!

about 4 years ago · Juan Pablo Isaza
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