在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”之类的标准节点库吗?
我发现这篇文章可能会对你有所帮助,但基本上答案如下:
请注意,它将结果存储在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客户端
我希望有一种方法可以直接读取存储桶中的文件,而无需复制到(ram)磁盘
任何处理都必须在RAM的某个位置进行,如果您检查该工具的功能不包括处理,则Cloud Storage并不是执行计算的解决方案。