Google 云函数 - 读取使用 NodeJS 创建存储桶的新文件的内容
Google Cloud Function - read the CONTENT of a new file created a bucket using NodeJS
在 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'
这样的标准节点库吗
我发现这个 post 可能对你有帮助,但看起来基本上答案如下:
请注意,它会将结果存储在 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 Client
的文档
在 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'
这样的标准节点库吗我发现这个 post 可能对你有帮助,但看起来基本上答案如下:
请注意,它会将结果存储在 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 Client
的文档