如何使用 gcloud 启用 API

How Can One Use gcloud To Enable APIs

我找不到使用 gcloud 命令行程序来更改项目的已启用 API 的方法。我的直觉是它会出现在账单中 "arena" 但我一直在努力寻找它并且运气很好。

编辑:这已被弃用。请参阅 gcloud services 答案。

检查 service-management 表面。有关更多帮助,请参阅 gcloud help service-management,有关启用新服务的帮助请参阅 gcloud help service-management enable。使用 gcloud service-management list 列出可用服务,以便您可以找到要启用的服务的名称。

问题 对于您当前的默认项目,请执行 gcloud service-management list --enabled 列出所有可用的 APIs 已启用。

$ gcloud service-management list --enabled
Listed 0 items.

如果您看到类似上面的内容,即 0 项,那么您很可能会在项目的某些命令中遇到以下错误。

ERROR: (gcloud.compute.machine-types.list) Some requests did not succeed:
 - Project {PROJECT_ID} is not found and cannot be used for API calls

解决方案 您需要做的是以下

  1. 列出可用于项目的 API 可以启用的

输出很长所以我建议你使用全局选项page-size 例如

$ gcloud service-management list  --available --page-size=10 --sort-by="NAME"
NAME                           TITLE
picker.googleapis.com          Google Picker API
bigquery-json.googleapis.com   BigQuery API
chromewebstore.googleapis.com  Chrome Web Store API
tracing.googleapis.com         Google Tracing API
youtube.googleapis.com         YouTube Data API v3
actions.googleapis.com         Google Actions API
dataflow.googleapis.com        Google Dataflow API
serviceuser.googleapis.com     Google Service User API
fusiontables.googleapis.com    Fusion Tables API
surveys.googleapis.com         Surveys API

NAME                                 TITLE
reseller.googleapis.com              Google Apps Reseller API
speech.googleapis.com                Google Cloud Speech API
appsmarket-component.googleapis.com  Google Apps Marketplace SDK
bigtabletableadmin.googleapis.com    Google Cloud Bigtable Table Admin API
container.googleapis.com             Google Container Engine API
vision.googleapis.com                Google Cloud Vision API
storage-api.googleapis.com           Google Cloud Storage JSON API
weavecompanion.googleapis.com        Weave Companion API
ml.googleapis.com                    Google Cloud Machine Learning Engine
firebaserules.googleapis.com         Firebase Rules API

...
  1. 最好还是查看您需要的具体可用 API 例如,检查我要启用的 Google Compute Engine API

    $ gcloud service-management list --available --filter='NAME:compute*' NAME TITLE compute-component.googleapis.com Google Compute Engine API

  2. 为项目启用计费。

    $ gcloud alpha billing accounts projects link amghouse-some-project-1 --account-id=XXFFXX-B9XX37-2D5DX --format=json { "billingAccountName": "billingAccounts/XXFFXX-B9XX37-2D5DX", "billingEnabled": true, "name": "projects/amghouse-some-project-1 /billingInfo", "projectId": "amghouse-some-project-1 " }

  3. 最终为您的项目启用api

`

$gcloud service-management enable compute-component.googleapis.com
Waiting for async operation operations/projectSettings.c6d11ddc-915f-4d66-9b98-237e473e7682 to complete...
Operation finished successfully. The following command can describe the Operation details:
 gcloud service-management operations describe operations/projectSettings.c6d11ddc-915f-4d66-9b98-237e473e7682

`

  1. 从好的方面来说,验证一下也没什么坏处

`

$ gcloud service-management operations describe operations/projectSettings.c6d11ddc-915f-4d66-9b98-237e473e7682 --format=json
        {
          "done": true,
          "metadata": {
            "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
            "persisted": true,
            "resourceNames": [
              "services/compute-component.googleapis.com/projectSettings/"
            ],
            "startTime": "2017-04-08 23:30:22 WAT"
          },
          "name": "operations/projectSettings.c6d11ddc-915f-4d66-9b98-237e473e7682",
          "response": {
            "@type": "type.googleapis.com/google.api.servicemanagement.v1.EnableServiceResponse"
          }
        }

`

注意 请注意,如果项目未链接到帐单信息,尝试启用 api 将失败并出现类似于

的错误
$ gcloud service-management enable compute-component.googleapis.com
ERROR: (gcloud.service-management.enable) FAILED_PRECONDITION: Operation does not satisfy the following requirements: billing-enabled {Billing must be enabled for activation of service '' in project 'amghouse-bct-sms-1' to proceed., https://console.developers.google.com/project/amghouse-bct-sms-1/settings}

启用容器引擎对我有用 API -

gcloud 服务启用 containerregistry.googleapis.com

使用

gcloud services enable <service name>

gcloud Documentation for this

示例:

gcloud services enable containerregistry.googleapis.com

2021年,改为

gcloud services list

详情为here

Select 您的应用程序将注册到的项目(请参阅有关 gcloud config set 的文档)

gcloud config set project myProject

要查看项目的可用服务列表,运行:

gcloud services list --available

在当前项目中启用服务(参见有关gcloud services enable的文档)

gcloud services enable my-consumed-service