Warm tip: This article is reproduced from serverfault.com, please click

node.js-Google Cloud功能

(node.js - Google Cloud Function)

发布于 2020-12-01 19:25:24

在GCP(不是firebase)中,我有一个存储桶和一个函数,当在存储桶中创建新文件时会调用该函数。效果很好。

/**
 * Triggered from a change to a Cloud Storage bucket.
 */
exports.mycompany_upload_file = (event, context) => {
    const gcsEvent = event;
    const filename = gcsEvent.name;
    const bucketName = event.bucket;
    console.log(`=====================================================`);
    console.log(`Event Type: ${context.eventType}`);
    console.log(`Bucket:     ${bucketName}`);
    console.log(`Datei:      ${filename}`);
    console.log(`=====================================================`);

    // now I would like to open that file and read it line-by-line 

我该如何处理该文件?该文件的路径是什么?我可以使用“ fs”之类的标准节点库吗?

Questioner
JFSebastian
Viewed
11
Lalo P 2020-12-02 05:38:50

我发现这篇文章可能会对你有所帮助,但基本上答案如下:

请注意,它将结果存储在ramdisk中,因此你将需要足够的RAM用于函数来下载文件。

var storage = require('@google-cloud/storage');
const gcs = storage({projectId: "<your_project>"});
const bucket = gcs.bucket("<your_bucket>");
const file = bucket.file("<path/to/your_file>")

exports.gcstest = (event, callback) => {
  file.download({destination:"/tmp/test"}, function(err, file) {
    if (err) {console.log(err)}
    else{callback();}
  })
};

有关更多信息,你可以查看Google Cloud Storage的文档:Node.js客户端