Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda