# Imports the Google Cloud client libraryfromgoogle.cloudimportstorage# Instantiates a clientstorage_client=storage.Client()# The name for the new bucketbucket_name="my-new-bucket"# Creates the new bucketbucket=storage_client.create_bucket(bucket_name)print(f"Bucket {bucket.name} created.")
指定 Cloud Storage 存储分区
在 Cloud Storage 中执行任何操作之前,您需要提供存储分区名称。
# Imports the Google Cloud client libraryfromgoogle.cloudimportstorage# Instantiates a clientstorage_client=storage.Client()# The name for the new bucketbucket_name="my-new-bucket"# Creates the new bucketbucket=storage_client.create_bucket(bucket_name)print(f"Bucket {bucket.name} created.")
fromgoogle.cloudimportstoragedefwrite_read(bucket_name,blob_name):"""Write and read a blob from GCS using file-like IO"""# The ID of your GCS bucket# bucket_name = "your-bucket-name"# The ID of your new GCS object# blob_name = "storage-object-name"storage_client=storage.Client()bucket=storage_client.bucket(bucket_name)blob=bucket.blob(blob_name)# Mode can be specified as wb/rb for bytes mode.# See: https://docs.python.org/3/library/io.htmlwithblob.open("w")asf:f.write("Hello world")withblob.open("r")asf:print(f.read())
fromgoogle.cloudimportstoragedefwrite_read(bucket_name,blob_name):"""Write and read a blob from GCS using file-like IO"""# The ID of your GCS bucket# bucket_name = "your-bucket-name"# The ID of your new GCS object# blob_name = "storage-object-name"storage_client=storage.Client()bucket=storage_client.bucket(bucket_name)blob=bucket.blob(blob_name)# Mode can be specified as wb/rb for bytes mode.# See: https://docs.python.org/3/library/io.htmlwithblob.open("w")asf:f.write("Hello world")withblob.open("r")asf:print(f.read())
fromgoogle.cloudimportstoragedeflist_blobs(bucket_name):"""Lists all the blobs in the bucket."""# bucket_name = "your-bucket-name"storage_client=storage.Client()# Note: Client.list_blobs requires at least package version 1.17.0.blobs=storage_client.list_blobs(bucket_name)# Note: The call returns a response only when the iterator is consumed.forblobinblobs:print(blob.name)
fromgoogle.cloudimportstoragedefdelete_blob(bucket_name,blob_name):"""Deletes a blob from the bucket."""# bucket_name = "your-bucket-name"# blob_name = "your-object-name"storage_client=storage.Client()bucket=storage_client.bucket(bucket_name)blob=bucket.blob(blob_name)generation_match_precondition=None# Optional: set a generation-match precondition to avoid potential race conditions# and data corruptions. The request to delete is aborted if the object's# generation number does not match your precondition.blob.reload()# Fetch blob metadata to use in generation_match_precondition.generation_match_precondition=blob.generationblob.delete(if_generation_match=generation_match_precondition)print(f"Blob {blob_name} deleted.")
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-06。"],[[["This document guides you on how to store and retrieve data using the Cloud Storage client library, assuming you've already set up a Cloud Storage bucket and downloaded the necessary client libraries."],["The process starts by instantiating a storage client and creating a bucket, then to specify the correct bucket name to execute operations, you should utilize your project's default bucket."],["To write data, you use the `blob.open(\"w\")` method and to ensure the file is properly written, the `close()` function must be invoked, overwriting existing data if the file is reopened in write mode."],["To read a file, utilize `blob.open(\"r\")`, which is the default setting for reading; the path to the file is specified in `YOUR_BUCKET_NAME/PATH_IN_GCS` format."],["You can list the content of a bucket using the method `list_blobs`, and delete a file using `blob.delete`, optionally employing generation-match preconditions to prevent data corruption."]]],[]]