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

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

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 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