云功能可以从云存储中读取吗?

时间:2018-03-09 19:39:49

标签: google-cloud-platform google-cloud-storage google-cloud-functions

我能找到的关于使用GCF + GCS的唯一文档是https://cloud.google.com/functions/docs/tutorials/storage。 AFAICT这只是展示了如何使用GCS事件来触发GCF。

GCF dependencies的文档中,它只提到了节点模块。 GCF代码是否可以从GCS存储桶读取?是否只需要一个知道如何与GCS通信的节点模块,如果有,是否有任何示例?

3 个答案:

答案 0 :(得分:5)

是的,但请注意,它会将结果存储在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();}
  })
};

答案 1 :(得分:2)

是的,您可以读写存储桶

const storage = require('@google-cloud/storage')();
const myBucket = storage.bucket('my-bucket');
const file = myBucket.file('my-file');

file.createReadStream().on('data', () => {
    // do something!
});

有关更多信息,请查看Google Cloud上的文档。

答案 2 :(得分:1)

Google云端功能只会执行您上传的代码。 如果您的代码包含连接到Google云存储的库,则您将能够连接到Google云存储,因为您将连接到任何其他API /服务。

有几种方法可以连接到谷歌云存储,如API,oauth或签名网址...所有这些方法都可用于谷歌云功能,所以我建议你看看谷歌云存储文档找到对你的案件最好的方法。