【问题标题】:Send CSV file from Google bucket to SFTP server using GCP Cloud function NodeJS使用 GCP Cloud 功能 NodeJS 将 CSV 文件从 Google 存储桶发送到 SFTP 服务器
【发布时间】:2022-01-27 00:44:44
【问题描述】:

我正在尝试使用Google Cloud Functioncsv 文件发送到SFTP 服务器。 这意味着 -

Step 1 - 需要使用SFTP Server 创建一个Connection

Step 2- 从GCP Bucket 中选择csv File

Step 3 - Push 文件到SFTP Server 在某个位置

这是我正在使用的 nodejs 脚本 -

exports.helloWorld = (req, res) => {
  let Client = require('ssh2-sftp-client');
  let sftp = new Client();

sftp.connect({
  host: process.env.SFTP_SERVER,
  username: process.env.SFTP_USER,
  port: process.env.SFTP_PORT,
  password: process.env.SFTP_PASSWORD
}).then(() => {
  return sftp.list('/public');
}).then(data => {
  console.log(data, 'the data info');
}).catch(err => {
  console.log(err, 'catch error');
});
};

这是我的Package.json 文件

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "node-fetch": "^2.6.0",
    "@google-cloud/bigquery": "5.10.0",
    "@google-cloud/storage": "5.18.0",
    "ssh2-sftp-client": "7.2.1"
    
  }
}

现在一切正常,但它什么也没做。 谁能指出我在这里缺少什么脚本以及是否有任何documentation 来执行以下步骤??

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-functions google-cloud-storage


    【解决方案1】:

    因为 SFTP 代码都是异步的,所以您的函数可能在 .then() 运行之前返回,并且您的执行环境在它上传文件之前结束。您应该使您的 helloWorld 函数异步并等待并在返回之前返回响应:

    exports.helloWorld = async (req, res) => {
      // ...
      await sftp.connect({
        // ...
      });
      res.send(200);
      res.end();
    };
    

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多