您最多可以检索 35 天内 Google Cloud 项目或组织中资源的创建、更新和删除记录。过去 35 天内未发生更改的素材资源会报告其最新状态。
准备工作
在您将运行 Cloud Asset Inventory 命令的项目中启用 Cloud Asset Inventory API。
确保您的账号具有调用 Cloud Asset Inventory API 的正确角色。如需了解每种通话类型的具体权限,请参阅权限。
限制
您只能在组织级和项目级检索资产历史记录。不支持文件夹。
获取资产历史记录
控制台
如需获取存储在 Google Cloud中的资源的历史记录,请完成以下步骤。
-
前往 Google Cloud 控制台中的资产库存页面。
- 切换到要搜索的项目、文件夹或组织。
- 点击资源标签页。
- 点击结果面板中的资源。
- 点击更改历史记录标签页。
- 为报告选择开始时间和结束时间。
- 使用选择要进行比较的记录列表框,显示所选日期的记录之间的差异。
执行搜索后,结果表中会列出与查询匹配的资源。双击您的查询即可对其进行修改,或使用过滤结果窗格按特定条件限制搜索结果。
如需以 Google Cloud CLI 命令的形式来查看查询,请点击查看查询。
如需导出结果,请点击下载 CSV。
gcloud
gcloud asset get-history \ --SCOPE \ --asset-names=ASSET_NAME_1,ASSET_NAME_2,... \ --content-type=CONTENT_TYPE \ --relationship-types=RELATIONSHIP_TYPE_1,RELATIONSHIP_TYPE_2,... \ --start-time="START_TIME" \ --end-time="END_TIME"
请提供以下值:
-
SCOPE
:请使用以下某个值:-
project=PROJECT_ID
,其中PROJECT_ID
是包含要检索的资源的项目的 ID。 -
organization=ORGANIZATION_ID
,其中ORGANIZATION_ID
是包含您要检索的资源的组织的 ID。如何查找 Google Cloud 组织的 ID
Google Cloud 控制台
如需查找 Google Cloud 组织的 ID,请完成以下步骤:
-
转到 Google Cloud 控制台。
- 点击菜单栏中的切换器列表框。
- 从列表框中选择您的组织。
- 点击全部标签页。组织 ID 显示在组织名称旁边。
gcloud CLI
您可以使用以下命令检索组织的 ID: Google Cloud
gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
-
-
ASSET_NAME_#
:资产全名的逗号分隔列表。CONTENT_TYPE
:您要检索的元数据的 内容类型。如果未指定--content-type
,则只会返回基本信息,例如素材资源名称、上次更新素材资源的时间,以及素材资源所属的项目、文件夹和组织。-
RELATIONSHIP_TYPE_#
:可选。需要有权访问 Security Command Center 高级或企业版,或 Gemini Cloud Assist。您要检索的资产关系类型的逗号分隔列表。您必须将CONTENT_TYPE
设置为RELATIONSHIP
,此功能才能正常运行。 -
START_TIME
:时间范围的开始时间,采用 gcloud topic 日期时间格式。该值不得超过过去 35 天。 -
END_TIME
:可选。时间范围的结束点,采用 gcloud topic datetime 格式。该值不得超过过去 35 天。如果未指定--end-time
,则将其设置为当前时间。
如需了解所有选项,请参阅 gcloud CLI 参考文档。
示例
运行以下命令可获取 my-project
项目中 my-instance
Compute Engine 实例在 2024 年 1 月 30 日至 2024 年 2 月 5 日之间的 resource
历史记录元数据。该实例具有以下属性:
- 实例名称:
my-instance
- 项目:
my-project
- 位置:
us-central1-a
gcloud asset get-history \ --project=my-project \ --asset-names=//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance \ --content-type=resource \ --start-time="2024-01-30" \ --end-time="2024-02-05"
示例响应
--- asset: ancestors: - projects/000000000000 - folders/000000000000 - organizations/000000000000 assetType: compute.googleapis.com/Instance name: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance resource: data: LATEST_ASSET_METADATA discoveryDocumentUri: https://www.googleapis.com/discovery/v1/apis/compute/v1/rest discoveryName: Instance location: us-central1-a parent: //cloudresourcemanager.googleapis.com/projects/000000000000 version: v1 updateTime: '2024-02-05T16:00:25.259186Z' window: endTime: '2024-02-05T16:00:25.259186Z' startTime: '2024-02-03T16:00:28.854779Z' --- asset: ancestors: - projects/000000000000 - folders/000000000000 - organizations/000000000000 assetType: compute.googleapis.com/Instance name: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance resource: data: EARLIEST_ASSET_METADATA discoveryDocumentUri: https://www.googleapis.com/discovery/v1/apis/compute/v1/rest discoveryName: Instance location: us-central1-a parent: //cloudresourcemanager.googleapis.com/projects/000000000000 version: v1 updateTime: '2024-02-03T16:00:28.854779Z' window: endTime: '2024-02-03T16:00:28.854779Z' startTime: '2024-01-30T08:00:22.930462Z'
REST
HTTP 方法和网址:
POST https://cloudasset.googleapis.com/v1/SCOPE_PATH:batchGetAssetsHistory
请求 JSON 正文:
{ "assetNames": [ "ASSET_NAME_1", "ASSET_NAME_2", "..." ], "contentType": "CONTENT_TYPE", "relationshipTypes": [ "RELATIONSHIP_TYPE_1", "RELATIONSHIP_TYPE_2", "..." ], "readTimeWindow": { "startTime": "START_TIME", "endTime": "END_TIME" } }
请提供以下值:
-
SCOPE_PATH
:请使用以下某个值:允许使用的值包括:
-
projects/PROJECT_ID
,其中PROJECT_ID
是包含要检索的资源的项目的 ID。 -
projects/PROJECT_NUMBER
,其中PROJECT_NUMBER
是包含您要检索的资产的项目的编号。如何查找 Google Cloud 项目编号
Google Cloud 控制台
如需查找 Google Cloud 项目编号,请完成以下步骤:
gcloud CLI
您可以使用以下命令检索 Google Cloud 项目编号:
gcloud projects describe PROJECT_ID --format="value(projectNumber)"
-
organizations/ORGANIZATION_ID
,其中ORGANIZATION_ID
是包含您要检索的资源的组织的 ID。如何查找 Google Cloud 组织的 ID
Google Cloud 控制台
如需查找 Google Cloud 组织的 ID,请完成以下步骤:
-
转到 Google Cloud 控制台。
- 点击菜单栏中的切换器列表框。
- 从列表框中选择您的组织。
- 点击全部标签页。组织 ID 显示在组织名称旁边。
gcloud CLI
您可以使用以下命令检索组织的 ID: Google Cloud
gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
-
-
ASSET_NAME_#
:资产全名的数组。CONTENT_TYPE
:您要检索的元数据的 内容类型。如果未指定contentType
,则只会返回基本信息,例如素材资源名称、上次更新素材资源的时间,以及素材资源所属的项目、文件夹和组织。-
RELATIONSHIP_TYPE_#
:可选。需要有权访问 Security Command Center 高级或企业版,或 Gemini Cloud Assist。您要检索的资产关系类型的逗号分隔列表。您必须将CONTENT_TYPE
设置为RELATIONSHIP
,此功能才能正常运行。 -
START_TIME
:时间范围的开始时间,采用 RFC 3339 格式。该值不得超过过去 35 天。 -
END_TIME
:可选。时间范围的结束点,采用 RFC 3339 格式。该值不得超过过去 35 天。如果未指定endTime
,则将其设置为当前时间。
如需了解所有选项,请参阅 REST 参考文档。
命令示例
运行以下任一命令,即可获取 my-project
项目中 my-instance
Compute Engine 实例在 2024 年 1 月 30 日至 2024 年 2 月 5 日之间的 resource
历史记录。该实例具有以下属性:
- 实例名称:
my-instance
- 项目:
my-project
- 位置:
us-central1-a
curl(Linux、macOS 或 Cloud Shell)
curl -X POST \ -H "X-HTTP-Method-Override: GET" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "assetNames": ["//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance"], "contentType": "RESOURCE", "readTimeWindow": { "startTime": "2024-01-30T00:00:00Z", "endTime": "2024-02-05T00:00:00Z" } }' \ https://cloudasset.googleapis.com/v1/projects/my-project:batchGetAssetsHistory
PowerShell (Windows)
$cred = gcloud auth print-access-token $headers = @{ "X-HTTP-Method-Override" = "GET"; "Authorization" = "Bearer $cred" } $body = @" { "assetNames": ["//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance"], "contentType": "RESOURCE", "readTimeWindow": { "startTime": "2024-01-30T00:00:00Z", "endTime": "2024-02-05T00:00:00Z" } } "@ Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Body $body ` -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:batchGetAssetsHistory" | Select-Object -Expand Content
示例响应
{ "assets": [ { "window": { "startTime": "2024-02-03T16:00:28.854779Z", "endTime": "2024-02-05T16:00:25.259186Z" }, "asset": { "name": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance", "assetType": "compute.googleapis.com/Instance", "resource": { "version": "v1", "discoveryDocumentUri": "https://www.googleapis.com/discovery/v1/apis/compute/v1/rest", "discoveryName": "Instance", "parent": "//cloudresourcemanager.googleapis.com/projects/000000000000", "data": { LATEST_ASSET_METADATA }, "location": "us-central1-a" }, "ancestors": [ "projects/000000000000", "folders/000000000000", "organizations/000000000000" ], "updateTime": "2024-02-05T16:00:25.259186Z" } }, { "window": { "startTime": "2024-01-30T08:00:22.930462Z", "endTime": "2024-02-03T16:00:28.854779Z" }, "asset": { "name": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance", "assetType": "compute.googleapis.com/Instance", "resource": { "version": "v1", "discoveryDocumentUri": "https://www.googleapis.com/discovery/v1/apis/compute/v1/rest", "discoveryName": "Instance", "parent": "//cloudresourcemanager.googleapis.com/projects/000000000000", "data": { EARLIEST_ASSET_METADATA }, "location": "us-central1-a" }, "ancestors": [ "projects/000000000000", "folders/000000000000", "organizations/000000000000" ], "updateTime": "2024-02-03T16:00:28.854779Z" } } ] }
C#
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Go
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
PHP
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Ruby
如需了解如何安装和使用 Cloud Asset Inventory 客户端库,请参阅 Cloud Asset Inventory 客户端库。
如需向 Cloud Asset Inventory 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。