Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:08:15+00:00 2026-05-30T22:08:15+00:00

I’m trying to devise a way to upload a file from a url to

  • 0

I’m trying to devise a way to upload a file from a url to s3 using request and knox. Currently, my code looks like this:

request(item.productImage, function(err, res, body) {
  if (!err && res.statusCode == 200) {
    fs.writeFile('/tmp/' + filename, body, 'base64', function(err, data){
      if (err) { 
        return console.log(err);
      }

      client.putFile('/tmp/' + filename, '/item/' + item._id + '/' + filename, function(err, res) {
        if (err) {
          return console.log(err);
        }
      }); 
    });
  }
});

This doesn’t work as it downloads about 652 bytes of a 4kb file before it stops. Strangely, if I don’t provide a callback to fs.writeFile() it downloads the entire 4kb locally.

What’s the best way of accomplishing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:08:16+00:00Added an answer on May 30, 2026 at 10:08 pm

    There are a number of questions about this here on Stackoverflow, but I can’t seem to find one that answers your question. The solution below should work, however, I’m having trouble getting knox to work at all on my machine right now. I hope you will be more lucky!

    UPDATE: I seem to have had some problems with s3 here, the code below works — I did change one thing, you need to specify encoding as null to request, so you get a Buffer back. Otherwise, binary data won’t work so well.

    request(item.productImage, {encoding: null}, function(err, res, body) {
      if(!err && res.statusCode == 200) {
        var req = client.put('/item/' + item._id + '/' + filename, {
          'Content-Type': res.headers['content-type'],
          'Content-Length': res.headers['content-length']
        });
    
        req.on('response', function(res) {
          console.log('response from s3, status:', res.statusCode, 'url:', req.url);
        });
    
        req.on('error', function(err) {
          console.error('Error uploading to s3:', err);
        });
    
        req.end(body);
      }
    });
    

    Note: With this solution, you avoid having to buffer the files to disk – that’s why I chose to use the lower level put method of the knox client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am using Paperclip to handle profile photo uploads in my app. They upload
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.