Azure functions 如何使用Azure Functon将blob触发器获取的文件保存到SFTP服务器

Azure functions 如何使用Azure Functon将blob触发器获取的文件保存到SFTP服务器,azure-functions,azure-blob-storage,ssh2,Azure Functions,Azure Blob Storage,Ssh2,我想用Azure函数将blob触发器获取的文件保存到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 = conte

我想用Azure函数将blob触发器获取的文件保存到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 server ipaddress>',
        port:22,
        username:'sftpuser',
        privateKey:require('fs').readFileSync('D:/home/site/wwwroot/verification-BlobStorage-sftp/.ssh/id_rsa'),
    }); 
};
module.exports=函数(上下文,myBlob){
log(“JavaScript blob触发器函数处理的blob\n blob:,context.bindingData.blobTrigger,”\n blob大小:,myBlob.length,“字节”);
var url=context.bindingData.uri;
log(context);
日志(url);
var Client=require('ssh2')。Client;
var myhost=new Client();
myhost.on('ready',function(){
//准备好了吗
log('ready sftp…');
//sftp
myhost.sftp(函数(err,sftp){
//如果出错,抛出
如果(错误)抛出错误;
//文件路径
var localfile=url;
var remotefile=“./sftp test/testfile.txt”;
//上传
fastp(localfile,remotefile,{},函数(err){
如果(错误){
log(“上传错误”);
}否则{
console.log(“上传”);
myhost.end();
}
});
});
//联系
}).连接({
主机:“”,
港口:22,
用户名:'sftpuser',
privateKey:require('fs').readFileSync('D:/home/site/wwwroot/verification BlobStorage sftp/.ssh/id_rsa'),
}); 
};
我试图在目标blob存储上创建新文件。
之后,通过blob触发器启动函数。 结果日志为:

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 'Functions.verification-BlobStorage-sftp2' (Reason='New blob detected: verificationblobstorage/test0106.test', 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: '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>',
---Omitted---
2020-01-06T06:23:04欢迎光临,您现在已连接到日志流服务。默认超时为2小时。使用应用程序设置SCM_LOGSTREAM_timeout(以秒为单位)更改超时。
2020-01-06T06:24:04过去1分钟内没有新的痕迹。
2020-01-06T06:25:02.718[信息]执行“函数.验证-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大小:23字节
2020-01-06T06:25:10.553[信息]上下文{
调用ID:'0d5dda28-8913-4660-bcdd-82a6e78c8fec',
traceContext:
{traceparent:'00-6a5c700bee92534599b540ce4fb6078f-e1e6436e28ea684d-00',
tracestate:“”,
属性:{OperationName:'verification-BlobStorage-sftp2'},
执行上下文:
{调用ID:'0d5dda28-8913-4660-bcdd-82a6e78c8fec',
functionName:'verification-BlobStorage-sftp2',
functionDirectory:'D:\\home\\site\\wwwroot\\verification-BlobStorage-sftp2'},
绑定:
{myBlob:
},
日志:
{[功能]
错误:[函数:错误],
警告:[功能:警告],
信息:[功能:信息],
verbose:[函数:verbose]},
绑定数据:
{调用ID:'0d5dda28-8913-4660-bcdd-82a6e78c8fec',
blobTrigger:“”,
uri:
“https://”,
---省略---
没有显示更多日志

查看日志时,我似乎可以获取blob文件。

为什么无法在SFTP服务器上保存文件?

根据我的测试,我们可以使用SDK
ssh2 SFTP客户端
将文件上载到SFTP服务器。有关SDK的更多详细信息,请参阅

比如说

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

module.exports = async function (context, myBlob) {
   // get blob name
    var strs =context.bindingData.blobTrigger.split("/")

    var sftp  = new Client();
    var data = context.bindings.myBlob; // get blob content as buffer
    var remote = "/home/jimtest/"+strs[1];
    context.log(remote);
    sftp.connect({
        host: '< string Hostname or IP of server>',
        port: '22',
        username: ' ',
        password: ' '
      }).then(() => {
        return sftp.put(data, remote);
      })
      .then(() => {
        return sftp.end();
      })
      .catch(err => {
        context.log(err.message);
      });
     context.done()
};
const Client=require('ssh2-sftp-Client');
module.exports=异步函数(上下文,myBlob){
//获取blob名称
var strs=context.bindingData.blobTrigger.split(“/”)
var sftp=新客户端();
var data=context.bindings.myBlob;//获取blob内容作为缓冲区
var remote=“/home/jimtest/”+strs[1];
context.log(远程);
连接({
主机:“”,
端口:'22',
用户名:“”,
密码:“”
}).然后(()=>{
返回sftp.put(数据,远程);
})
.然后(()=>{
返回sftp.end();
})
.catch(错误=>{
context.log(错误消息);
});
context.done()
};

如果您想将文件上传到sftp服务器,您可以使用sdk
ssh2 sftp客户端
。有关详细信息,请参阅@:该文件已在sftp服务器上成功创建。非常感谢!@RyoSasaki,因为它对您很有用,可以吗?它可能会帮助更多有类似问题的人。