Node.js copy remote file to server

前端 未结 3 2041
南旧
南旧 2020-12-11 20:33

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 remot

相关标签:
3条回答
  • 2020-12-11 20:57

    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}`);
    });
    
    0 讨论(0)
  • 2020-12-11 21:07

    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.

    0 讨论(0)
  • 2020-12-11 21:13

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题