gpt4 book ai didi

node.js - 访问 Cloud Functions 中的标签

转载 作者:搜寻专家 更新时间:2023-11-01 00:20:04 24 4
gpt4 key购买 nike

当相关资源是 Google Cloud Function 时,有没有办法访问资源的标签?

我使用了一个测试函数并记录了 process.envprocess 对象,但我看不到我针对该函数设置的任何标签。

用例:用于配置目的(而不是重新部署)。

谢谢!

最佳答案

您可以使用 googleapis: cloudfunctions Namespace , 使用环境变量你可以得到 API 需要的参数。这是 functions/get docs在以下脚本中使用:

const google = require('googleapis');
const cloudfunctions = google.cloudfunctions('v1');

const projectId = process.env.GCLOUD_PROJECT;
const regionId = process.env.FUNCTION_REGION;
const functionName = process.env.FUNCTION_NAME;

function getFunctionDetails(callback) {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
return callback(err);
}

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloudfunctions'
]);
}

cloudfunctions.projects.locations.functions.get({
name: `projects/${projectId}/locations/${regionId}/functions/${functionName}`,
auth: authClient
}, (err, result) => {
if (err) {
return callback(err);
}

callback(null, result.data)
});
});
}

exports.functionDetails = function (req, res) {
getFunctionDetails((err, data) => {
if (err) res.status(500).end();
// const labels = data.labels;
/* example `data`
{ name: 'projects/xxxx/locations/us-central1/functions/test', httpsTrigger: { url: 'https://us-central1-xxxx.cloudfunctions.net/test' }, status: 'ACTIVE', entryPoint: 'fnc', timeout: '60s', availableMemoryMb: 256, serviceAccountEmail: 'xxxx@appspot.gserviceaccount.com', updateTime: '2018-02-13T00:18:27Z', versionId: '9', labels: { 'deployment-tool': 'console-cloud', key1: '666' }, sourceUploadUrl: 'yyyy' }
*/
res.status(200).end();
});
};

关于node.js - 访问 Cloud Functions 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48734853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com