Client Libraries Guide
✅ Fresh -- Updated March 2026 from official Google Cloud documentation.
Overview
Google provides idiomatic client libraries for all major programming languages. These libraries handle authentication, retries, pagination, and serialization automatically.
Supported Languages
| Language | Package Manager | Install Pattern |
|---|---|---|
| Python | pip | pip install google-cloud-SERVICE |
| Node.js | npm | npm install @google-cloud/SERVICE |
| Java | Maven/Gradle | com.google.cloud:google-cloud-SERVICE |
| Go | go modules | go get cloud.google.com/go/SERVICE |
| C# | NuGet | dotnet add package Google.Cloud.SERVICE.V1 |
| PHP | Composer | composer require google/cloud-SERVICE |
| Ruby | gem | gem install google-cloud-SERVICE |
Installation
Python
bash
# Install a specific service library
pip install google-cloud-storage
pip install google-cloud-bigquery
pip install google-cloud-pubsub
# Install with extras (e.g., pandas integration for BigQuery)
pip install google-cloud-bigquery[pandas]Node.js
bash
npm install @google-cloud/storage
npm install @google-cloud/bigquery
npm install @google-cloud/pubsubJava (Maven)
xml
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.30.0</version>
</dependency>Go
bash
go get cloud.google.com/go/storage
go get cloud.google.com/go/bigquery
go get cloud.google.com/go/pubsubAuthentication in Client Libraries
All client libraries use Application Default Credentials (ADC) automatically:
Python
python
from google.cloud import storage
# ADC is used automatically
client = storage.Client()
# Or specify a project explicitly
client = storage.Client(project='my-project')
# Or use a specific credentials file
from google.oauth2 import service_account
creds = service_account.Credentials.from_service_account_file('key.json')
client = storage.Client(credentials=creds, project='my-project')Node.js
javascript
const { Storage } = require('@google-cloud/storage');
// ADC is used automatically
const storage = new Storage();
// Or specify project and key file
const storage = new Storage({
projectId: 'my-project',
keyFilename: '/path/to/key.json'
});Go
go
import "cloud.google.com/go/storage"
// ADC is used automatically
client, err := storage.NewClient(ctx)
// Or specify credentials
client, err := storage.NewClient(ctx,
option.WithCredentialsFile("/path/to/key.json"))Common Operations
Cloud Storage
python
from google.cloud import storage
client = storage.Client()
# List buckets
for bucket in client.list_buckets():
print(bucket.name)
# Upload a file
bucket = client.bucket('my-bucket')
blob = bucket.blob('path/to/file.txt')
blob.upload_from_filename('/local/file.txt')
# Download a file
blob.download_to_filename('/local/download.txt')
# Delete a file
blob.delete()BigQuery
python
from google.cloud import bigquery
client = bigquery.Client()
# Run a query
query = "SELECT name, count FROM `my-project.my_dataset.my_table` LIMIT 10"
results = client.query(query)
for row in results:
print(f"{row.name}: {row.count}")Pub/Sub
python
from google.cloud import pubsub_v1
# Publish a message
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('my-project', 'my-topic')
future = publisher.publish(topic_path, b'Hello, World!')
print(f"Published message ID: {future.result()}")
# Subscribe to messages
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path('my-project', 'my-sub')
def callback(message):
print(f"Received: {message.data}")
message.ack()
streaming_pull = subscriber.subscribe(subscription_path, callback=callback)Automatic Retry
Client libraries implement automatic retry with exponential backoff for transient errors:
Customizing Retry Behavior
Python:
python
from google.api_core.retry import Retry
# Custom retry with different timeout
custom_retry = Retry(
initial=1.0, # Initial backoff in seconds
maximum=60.0, # Max backoff
multiplier=2.0, # Backoff multiplier
deadline=300.0, # Total deadline in seconds
)
client.get_bucket('my-bucket', retry=custom_retry)Node.js:
javascript
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({
retryOptions: {
autoRetry: true,
maxRetries: 5,
retryDelayMultiplier: 2,
totalTimeout: 300,
}
});Pagination
Client libraries handle pagination automatically:
python
from google.cloud import storage
client = storage.Client()
# Automatic iteration across all pages
for blob in client.list_blobs('my-bucket'):
print(blob.name)
# Manual pagination
blobs = client.list_blobs('my-bucket', max_results=100)
page = next(blobs.pages)
for blob in page:
print(blob.name)
print(f"Next page token: {blobs.next_page_token}")Error Handling
python
from google.api_core.exceptions import NotFound, PermissionDenied, TooManyRequests
try:
bucket = client.get_bucket('nonexistent-bucket')
except NotFound:
print("Bucket does not exist")
except PermissionDenied:
print("No permission to access bucket")
except TooManyRequests:
print("Rate limited -- slow down")
except Exception as e:
print(f"Unexpected error: {e}")Library Architecture
Best Practices
- Always use client libraries instead of raw HTTP calls
- Use ADC for authentication (no hardcoded keys)
- Handle specific exceptions (NotFound, PermissionDenied) not just generic Exception
- Close clients when done to release resources (
client.close()) - Use async clients for high-throughput applications
- Keep libraries updated for security patches and performance improvements
bash
# Check for updates
pip list --outdated | grep google-cloud
npm outdated | grep @google-cloudSee Also
- Authentication SOP -- Set up credentials
- HTTP Guidelines -- Protocol-level details
- Error Handling -- Error model and details
- Troubleshooting -- Common issues