|
| 1 | +--- |
| 2 | +catalog_title: Google Cloud Storage |
| 3 | +catalog_description: Access and perform operations on Google Cloud Storage buckets and objects. |
| 4 | +catalog_icon: /integrations/assets/gcs.png |
| 5 | +catalog_tags: ["storage", "google"] |
| 6 | +--- |
| 7 | + |
| 8 | +# Google Cloud Storage (GCS) |
| 9 | + |
| 10 | +<div class="language-support-tag"> |
| 11 | + <span class="lst-supported">Supported in ADK</span><span class="lst-python">Python v2.3.0</span> |
| 12 | +</div> |
| 13 | + |
| 14 | +The `GCSToolset` and `GCSAdminToolset` allow ADK agents to interact with |
| 15 | +[Google Cloud Storage (GCS)](https://cloud.google.com/storage) to manage buckets |
| 16 | +and read/write objects. |
| 17 | + |
| 18 | +## Use cases |
| 19 | + |
| 20 | +- **Object Management**: Read, download, create, upload, list, metadata check, |
| 21 | + and delete GCS objects. |
| 22 | +- **Bucket Management**: List cloud storage buckets, create new buckets, change |
| 23 | + configurations, such as enabling versioning or uniform bucket-level access, and |
| 24 | + delete buckets. |
| 25 | +- **Data Integration**: Use cloud storage objects dynamically as part of the |
| 26 | + agent's workflow, such as processing files and ingestion. |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +- **Enable the Google Cloud Storage API** in the target Google Cloud project. |
| 31 | +- **IAM Permissions**: The authenticated principal (Application Default |
| 32 | + Credentials, service account, or user) must have the correct permissions, |
| 33 | + including `roles/storage.objectAdmin` and `roles/storage.admin`, to perform GCS |
| 34 | + bucket and object operations. |
| 35 | +- A Google Cloud Project ID configured. |
| 36 | + |
| 37 | +## Authentication |
| 38 | + |
| 39 | +The `GCSToolset` and `GCSAdminToolset` support several authentication mechanisms |
| 40 | +via `GCSCredentialsConfig`: |
| 41 | + |
| 42 | +### Application Default Credentials |
| 43 | + |
| 44 | +Recommended for local development and deployment to Google Cloud, including |
| 45 | +Agent Runtime, Cloud Run, and GKE. |
| 46 | + |
| 47 | +```python |
| 48 | +import google.auth |
| 49 | +from google.adk.integrations.gcs import GCSToolset |
| 50 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 51 | + |
| 52 | +# Load Application Default Credentials |
| 53 | +credentials, _ = google.auth.default() |
| 54 | + |
| 55 | +# Configure the toolset |
| 56 | +credentials_config = GCSCredentialsConfig(credentials=credentials) |
| 57 | +gcs_toolset = GCSToolset(credentials_config=credentials_config) |
| 58 | +``` |
| 59 | + |
| 60 | +### Service Account |
| 61 | + |
| 62 | +Allows providing credentials from a service account file. |
| 63 | + |
| 64 | +```python |
| 65 | +import google.auth |
| 66 | +from google.adk.integrations.gcs import GCSToolset |
| 67 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 68 | + |
| 69 | +# Load Service Account credentials |
| 70 | +credentials, _ = google.auth.load_credentials_from_file('path/to/key.json') |
| 71 | + |
| 72 | +# Configure the toolset |
| 73 | +credentials_config = GCSCredentialsConfig(credentials=credentials) |
| 74 | +gcs_toolset = GCSToolset(credentials_config=credentials_config) |
| 75 | +``` |
| 76 | + |
| 77 | +### External Access Token |
| 78 | + |
| 79 | +For acting on behalf of an end-user, such as via an OAuth2 flow or an external |
| 80 | +identity provider. |
| 81 | + |
| 82 | +```python |
| 83 | +from google.oauth2.credentials import Credentials |
| 84 | +from google.adk.integrations.gcs import GCSToolset |
| 85 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 86 | + |
| 87 | +# Assume 'user_token' is obtained via an external OAuth flow |
| 88 | +credentials = Credentials(token=user_token) |
| 89 | + |
| 90 | +# Configure the toolset |
| 91 | +credentials_config = GCSCredentialsConfig(credentials=credentials) |
| 92 | +gcs_toolset = GCSToolset(credentials_config=credentials_config) |
| 93 | +``` |
| 94 | + |
| 95 | +### External Auth Providers |
| 96 | + |
| 97 | +For platforms like Gemini Enterprise where the token is managed externally by |
| 98 | +the environment or platform. |
| 99 | + |
| 100 | +```python |
| 101 | +from google.adk.integrations.gcs import GCSToolset |
| 102 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 103 | + |
| 104 | +# The key used to look up the access token in the session state |
| 105 | +credentials_config = GCSCredentialsConfig( |
| 106 | + external_access_token_key="YOUR_AUTH_ID" |
| 107 | +) |
| 108 | +gcs_toolset = GCSToolset(credentials_config=credentials_config) |
| 109 | +``` |
| 110 | + |
| 111 | +### Interactive Auth (ADK Web) |
| 112 | + |
| 113 | +For interactive sessions using `adk web` interface to trigger an OAuth 2.0 |
| 114 | +login flow. |
| 115 | + |
| 116 | +```python |
| 117 | +from google.adk.integrations.gcs import GCSToolset |
| 118 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 119 | + |
| 120 | +# Provide OAuth 2.0 Client ID and Secret |
| 121 | +credentials_config = GCSCredentialsConfig( |
| 122 | + client_id="YOUR_CLIENT_ID", |
| 123 | + client_secret="YOUR_CLIENT_SECRET" |
| 124 | +) |
| 125 | +gcs_toolset = GCSToolset(credentials_config=credentials_config) |
| 126 | +``` |
| 127 | + |
| 128 | +## Use with agent |
| 129 | + |
| 130 | +The following example shows how to configure credentials and instantiate the |
| 131 | +storage toolset with write access enabled. |
| 132 | + |
| 133 | +```python |
| 134 | +import google.auth |
| 135 | +from google.adk.agents.llm_agent import LlmAgent |
| 136 | +from google.adk.integrations.gcs import GCSToolset |
| 137 | +from google.adk.integrations.gcs.settings import GCSToolSettings, Capabilities |
| 138 | +from google.adk.integrations.gcs.gcs_credentials import GCSCredentialsConfig |
| 139 | + |
| 140 | +# 1. Load Application Default Credentials (ADC) |
| 141 | +application_default_credentials, _ = google.auth.default() |
| 142 | + |
| 143 | +# 2. Configure credentials config |
| 144 | +credentials_config = GCSCredentialsConfig( |
| 145 | + credentials=application_default_credentials |
| 146 | +) |
| 147 | + |
| 148 | +# 3. Configure settings (allow read and write operations) |
| 149 | +tool_settings = GCSToolSettings(capabilities=[Capabilities.READ_WRITE]) |
| 150 | + |
| 151 | +# 4. Instantiate the GCS Toolset |
| 152 | +gcs_toolset = GCSToolset( |
| 153 | + credentials_config=credentials_config, |
| 154 | + gcs_tool_settings=tool_settings |
| 155 | +) |
| 156 | + |
| 157 | +# 5. Define an LLM Agent with the toolset |
| 158 | +agent = LlmAgent( |
| 159 | + model="gemini-2.5-flash", |
| 160 | + name="gcs_agent", |
| 161 | + description="Agent for interacting with GCS buckets and objects.", |
| 162 | + instruction=""" |
| 163 | + You are a storage assistant agent. Use the GCS tools to answer questions, |
| 164 | + list objects, upload files, or perform admin tasks as requested. |
| 165 | + """, |
| 166 | + tools=[gcs_toolset] |
| 167 | +) |
| 168 | +``` |
| 169 | + |
| 170 | +## Available tools |
| 171 | + |
| 172 | +The GCS integration split the capabilities into two main toolsets: |
| 173 | + |
| 174 | +### GCS Storage Tools (`GCSToolset`) |
| 175 | + |
| 176 | +Tool | Description |
| 177 | +---- | ----------- |
| 178 | +`gcs_get_bucket` | Get metadata information about a GCS bucket. |
| 179 | +`gcs_list_objects` | List object names in a GCS bucket. Supports optional prefix filtering and pagination. |
| 180 | +`gcs_get_object_metadata` | Get metadata properties of a specific GCS object (blob). |
| 181 | +`gcs_create_object` | Create a new object (blob) in a bucket from in-memory string data or a local file upload. |
| 182 | +`gcs_get_object_data` | Get content of a GCS object as a string, or download it directly to a local file. |
| 183 | +`gcs_delete_objects` | Delete multiple GCS objects (blobs) from a bucket. |
| 184 | + |
| 185 | +### GCS Admin Tools (`GCSAdminToolset`) |
| 186 | + |
| 187 | +Tool | Description |
| 188 | +---- | ----------- |
| 189 | +`gcs_list_buckets` | List GCS bucket names in a Google Cloud project. |
| 190 | +`gcs_create_bucket` | Create a new GCS bucket in a specific location. |
| 191 | +`gcs_update_bucket` | Update properties of a GCS bucket (e.g. versioning or uniform bucket-level access). |
| 192 | +`gcs_delete_bucket` | Delete a GCS bucket (bucket must be empty first). |
| 193 | + |
| 194 | +## Sample agents |
| 195 | + |
| 196 | +For complete, ready-to-run examples of GCS-powered agents with detailed |
| 197 | +authentication configurations, see: |
| 198 | + |
| 199 | +- [GCS Storage Sample Agent](https://github.com/google/adk-python/tree/main/contributing/samples/integrations/gcs) |
| 200 | +- [GCS Admin Sample Agent](https://github.com/google/adk-python/tree/main/contributing/samples/integrations/gcs_admin) |
| 201 | + |
| 202 | +## Resources |
| 203 | + |
| 204 | +- [Google Cloud Storage Documentation](https://cloud.google.com/storage/docs) |
| 205 | +- [GitHub Repository](https://github.com/google/adk-python) |
0 commit comments