0

I am working on a task that uploads image to SFTP server with Firebase Function. But the image source is not from my local computer but a http URL such as https://image.com/abc.jpg. I am using ssh2-sftp-client npm package. Currently I am using my mac both for client and server and it is working fine when I am accessing local file(/Users/shared/abc.jpeg) and uploading it to local server(/Uesrs/shared/sftp-server/abc.jpeg). But when I tried to have access to https://image.com/abc.jpg. and upload it to local server I got the error that says "ENOENT: no such file or directory/ ...". And below is my code

const functions = require('firebase-functions');
let Client = require('ssh2-sftp-client');


exports.sftpTest = functions.https.onRequest((request, response) => {

    let sftp = new Client();
    const config = {
        host: '192.***.***.***',
        port: '22',
        username: '****',
        password: '****'
    }



    let localFile = 'https://images.unsplash.com/photo-1487260211189-670c54da558d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80';



    let remoteFile = '/Users/Shared/unsplash.JPG';

   sftp.connect(config)
        .then(() => {
            sftp.fastPut(localFile, remoteFile);
        })
        .catch(err => {
            console.error(err.message);
        });

});

My first time to have access to sftp server and anyone's advice will be much appreciated.

M.K. Kim
  • 481
  • 1
  • 4
  • 17
  • Are you sure that this SFTP client library can both download content from a remote location, then upload it to another place? Perhaps it's limited to files on the local machine running the code? Are you sure that Cloud Functions can access your local server, given that it might be behind a firewall that could make it inaccessible from outside sources? – Doug Stevenson Dec 01 '19 at 00:35
  • Thanks for the feedback. Reading through the documentation of the library, I don't see an example of accessing file from a remote location. When I run firebase function locally (using firebase functions:shell command) to test this task (copying one image file to another folder within local setting), it worked so I assumed that it might work as well when accessing a http link. – M.K. Kim Dec 01 '19 at 03:58
  • Did you get this to work in the Firebase function? – 1252748 Oct 17 '21 at 17:58

1 Answers1

0

The method you are using from this library does not support usage in the way you are trying to set it up. the method fastPut usually is to upload local files to a remote server, I think you should use the fastGet method in order to download files from a remote server, however, please note that there are no notes that indicate that you can use these methods with the URL in the way you are trying to achieve.

rsalinas
  • 1,507
  • 8
  • 9