使用 Google Cloud Client Libraries 进行 Google Cloud Platform (GCP) 操作可以简化与 GCP 服务的交互。Google Cloud 提供了针对各种服务的官方 Python 客户端库,可以帮助你更轻松地进行云资源管理、数据存储、计算等操作。
以下是如何使用 Google Cloud Client Libraries 进行 GCP 操作的基本步骤:
1. 安装 Google Cloud Client Libraries
你可以使用 pip
安装 Google Cloud Client Libraries。以下是一些常见的库的安装命令:
-
Google Cloud Storage (GCS):
pip install google-cloud-storage
-
Google Cloud Compute Engine:
pip install google-cloud-compute
-
Google Cloud BigQuery:
pip install google-cloud-bigquery
-
Google Cloud Pub/Sub:
pip install google-cloud-pubsub
2. 配置认证
为了使用 Google Cloud Client Libraries,你需要设置 Google Cloud 认证。通常你会用到服务帐户密钥文件。你可以通过以下方式配置认证:
-
下载服务帐户密钥文件:在 Google Cloud Console 中创建服务帐户,并下载 JSON 格式的密钥文件。
-
设置环境变量:在你的终端或环境中设置
GOOGLE_APPLICATION_CREDENTIALS
环境变量,指向你的服务帐户密钥文件:export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-service-account-file.json"
3. 使用 Google Cloud Client Libraries
以下是一些常见服务的使用示例:
Google Cloud Storage (GCS)
-
列出所有 GCS 桶
from google.cloud import storage def list_buckets(): client = storage.Client() buckets = list(client.list_buckets()) for bucket in buckets: print(bucket.name) list_buckets()
-
上传文件到 GCS
from google.cloud import storage def upload_file(bucket_name, source_file_name, destination_blob_name): client = storage.Client() bucket = client.bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name) print(f"File {source_file_name} uploaded to {destination_blob_name}.") upload_file('my-bucket', 'local_file.txt', 'remote_file.txt')
-
下载文件从 GCS
from google.cloud import storage def download_file(bucket_name, blob_name, destination_file_name): client = storage.Client() bucket = client.bucket(bucket_name) blob = bucket.blob(blob_name) blob.download_to_filename(destination_file_name) print(f"Blob {blob_name} downloaded to {destination_file_name}.") download_file('my-bucket', 'remote_file.txt', 'local_file.txt')
Google Cloud Compute Engine
-
列出所有 VM 实例
from google.cloud import compute_v1 def list_instances(project_id, zone): instance_client = compute_v1.InstancesClient() request = compute_v1.ListInstancesRequest(project=project_id, zone=zone) response = instance_client.list(request=request) for instance in response: print(f"Instance name: {instance.name}") list_instances('your-project-id', 'us-central1-a')
-
启动一个 VM 实例
from google.cloud import compute_v1 def start_instance(project_id, zone, instance_name): instance_client = compute_v1.InstancesClient() request = compute_v1.StartInstanceRequest( project=project_id, zone=zone, instance=instance_name, ) operation = instance_client.start(request=request) print(f"Starting instance {instance_name}. Operation ID: {operation.id}") start_instance('your-project-id', 'us-central1-a', 'your-instance-name')
Google Cloud BigQuery
-
列出所有 BigQuery 数据集
from google.cloud import bigquery def list_datasets(project_id): client = bigquery.Client(project=project_id) datasets = list(client.list_datasets()) if datasets: print("Datasets:") for dataset in datasets: print(f"\t{dataset.dataset_id}") else: print("No datasets found.") list_datasets('your-project-id')
-
运行一个 BigQuery 查询
from google.cloud import bigquery def run_query(project_id): client = bigquery.Client(project=project_id) query = """ SELECT name, COUNT(*) as num FROM `bigquery-public-data.samples.shakespeare` GROUP BY name ORDER BY num DESC LIMIT 10 """ query_job = client.query(query) results = query_job.result() # Wait for the job to complete. for row in results: print(f"{row.name}: {row.num}") run_query('your-project-id')
4. 错误处理和调试
处理异常是很重要的,以下是一个简单的错误处理示例:
from google.cloud import storage
from google.api_core.exceptions import GoogleAPIError
def upload_file(bucket_name, source_file_name, destination_blob_name):
try:
client = storage.Client()
bucket = client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"File {source_file_name} uploaded to {destination_blob_name}.")
except GoogleAPIError as e:
print(f"An error occurred: {e}")
5. 查阅文档
Google Cloud 的 Python 客户端库文档提供了详细的信息和示例,你可以查阅 Google Cloud Python 客户端库文档 了解更多功能和用法。
如果你有具体的需求或遇到问题,随时告诉我!