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

170
Views
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 answers
Answer question

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