我如何从云端功能调用其他Google API? [英] How do I call other Google APIs from a Cloud Function?
问题描述
我想从我的云端功能中调用其他Google API,例如,在收到来自Pubsub的消息后,将文件写入云端存储。我怎样才能做到这一点? google-cloud-node /rel =nofollow noreferrer>谷歌 - 云客户端库Node.js 来完成此操作。相同的库也可用于Java,Python和Ruby。
例如在Node JS中,您需要相应地编辑您的package.json文件:
{
依赖关系:{
google-cloud:*
},
...
}
然后,在您的代码中,您可以简单地调用相关库。以下示例仅列出项目中的存储桶:
var gcloud = require('google-cloud') ;
exports.helloworld = function(context,data){
var gcs = gcloud.storage({projectId:'< PROJECT>'});
gcs.getBuckets(function(err,bucket){
if(!err){
buckets.forEach(function(bucket){
console.log(bucket.name) ;
});
} else {
console.log('error:'+ err);
}
});
context.success();
}
您也不应该包含整个 google-云
npm模块,而是指定一个特定的子模块,例如 require('@ google-cloud / storage')
在上面的例子中。
I would like to call other Google APIs from my cloud function, for example, to write a file to Cloud Storage after receiving a message from Pubsub. How can I do this?
You can use the google-cloud client library for Node.js to accomplish this. The same library is also available for Java, Python and Ruby.
For example in Node JS, you'll want to edit your package.json file accordingly:
{
"dependencies": {
"google-cloud": "*"
},
...
}
Then, in your code, you can simply invoke the relevant library. The following example just lists the buckets in the project:
var gcloud = require('google-cloud');
exports.helloworld = function(context, data) {
var gcs = gcloud.storage({projectId: '<PROJECT>'});
gcs.getBuckets(function(err, buckets) {
if (!err) {
buckets.forEach(function(bucket) {
console.log(bucket.name);
});
} else {
console.log('error: ' + err);
}
});
context.success();
}
You also shouldn't include the entire google-cloud
npm module, but instead specify a specific sub-module, e.g. require('@google-cloud/storage')
in the above example.
这篇关于我如何从云端功能调用其他Google API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!