简体   繁体   中英

Node.js copy remote file to server

Right now I'm using this script in PHP. I pass it the image and size (large/medium/small) and if it's on my server it returns the link, otherwise it copies it from a remote server then returns the local link.

function getImage ($img, $size) {
    if (@filesize("./images/".$size."/".$img.".jpg")) {
        return './images/'.$size.'/'.$img.'.jpg';
    } else {
        copy('http://www.othersite.com/images/'.$size.'/'.$img.'.jpg', './images/'.$size.'/'.$img.'.jpg');
        return './images/'.$size.'/'.$img.'.jpg';
    }
}

It works fine, but I'm trying to do the same thing in Node.js and I can't seem to figure it out. The filesystem seems to be unable to interact with any remote servers so I'm wondering if I'm just messing something up, or if it can't be done natively and a module will be required.

Anyone know of a way in Node.js?

You should check out http.Client and http.ClientResponse . Using those you can make a request to the remote server and write out the response to a local file using fs.WriteStream .

Something like this:

var http = require('http');
var fs = require('fs');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/',
  {'host': 'www.google.com'});
request.end();
out = fs.createWriteStream('out');
request.on('response', function (response) {
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    out.write(chunk);
  });
});

I haven't tested that, and I'm not sure it'll work out of the box. But I hope it'll guide you to what you need.

To give a more updated version (as the most recent answer is 4 years old, and http.createClient is now deprecated), here is a solution using the request method:

var fs = require('fs');
var request = require('request');
function getImage (img, size, filesize) {
    var imgPath = size + '/' + img + '.jpg';
    if (filesize) {
        return './images/' + imgPath;
    } else {
        request('http://www.othersite.com/images/' + imgPath).pipe(fs.createWriteStream('./images/' + imgPath))
        return './images/' + imgPath;
    }
}

If you can't use remote user's password for some reasons and need to use the identity key (RSA) for authentication, then programmatically executing the scp with child_process is good to go

const { exec } = require('child_process');

exec(`scp -i /path/to/key username@example.com:/remote/path/to/file /local/path`, 
     (error, stdout, stderr) => {

    if (error) {
      console.log(`There was an error ${error}`);
    }

      console.log(`The stdout is ${stdout}`);
      console.log(`The stderr is ${stderr}`);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM