Google Cloud Storage (GCS)¶
在 ADK 中得到支持。Python v2.3.0
The GCSToolset 和 GCSAdminToolset 允许 ADK 代理进行交互(即允许 ADK 代理与其他系统或组件进行通信或数据交换)。
Google Cloud Storage (GCS) 用于管理存储桶(buckets),以及读写其中的对象(objects)。
使用场景¶
- 对象管理(Object Management)可以读取、下载、创建、上传、列出 GCS 对象,检查其元数据,以及删除这些对象。
- 桶管理(Bucket Management):列出云存储桶,创建新的存储桶,修改配置(例如启用版本控制或统一的全局访问权限),以及删除存储桶。
- 数据集成:在代理的工作流程中动态使用云存储对象,例如处理文件和数据导入等操作。
先决条件¶
- 启用 Google Cloud Storage API 在目标 Google Cloud 项目中。
- IAM 权限经过身份验证的主体(可以是应用程序默认的凭据、服务账户或用户)必须具有正确的权限,包括……
roles/storage.admin为了执行 GCS(Google Cloud Storage)中的桶(bucket)和对象(object)相关操作。 - 已配置一个 Google Cloud 项目 ID。
身份验证(Authentication)¶
The GCSToolset 和 GCSAdminToolset 支持多种身份验证机制;通过……实现 GCSCredentialsConfig:
应用程序默认凭据(Application Default Credentials)¶
推荐用于本地开发以及将应用程序部署到 Google Cloud 上,包括使用 Agent Runtime、Cloud Run 和 GKE 等服务。
import google.auth
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# Load Application Default Credentials
credentials, _ = google.auth.default()
# Configure the toolset
credentials_config = GCSCredentialsConfig(credentials=credentials)
gcs_toolset = GCSToolset(credentials_config=credentials_config)
服务账户(Service Account)¶
允许从服务账户文件中获取凭据(即用户名和密码)。
import google.auth
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# Load Service Account credentials
credentials, _ = google.auth.load_credentials_from_file('path/to/key.json')
# Configure the toolset
credentials_config = GCSCredentialsConfig(credentials=credentials)
gcs_toolset = GCSToolset(credentials_config=credentials_config)
外部访问令牌(External Access Token)¶
用于代表最终用户执行操作,例如通过 OAuth2 流程或外部身份提供商来实现。
from google.oauth2.credentials import Credentials
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# Assume 'user_token' is obtained via an external OAuth flow
credentials = Credentials(token=user_token)
# Configure the toolset
credentials_config = GCSCredentialsConfig(credentials=credentials)
gcs_toolset = GCSToolset(credentials_config=credentials_config)
外部身份验证提供商(External Auth Providers)¶
对于像 Gemini Enterprise 这样的平台,令牌是由外部环境或平台来管理的。
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# The key used to look up the access token in the session state
credentials_config = GCSCredentialsConfig(
external_access_token_key="YOUR_AUTH_ID"
)
gcs_toolset = GCSToolset(credentials_config=credentials_config)
交互式身份验证(ADK Web)¶
用于需要交互式的会话(即用户需要与系统进行实时互动的会话)。 adk web 用于触发 OAuth 2.0 登录流程的接口。
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# Provide OAuth 2.0 Client ID and Secret
credentials_config = GCSCredentialsConfig(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET"
)
gcs_toolset = GCSToolset(credentials_config=credentials_config)
与代理程序(agent)一起使用。¶
以下示例展示了如何配置凭据,并实例化具有写入访问权限的存储工具集。
import google.auth
from google.adk.agents.llm_agent import LlmAgent
from google.adk.integrations.gcs import GCSToolset
from google.adk.integrations.gcs.settings import GCSToolSettings, Capabilities
from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig
# 1. Load Application Default Credentials (ADC)
application_default_credentials, _ = google.auth.default()
# 2. Configure credentials config
credentials_config = GCSCredentialsConfig(
credentials=application_default_credentials
)
# 3. Configure settings (allow read and write operations)
tool_settings = GCSToolSettings(capabilities=[Capabilities.READ_WRITE])
# 4. Instantiate the GCS Toolset
gcs_toolset = GCSToolset(
credentials_config=credentials_config,
gcs_tool_settings=tool_settings
)
# 5. Define an LLM Agent with the toolset
agent = LlmAgent(
model="gemini-2.5-flash",
name="gcs_agent",
description="Agent for interacting with GCS buckets and objects.",
instruction="""
You are a storage assistant agent. Use the GCS tools to answer questions,
list objects, upload files, or perform admin tasks as requested.
""",
tools=[gcs_toolset]
)
可用的工具¶
GCS(Google Cloud Storage)的集成将相关功能分为两个主要的工具集:
GCS存储工具(GCSToolset)¶
| 工具 | 描述 |
|---|---|
gcs_list_objects |
列出 GCS 存储桶中的对象名称。支持可选的前缀过滤和分页功能。 |
gcs_get_object_metadata |
获取特定 GCS 对象(即数据块)的元数据属性。 |
gcs_create_object |
可以从内存中的字符串数据或本地文件上传,在存储桶中创建一个新的对象(即“blob”)。此操作需要相应的权限或配置。 Capabilities.READ_WRITE. |
gcs_get_object_data |
将 GCS 对象的内容作为字符串获取,或者直接将其下载到本地文件中。 |
gcs_delete_objects |
从某个桶中删除多个 GCS 对象(即数据块)。需要相应的权限或工具才能执行此操作。 Capabilities.READ_WRITE. |
GCS管理工具(GCS Admin Tools)GCSAdminToolset)¶
| 工具 | 描述 |
|---|---|
gcs_list_buckets |
列出 Google Cloud 项目中所有的 GCS(Google Cloud Storage)存储桶名称。 |
gcs_get_bucket |
获取关于 GCS 存储桶的元数据信息。 |
gcs_create_bucket |
在特定位置创建一个新的 GCS 存储桶。需要……(此处应补充具体操作或要求)。 Capabilities.READ_WRITE. |
gcs_update_bucket |
更新 GCS 存储桶的属性,例如版本控制或统一的桶级访问权限。需要相应的权限才能执行这些操作。 Capabilities.READ_WRITE. |
gcs_delete_bucket |
删除一个 GCS 存储桶(该存储桶必须先为空)。需要执行以下操作: Capabilities.READ_WRITE. |
注意:
这里列出的工具名称是那些会被模型使用的工具名称(这些名称前面都加上了特定的前缀)。 gcs_在使用时…… tool_filter请参考那些没有前缀的名称(例如……)。 get_bucket.
示例代理程序(Sample agents)¶
有关使用 GCS(Google Cloud Storage)驱动的代理程序的完整、可立即运行的示例(其中包含详细的身份验证配置),请参阅: