Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
NodeJS: descargue el archivo de la URL y cárguelo en S3 Strorage

Estoy tratando de realizar la siguiente tarea: necesito descargar la imagen de la URL, luego cargarla en el almacenamiento S3 y devolver la ubicación del archivo cargado. Estoy usando funciones async/await para hacer la tarea, pero devuelve Promise {pendiente} y después de unos segundos devuelve la ubicación, quiero devolver la ubicación después de que se resuelva la promesa. Aquí está mi código:

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

Así que quiero devolver la ubicación cuando se resuelva la promesa. ¡Gracias de antemano por la ayuda!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La upload de su función es asíncrona y, por lo tanto, siempre devuelve una promesa que también debe await . await su llamada de upload . Si se encuentra en un entorno que no admite la await de nivel superior, use .then para registrar los resultados en su lugar o coloque el código de registro externo en la función auxiliar.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!