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

169
Visualizações
NodeJS: Download file from url and upload to S3 Strorage

I'm trying to accomplish the following task: I need to download image from url, then upload it to S3 storage and return the location of the uploaded file. I'm using async/await functions to do the task, but it returns Promise { pending } and after few seconds returns the location, i want to return location after promise is resolved. Here is my code:

    // Space config
    const spaceEndPoint = new AWS.Endpoint("fra1.digitaloceanspaces.com");
    const s3 = new AWS.S3({
      endpoint: spaceEndPoint,
      accessKeyId: "xxxxxxxxx",
      secretAccessKey: "xxxxxxxxxxxxxxxxx",
    });

    // Download image from url
    const downloadImage = async (url) => {
      try {
        const file = axios
          .get(url, {
            responseType: "stream",
          })
          .then((res) => res.data)
          .catch((err) => console.log(err));
        return file;
      } catch (err) {
        console.log(err);
      }
    };
    
    // Upload to space
    const upload = async (fileUrl) => {

      // Get file name from url
      const fileName = path.basename(fileUrl);

      // Path to save tmp file
      const localFilePath = path.resolve(__dirname, "../downloads", fileName);

      // Download file
      const file = await downloadImage(fileUrl);

      // Write file to disk
      await file.pipe(fs.createWriteStream(localFilePath));

      // Upload params
      const params = {
        Bucket: "sunday",
        Body: fs.createReadStream(localFilePath),
        Key: path.basename(fileName),
        ContentType: "application/octet-stream",
        ACL: "public-read",
      };
    
      const { Location } = await s3.upload(params).promise();
    
      return Location;
    };
    
    console.log(
      upload(
        "https://i.pinimg.com/474x/e9/62/7c/e9627ce6fe731ba49597d3a83e21e398.jpg"
      ).then((data) => data)
    );

// Result:
Promise { <pending> }
https://sunday.fra1.digitaloceanspaces.com/e9627ce6fe731ba49597d3a83e21e398.jpg

So i want to return location when promise is resolved. Thanks in advance for help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your function upload is async and thus always returns a promise that should be awaited too. await your upload call. If you're in environment that doesn't support top level await, use .then to log results instead or put outer logging code in helper function.

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