创建和管理项目

Google Cloud 项目是执行下列操作的基础:创建、启用和使用所有 Google Cloud 服务,包括管理 API、启用结算功能、添加和移除协作者以及管理 Google Cloud 资源的权限。

本页面介绍了如何使用以下各项创建和管理 Google Cloud 项目: Cloud Resource Manager API 和 Google Cloud 控制台。

准备工作

如需了解项目资源,请参阅资源层次结构概览。如需有关设置资源层次结构的指导,请参阅 确定 Google Cloud 着陆区的资源层次结构

以下内容用于标识您的项目:

  • 项目名称:人类可读的项目名称。

    任何 Google API 都不会使用项目名称。您可以在项目创建期间或之后随时修改项目名称。项目名称不必具有唯一性。

  • 项目 ID:项目的全局唯一标识符。

    项目 ID 是一个唯一字符串,用于将您的项目与 GCP 中的所有其他项目区分开来。输入项目名称后, Google Cloud 控制台会生成一个唯一的项目 ID,此 ID 可以组合使用 由字母、数字和连字符组成我们建议您使用生成的项目 ID 但您可以在项目创建期间对其进行修改。创建项目后 项目 ID 是永久性的。

    项目 ID 具有以下要求:

    • 长度必须为 6 到 30 个字符。
    • 只能包含小写字母、数字和连字符。
    • 必须以字母开头。
    • 不得以连字符结尾。
    • 不得使用正在或以前使用过的项目 ID;这包括已删除的项目。
    • 不得包含受限字符串,例如 googlenullundefinedssl
  • 项目编号:系统为项目自动生成的唯一标识符。

请勿包含个人身份信息,例如个人身份信息 (PII) 或安全数据。 项目 ID 用在许多其他 Google Cloud 资源的名称中, 对项目或相关资源的引用会显示项目 ID 和资源名称。

创建项目

您必须拥有 resourcemanager.projects.create 权限才能创建项目。Project Creator 角色等角色中包含此权限 (roles/resourcemanager.projectCreator).已授予 Project Creator 角色 新组织资源的整个网域以及免费试用用户。 了解如何向个人授予该角色并限制组织资源 请参阅管理默认组织 角色页面。

如果您未指定父资源,则系统会选择父资源 (如果适用)。

您可以使用 Google Cloud 控制台、 Google Cloud CLI 或 projects.create() 方法。

控制台

如需创建新项目,请执行以下操作:

  1. 转到 Google Cloud 控制台中的管理资源页面。

    转到“管理资源”

    其余步骤会显示在 Google Cloud 控制台中。

  2. 在页面顶部的选择组织下拉列表中,选择要在其中创建项目的组织资源。如果您使用的是免费试用版,请跳过此步骤,因为系统不会显示此列表。
  3. 点击创建项目
  4. 在显示的新建项目窗口中,输入项目名称并选择适用的结算账号。项目名称只能包含字母、数字、英文单引号、连字符、空格或英文感叹号,且长度必须介于 4 到 30 个字符之间。
  5. 位置框中输入父级组织或文件夹资源。该资源将是新项目的分层父级。如果可以选择无组织,那么您也可以选择该选项,将新项目作为其自身资源层次结构的顶层进行创建。
  6. 输入完新项目的详细信息后,点击创建

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如需创建新项目,请使用 gcloud projects create 命令:

    gcloud projects create PROJECT_ID
    

    其中 PROJECT_ID 是您要创建的项目的 ID。项目 ID 必须以小写字母开头,只能包含 ASCII 字母、数字和连字符,并且长度必须介于 6 到 30 个字符之间。

    如需创建以组织资源或文件夹作为父级的项目,请使用 --organization--folder 标志。由于一个资源只能有一个父级,因此您只能使用其中一个标志:

    gcloud projects create PROJECT_ID --organization=ORGANIZATION_ID
    
    gcloud projects create PROJECT_ID --folder=FOLDER_ID
    

API

使用 projects.create() 方法创建新项目时,您不能在项目 ID 中使用某些特定字词,部分示例包括 googlenullundefinedssl。 如果您使用限制词,请求将返回 INVALID_ARGUMENT 错误。

以下请求仅创建项目,不会将项目与结算账号自动关联。您可以使用 projects.updateBillingInfo 方法设置或更新与项目关联的结算账号。

创建项目请求:

POST https://cloudresourcemanager.googleapis.com/v3/projects/
Authorization: *************
Content-Type: application/json

{
    "projectId": "our-project-123",
    "name": "my project",
    "labels": {
      "mylabel": "prod"
    }
}

创建项目响应:

{
    "name": "operations/pc.123456789",
}

获取操作请求:

GET https://cloudresourcemanager.googleapis.com/v3/operations/pc.123456789
Authorization: *************
Content-Type: application/json

获取操作响应:

{
    "name": "operations/pc.123456789",
    "done": true,
    "response": {
        "@type": "type.googleapis.com/google.cloudresourcemanager.v3.Project",
        "projectNumber": "464036093014",
        "projectId": "our-project-123",
        "lifecycleState": "ACTIVE",
        "name": "my project",
        "labels": {
        "mylabel": "prod"
        },
    "createTime": "2016-01-07T21:59:43.314Z"
    }
}

在创建项目期间添加标记

标记提供了一种为资源创建注解的方法。您可以在创建项目时添加标记。您 必须在添加代码时分配 Tag User 角色。如需详细了解系统分配给您的 此角色,请参阅管理资源上的标记。 您只能通过以下某种方式为标记键值对添加命名空间:

gcloud

如需在项目创建期间添加标记,请运行以下命令:

gcloud projects create PROJECT_ID --organization=ORGANIZATION_ID --tags=KEY_VALUE_PAIRS

替换以下内容:

  • PROJECT_ID 是项目的唯一标识符。
  • ORGANIZATION_ID 是组织的唯一标识符。
  • KEY_VALUE_PAIRS 是一系列以英文逗号分隔的键值对,您可以将其分配给 资源。例如,123/environment=production, 456/create=testresource 是以英文逗号分隔的键值对。

API

以下代码段是一个 JSON 请求,您可以在其中创建项目并 并为其添加标签

  POST https://cloudresourcemanager.googleapis.com/v3/projects/
  Authorization: *************
  Content-Type: application/json

  {
    "projectId": "our-project-456",
    "name": "my project",
    "parent": "organizations/123",
    "tags": {
      "key": "123/environment"
      "value": "production"
    },
"tags": {
      "key": "123/costCenter"
      "value": "marketing"
  }
}

使用服务账号创建项目

您可以使用服务账号自动创建项目。和用户账号一样 服务账号可以被授予在 组织资源。不允许服务账号在 组织资源,并且在创建项目时必须指定父级资源。 服务账号可以使用 gcloud CLI 或 projects.create() 方法结合使用。

管理项目配额

如果配额中的剩余项目少于 30 个,新建项目页面上将出现一条通知,显示配额中剩余的项目数量。达到 项目限制,如需创建更多项目,您必须申请项目限制 增加。或者,您也可以通过“管理资源”页面安排系统在 30 天后删除某些项目。 用户已软删除的项目会计入您的配额。这些项目将在 30 天后完全删除。

如需为配额中的项目申请额外容量,请使用申请增加项目配额表单。如需详细了解配额以及使用配额的原因,请参阅免费试用项目配额申请支持页面。如需详细了解结算报告,请参阅结算报告支持页面。

标识项目

如要与 Google Cloud 资源交互,您必须为每个请求提供标识性的项目信息。项目由项目 ID 和项目编号标识。

如需获取项目 ID 和项目编号,请执行以下操作:

  1. 转到 Google Cloud 控制台中的信息中心页面

    转到“信息中心”页面

  2. 点击页面顶部的从以下列表中选择:下拉列表。在随即显示的请选择:窗口中,选择您的项目。

项目信息中心的项目信息卡片会显示项目 ID 和项目编号:

在上例中,项目名称为 My Sample Project,项目 ID 为 my-sample-project-191923

获取现有项目

您可以使用 Google Cloud 控制台或 projects.get() 方法。

如果您不是项目所有者,则必须拥有 浏览器角色 (roles/browser)。

控制台

如需使用 Google Cloud 控制台查看项目,请执行以下操作:

  1. 前往 Google Cloud 控制台中的信息中心页面。

    转到“信息中心”页面

  2. 从页面顶部的项目选择器中选择您的项目。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如需获取项目的元数据,请使用 gcloud projects describe 命令:

    gcloud projects describe PROJECT_ID
    

    其中,PROJECT_ID 是您要查看的项目的 ID。

API

请求:

GET https://cloudresourcemanager.googleapis.com/v3/projects/our-project-123

响应:

{
    "projectNumber": "464036093014",
    "projectId": "our-project-123",
    "lifecycleState": "ACTIVE",
    "name": "my project",
    "labels": {
        "mylabel": "prod"
    },
    "createTime": "2016-01-07T21:59:43.314Z"
}

列出项目

列出资源下的所有项目

如需列出直接属于资源子项的所有项目,请使用 v3 projects.list 方法,并在查询中指定父资源:

请求:

GET https://cloudresourcemanager.googleapis.com/v3/projects

{
    "parent": "folders/662951040570"
}

响应:

{
    "projects": [
    {
        "name": "projects/951040570662",
        "parent": "folders/662951040570",
        "projectId": "tokyo-rain-123",
        "state": "ACTIVE",
        "displayName": "Tokyo Rain"
        "createTime": "2013-11-13T20:31:53.308Z"
        "updateTime": "2013-11-13T20:31:53.308Z"
        "etag": "BwWUlZ6XEfY="
    }
    ]
}

搜索项目

如需搜索与指定查询匹配的项目,请使用 gcloud alpha resource-manager projects search,并在 --query 标志中传递查询。搜索范围 是用户具有 projects.get 权限的所有项目。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如需获取所有项目的列表,请使用 gcloud alpha projects search 命令:

    gcloud alpha projects search --query="displayName=rek*"
    <table output showing the projects with display names starting from rek eg. rekey-project-2, rekha-project>
    
    gcloud alpha projects search --query="state:DELETE_REQUESTED"
    <table output showing the projects for which delete has been requested>
    

API

您可以使用 projects.search 方法和查询字符串返回与过滤器匹配的特定项目资源。结果仅包含您已被授予 resourcemanager.projects.get 权限的项目。

以下代码段会返回显示名为“Tokyo Rain”的项目资源:

请求:

GET https://cloudresourcemanager.googleapis.com/v3/projects:search?query=displayName%3ATokyo%2BRain

响应:

{
  "projects": [
    {
      "name": "projects/951040570662",
      "parent": "folders/662951040570",
      "projectId": "tokyo-rain-123",
      "state": "ACTIVE",
      "displayName": "Tokyo Rain",
      "createTime": "2013-11-13T20:31:53.308Z",
      "updateTime": "2013-11-13T20:31:53.308Z",
      "etag": "BwWUlZ6XEfY="
    }
  ]
}

以下代码段会返回带有红色标签的所有项目资源:

请求:

GET https://cloudresourcemanager.googleapis.com/v3/projects:search?query=labels.color%3Ared

响应:

{
  "projects": [
    {
      "name": "projects/951054970012",
      "parent": "folders/662951040570",
      "projectId": "Osaka-rain-234",
      "state": "ACTIVE",
      "displayName": "Osaka Rain",
      "createTime": "2013-11-13T20:31:53.308Z",
      "updateTime": "2013-11-13T20:31:53.308Z",
      "etag": "BwWUlZ6XEfY=",
      "labels": {
        "color": "red"
      }
    }
  ]
}

如果您在请求正文中指定了 parent.typeparent.id 字段,则系统会对父级执行 resourcemanager.projects.list 权限检查。如果用户具有此权限,则在应用其余过滤条件后,系统将返回父级下的所有项目。

如果用户没有此权限,则在应用其余过滤条件后,系统将返回用户具有 resourcemanager.projects.get 权限的所有项目。

如果未指定过滤器,则调用将返回用户具有 resourcemanager.projects.get 权限的项目。

更新项目

您可以使用 Google Cloud 控制台或 projects.patch() 方法。

您只能更新项目名称和标签字段。 如需详细了解如何更新项目,请参阅项目 API 参考页面

如需在资源层次结构中移动项目,请参阅移动项目。如需从一个项目迁移项目,请执行以下操作: 组织资源迁移到另一个组织资源,请参阅迁移项目

控制台

如需使用 Google Cloud 控制台更新项目的名称或标签,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往 IAM 并管理员设置页面。

    前往“IAM”和管理员设置

    其余步骤会显示在 Google Cloud 控制台中。

  2. 在屏幕顶部,点击项目选择下拉列表。
  3. 在出现的选择范围窗口中,点击组织下拉列表,然后选择您的组织。如果您是免费试用用户,请跳过此步骤,因为不会出现组织列表。
  4. 从显示的列表中选择您的项目。
  5. 如需更改项目名称,请修改项目名称,然后点击保存
  6. 要更改标签,请点击左侧导航栏中的标签

    详细了解 使用标签

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 要更新项目名称,请使用 gcloud alpha projects update 命令:

    gcloud alpha projects update PROJECT_ID \
        --name=NAME \
        --update-labels=KEY=VALUE, ...
    

    其中:

    • PROJECT_ID 是您要更新的项目的 ID。

    • NAME 是您要为该项目指定的新名称。

    • KEY=VALUE, ...”是您要更新的标签的键值对列表。如果标签已存在,则会修改其值。如果不存在,则会创建新标签。

    如需详细了解此命令可使用的其他标志,请参阅 Google Cloud CLI SDK

API

如需更新项目,请执行以下操作:

以下示例将项目的显示名称更新为 myproject 并将 color 标签设置为 red

请求:

PATCH https://cloudresourcemanager.googleapis.com/v3/projects/PROJECT_NUMBER?updateMask=displayName,labels

其中 PROJECT_NUMBER 是所需项目的数字 ID 以进行更新。

请求 JSON 正文:

{
  "displayName": "myproject",
  "labels": {
    "color": "red"
  }
}

响应:

{
  "projects": [
    {
      "name": "projects/951054970012",
      "parent": "folders/662951040570",
      "projectId": "Osaka-rain-234",
      "state": "ACTIVE",
      "displayName": "myproject",
      "createTime": "2013-11-13T20:31:53.308Z",
      "updateTime": "2013-11-13T20:35:42.308Z",
      "etag": "BwWUlZ6XEfY=",
      "labels": {
        "color": "red"
      }
    }
  ]
}

关停(删除)项目

您可以使用 Google Cloud 控制台或 projects.delete 方法。项目的生命周期状态必须为 ACTIVE 才能关闭 如此一来,

此方法会立即标记要删除的项目。系统会尽力向执行删除操作的用户以及重要联系人中列出的技术类别联系人发送电子邮件通知;如果通知未能发送,该项目仍会被标记为待删除。如果“技术”类别中没有联系人,则后备联系人不会收到通知。

您无法使用已标记为待删除的项目。如果该项目已与结算账号关联,那么这种关联会断开,并且在取消项目删除操作时不再恢复。30 天后,项目将被完全删除。在项目被完全删除之前,虽然不可用,但项目可能仍然可见。

如需在 30 天的期限内停止项目删除过程,请参阅恢复项目的步骤

30 天后,项目及其所有资源将被删除,无法恢复。项目在删除之前将计入您的项目配额。

为避免删除任何重要项目,您可以启用更改风险建议。更改风险 推荐会在您尝试删除 Google Cloud 已经启用的项目时, 重要性。

如果您为项目设置了结算功能,则在当前结算周期结束且系统成功向您的账号收取费用之前,可能无法完全删除该项目。 使用中服务的数量和类型也会影响系统永久删除项目的时间。如需详细了解数据保留和安全删除机制,请参阅 Google 如何保留收集的数据

您可以通过以下方式关停项目:

控制台

  1. 在 Google Cloud 控制台中,前往 IAM 并管理员设置页面。

    前往“IAM”和管理员设置

    其余步骤会显示在 Google Cloud 控制台中。

  2. IAM 和管理设置页面,选择要删除的项目,然后 点击关停

    只有安装了 resourcemanager.projects.delete 的用户才会看到关机 权限。

  3. 点击关停
  4. 输入项目 ID,然后点击仍然关停

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如需删除项目,请使用 gcloud projects delete 命令:

    gcloud projects delete PROJECT_ID
    

    其中,PROJECT_ID 是您要删除的项目的 ID。

API

以下代码段将删除指定项目:

请求:

DELETE https://cloudresourcemanager.googleapis.com/v3/projects/my-project-123

排查项目删除问题

如果关停项目的过程失败,您可以在排查项目删除问题中找到更多信息。

恢复项目

项目所有者可以在自项目关闭之日算起的 30 天恢复期内恢复已删除的项目。如果恢复项目,则会将其恢复到 关停前的状态,但以下情况除外:

  • 在以下情况下,该项目会停用结算功能: 并关闭项目 恢复的项目不会自动启用结算功能。通过 必须手动关联 Cloud Billing 账号 并在项目恢复后再次访问您可能需要等待几个小时 然后才能成功将最近恢复的项目与结算功能相关联 。
  • 如果您在 30 天内恢复项目,则可以恢复大部分资源 。部分服务在恢复方面存在延迟,您可能需要等待 才能恢复服务
  • 某些资源(如 Cloud Storage 或 Pub/Sub 资源)会较快删除。即使您已对它们进行了恢复, 在 30 天内恢复该项目。
  • 某些服务可能需要手动重启。如需了解详情,请参阅重启 Google Cloud 服务

您必须拥有项目的 resourcemanager.projects.undelete 权限 要恢复的数据如需恢复项目,请执行以下操作:

控制台

如需在 Google Cloud 控制台中查看项目,您需要满足以下条件 权限:

  • resourcemanager.projects.list
  • resourcemanager.folders.list
  • resourcemanager.projects.get

如需使用 Google Cloud 控制台恢复项目,请执行以下操作:

  1. 转到 Google Cloud 控制台中的管理资源页面。

    转到“管理资源”页面

  2. 在页面顶部的项目选择器中,选择您的 组织资源。

  3. 在组织资源、文件夹和项目列表下方,点击 待删除的资源

  4. 勾选要恢复的项目对应的复选框,然后点击恢复。 在随即显示的对话框中,确认您要恢复该项目。

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 如需恢复项目,请使用 gcloud projects undelete 命令:

    gcloud projects undelete PROJECT_ID
    

    其中 PROJECT_ID 是您要恢复的项目的 ID 或编号。

API

以下代码段将恢复指定项目:

请求:

POST https://cloudresourcemanager.googleapis.com/v3/projects/my-project-123:undelete