如何使用 Azure 函数将通过 Blob 触发器获取的文件保存到 SFTP 服务器。

huangapple go评论63阅读模式
英文:

How to save file obtained by blob trigger to SFTP server with Azure Functon

问题

我想将通过 Blob 触发器获取的文件保存到 Azure 函数中的 SFTP 服务器。

module.exports = function (context, myBlob) {
    context.log("JavaScript blob trigger function processed blob \n Blob:", context.bindingData.blobTrigger, "\n Blob Size:", myBlob.length, "Bytes");
    var url = context.bindingData.uri;
    context.log(context);
    context.log(url);
    
    var Client = require('ssh2').Client;

    var myhost = new Client();
    myhost.on('ready',function(){

        //ready
        console.log('ready sftp...');

        //sftp
        myhost.sftp(function(err,sftp){

            //throw if error
            if(err) throw err;

            //file path
            var localfile = url;
            var remotefile = "./sftp-test/testfile.txt";

            //upload
            sftp.fastPut(localfile,remotefile,{},function(err){
                if(err){
                    console.log("upload error.");
                }else{
                    console.log("uploaded.");
                    myhost.end();
                }
            });
        });

    //connection
    }).connect({
        host: '<SFTP服务器的IP地址>',
        port: 22,
        username: 'sftpuser',
        privateKey: require('fs').readFileSync('D:/home/site/wwwroot/verification-BlobStorage-sftp/.ssh/id_rsa'),
    }); 
};

我尝试在目标 Blob 存储上创建新文件,然后通过 Blob 触发器启动函数。结果日志如下:

2020-01-06T06:23:04  欢迎,您现在已连接到日志流服务。默认超时时间为2小时。使用App Setting SCM_LOGSTREAM_TIMEOUT(以秒为单位)更改超时时间。
2020-01-06T06:24:04  在过去的1分钟内没有新的跟踪。
2020-01-06T06:25:02.718 [信息] 正在执行 'Functions.verification-BlobStorage-sftp2' (原因='检测到新的 blob: verificationblobstorage/test0106.test',Id=0d5dda28-8913-4660-bcdd-82a6e78c8fec)
2020-01-06T06:25:10.534 [信息] JavaScript blob 触发器函数已处理 blob
 Blob: verificationblobstorage/test0106.test
 Blob Size: 23 字节
2020-01-06T06:25:10.553 [信息] InvocationContext {
  invocationId: '0d5dda28-8913-4660-bcdd-82a6e78c8fec',
  traceContext:
   { traceparent: '00-6a5c700bee92534599b540ce4fb6078f-e1e6436e28ea684d-00',
     tracestate: '',
     attributes: { OperationName: 'verification-BlobStorage-sftp2' } },
  executionContext:
   { invocationId: '0d5dda28-8913-4660-bcdd-82a6e78c8fec',
     functionName: 'verification-BlobStorage-sftp2',
     functionDirectory: 'D:\\home\\site\\wwwroot\\verification-BlobStorage-sftp2' },
  bindings:
   { myBlob:
      <Buffer 74 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74 31 31 31 31 31 0d 0a> },
  log:
   { [Function]
     error: [Function: error],
     warn: [Function: warn],
     info: [Function: info],
     verbose: [Function: verbose] },
  bindingData:
   { invocationId: '0d5dda28-8913-4660-bcdd-82a6e78c8fec',
     blobTrigger: '<myblobpath>',
     uri:
      'https://<myblobpath>',
     // 其余部分已省略...

不再显示更多日志。

根据日志显示,似乎可以获取到 Blob 文件。为什么无法将文件保存到 SFTP 服务器上呢?

英文:

I want to save file obtained by blob trigger to SFTP server with Azure Function.

module.exports = function (context, myBlob) {
    context.log(&quot;JavaScript blob trigger function processed blob \n Blob:&quot;, context.bindingData.blobTrigger, &quot;\n Blob Size:&quot;, myBlob.length, &quot;Bytes&quot;);
    var url = context.bindingData.uri;
    context.log(context);
    context.log(url);
    
    var Client = require(&#39;ssh2&#39;).Client;

    var myhost = new Client();
    myhost.on(&#39;ready&#39;,function(){

        //ready
        console.log(&#39;ready sftp...&#39;);

        //sftp
        myhost.sftp(function(err,sftp){

            //throw if error
            if(err) throw err;

            //file path
            var localfile = url;
            var remotefile = &quot;./sftp-test/testfile.txt&quot;;

            //upload
            sftp.fastPut(localfile,remotefile,{},function(err){
                if(err){
                    console.log(&quot;upload error.&quot;);
                }else{
                    console.log(&quot;uploaded.&quot;);
                    myhost.end();
                }
            });
        });

    //connection
    }).connect({
        host: &#39;&lt;sftp server ipaddress&gt;&#39;,
        port:22,
        username:&#39;sftpuser&#39;,
        privateKey:require(&#39;fs&#39;).readFileSync(&#39;D:/home/site/wwwroot/verification-BlobStorage-sftp/.ssh/id_rsa&#39;),
    }); 
};

I tried to create new file on target blob storage.
After that, start function by blob trigger.
The result log is:

2020-01-06T06:23:04  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds). 
2020-01-06T06:24:04  No new trace in the past 1 min(s).
2020-01-06T06:25:02.718 [Information] Executing &#39;Functions.verification-BlobStorage-sftp2&#39; (Reason=&#39;New blob detected: verificationblobstorage/test0106.test&#39;, Id=0d5dda28-8913-4660-bcdd-82a6e78c8fec)
2020-01-06T06:25:10.534 [Information] JavaScript blob trigger function processed blob 
 Blob: verificationblobstorage/test0106.test 
 Blob Size: 23 Bytes
2020-01-06T06:25:10.553 [Information] InvocationContext {
  invocationId: &#39;0d5dda28-8913-4660-bcdd-82a6e78c8fec&#39;,
  traceContext:
   { traceparent: &#39;00-6a5c700bee92534599b540ce4fb6078f-e1e6436e28ea684d-00&#39;,
     tracestate: &#39;&#39;,
     attributes: { OperationName: &#39;verification-BlobStorage-sftp2&#39; } },
  executionContext:
   { invocationId: &#39;0d5dda28-8913-4660-bcdd-82a6e78c8fec&#39;,
     functionName: &#39;verification-BlobStorage-sftp2&#39;,
     functionDirectory: &#39;D:\\home\\site\\wwwroot\\verification-BlobStorage-sftp2&#39; },
  bindings:
   { myBlob:
      &lt;Buffer 74 65 73 74 0d 0a 74 65 73 74 0d 0a 74 65 73 74 31 31 31 31 31 0d 0a&gt; },
  log:
   { [Function]
     error: [Function: error],
     warn: [Function: warn],
     info: [Function: info],
     verbose: [Function: verbose] },
  bindingData:
   { invocationId: &#39;0d5dda28-8913-4660-bcdd-82a6e78c8fec&#39;,
     blobTrigger: &#39;&lt;myblobpath&gt;&#39;,
     uri:
      &#39;https://&lt;myblobpath&gt;&#39;,
---Omitted---

No more logs were displayed.

It seems that I can get blob file when checked the log.
Why cannot save file on SFTP server?

答案1

得分: 0

根据我的测试,我们可以使用SDK ssh2-sftp-client将文件上传到SFTP服务器。有关该SDK的更多详细信息,请参考文档

例如:

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

module.exports = async function (context, myBlob) {
   // 获取 blob 名称
    var strs = context.bindingData.blobTrigger.split("/")
    
    var sftp  = new Client();
    var data = context.bindings.myBlob; // 以缓冲区形式获取 blob 内容
    var remote = "/home/jimtest/"+strs[1];
    context.log(remote);
    sftp.connect({
        host: '<服务器主机名或IP地址>',
        port: '22',
        username: '用户名',
        password: '密码'
      }).then(() => {
        return sftp.put(data, remote);
      })
      .then(() => {
        return sftp.end();
      })
      .catch(err => {
        context.log(err.message);
      });
     context.done()
};
英文:

According to my test, we can use the SDK ssh2-sftp-clientto upload file to SFTP server. For more details about the SDK, please refer to the document.

For example

const Client = require(&#39;ssh2-sftp-client&#39;);

module.exports = async function (context, myBlob) {
   // get blob name
    var strs =context.bindingData.blobTrigger.split(&quot;/&quot;)
    
    var sftp  = new Client();
    var data = context.bindings.myBlob; // get blob content as buffer
    var remote = &quot;/home/jimtest/&quot;+strs[1];
    context.log(remote);
    sftp.connect({
        host: &#39;&lt; string Hostname or IP of server&gt;&#39;,
        port: &#39;22&#39;,
        username: &#39; &#39;,
        password: &#39; &#39;
      }).then(() =&gt; {
        return sftp.put(data, remote);
      })
      .then(() =&gt; {
        return sftp.end();
      })
      .catch(err =&gt; {
        context.log(err.message);
      });
     context.done()
};

huangapple
  • 本文由 发表于 2020年1月6日 14:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定