Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 2021年谷歌云平台如何防止过度计费_Google App Engine_Google Cloud Platform_Billing_Google Cloud Billing - Fatal编程技术网

Google app engine 2021年谷歌云平台如何防止过度计费

Google app engine 2021年谷歌云平台如何防止过度计费,google-app-engine,google-cloud-platform,billing,google-cloud-billing,Google App Engine,Google Cloud Platform,Billing,Google Cloud Billing,我在谷歌云平台上有多个项目和api,比如地图api、php应用程序引擎、sql等等 谷歌改变了账单的管理方式,现在账单很有可能以任何原因(bug、黑客等)暴涨 我如何才能做到在达到限额时禁用所有账单?不仅仅是电子邮件通知,这是不够的! 我不能仅仅关闭我的应用程序引擎实例,因为地图和其他地方的api凭据仍然会产生费用 文档。你有两种应对策略 第一个是设置一个基于wuota或限制每个用户的调用,因此,如果您的服务中有一个对特定API的调用量过大,您只需阻止该API,这样您的整个服务/项目就可以继续提

我在谷歌云平台上有多个项目和api,比如地图api、php应用程序引擎、sql等等

谷歌改变了账单的管理方式,现在账单很有可能以任何原因(bug、黑客等)暴涨

我如何才能做到在达到限额时禁用所有账单?不仅仅是电子邮件通知,这是不够的! 我不能仅仅关闭我的应用程序引擎实例,因为地图和其他地方的api凭据仍然会产生费用

文档。你有两种应对策略

第一个是设置一个基于wuota或限制每个用户的调用,因此,如果您的服务中有一个对特定API的调用量过大,您只需阻止该API,这样您的整个服务/项目就可以继续提供服务

另一种方法是为整个项目提供服务。这样做的风险更大,因为它会阻塞整个项目,并可能导致数据丢失

为此,您将部署一个云功能,就像您的预算设置为使用的发布/订阅主题触发的文档中的云功能一样:

const {google} = require('googleapis');
const {GoogleAuth} = require('google-auth-library');

const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
const PROJECT_NAME = `projects/${PROJECT_ID}`;
const billing = google.cloudbilling('v1').projects;

exports.stopBilling = async pubsubEvent => {
  const pubsubData = JSON.parse(
    Buffer.from(pubsubEvent.data, 'base64').toString()
  );
  if (pubsubData.costAmount <= pubsubData.budgetAmount) {
    return `No action necessary. (Current cost: ${pubsubData.costAmount})`;
  }

  if (!PROJECT_ID) {
    return 'No project specified';
  }

  _setAuthCredential();
  const billingEnabled = await _isBillingEnabled(PROJECT_NAME);
  if (billingEnabled) {
    return _disableBillingForProject(PROJECT_NAME);
  } else {
    return 'Billing already disabled';
  }
};

/**
 * @return {Promise} Credentials set globally
 */
const _setAuthCredential = () => {
  const client = new GoogleAuth({
    scopes: [
      'https://www.googleapis.com/auth/cloud-billing',
      'https://www.googleapis.com/auth/cloud-platform',
    ],
  });

  // Set credential globally for all requests
  google.options({
    auth: client,
  });
};

/**
 * Determine whether billing is enabled for a project
 * @param {string} projectName Name of project to check if billing is enabled
 * @return {bool} Whether project has billing enabled or not
 */
const _isBillingEnabled = async projectName => {
  try {
    const res = await billing.getBillingInfo({name: projectName});
    return res.data.billingEnabled;
  } catch (e) {
    console.log(
      'Unable to determine if billing is enabled on specified project, assuming billing is enabled'
    );
    return true;
  }
};

/**
 * Disable billing for a project by removing its billing account
 * @param {string} projectName Name of project disable billing on
 * @return {string} Text containing response from disabling billing
 */
const _disableBillingForProject = async projectName => {
  const res = await billing.updateBillingInfo({
    name: projectName,
    resource: {billingAccountName: ''}, // Disable billing
  });
  return `Billing disabled: ${JSON.stringify(res.data)}`;
};
然后您向该云功能的服务帐户授予计费管理员权限


如果你想使用这种方法,我建议你在任何一种情况下,如果可能的话,每个服务都有不同的项目,这样你就可以只关闭部分服务。

换句话说,没有一种简单的好方法可以防止像过去那样过多的计费……1)禁用项目计费会停止自动付款。账单可能仍然存在,例如服务未立即终止。2) 如果您禁用某个项目的计费功能,您的一些谷歌云资源可能会被删除并变得不可恢复。3) 不能禁用使用该API部署服务的API。4) 从技术上讲,你的答案是好的,但有有害的副作用。我同意约翰的观点,但现在已经没有(或者至少我不知道)一个好方法可以安全地停止对一个项目的收费了
{
 "name": "cloud-functions-billing",
 "version": "0.0.1",
 "dependencies": {
   "google-auth-library": "^2.0.0",
   "googleapis": "^52.0.0"
 }
}