讀取及寫入 Cloud Storage

本文件說明如何使用 Cloud Storage 用戶端程式庫儲存及擷取資料。本文假設您已依設定 Cloud Storage 一文的指示啟用 Cloud Storage 值區及下載用戶端程式庫,同時假設您知道如何建構 App Engine 應用程式。

如需其他程式碼範例,請參閱 Cloud Storage 用戶端程式庫

必要匯入項目

使用下列程式碼片段,透過用戶端程式庫存取 Cloud Storage:

# Imports the Google Cloud client library
from google.cloud import storage

# Instantiates a client
storage_client = storage.Client()

# The name for the new bucket
bucket_name = "my-new-bucket"

# Creates the new bucket
bucket = storage_client.create_bucket(bucket_name)

print(f"Bucket {bucket.name} created.")

指定 Cloud Storage 值區

在 Cloud Storage 中執行任何作業前,您需要先提供值區名稱。

# Imports the Google Cloud client library
from google.cloud import storage

# Instantiates a client
storage_client = storage.Client()

# The name for the new bucket
bucket_name = "my-new-bucket"

# Creates the new bucket
bucket = storage_client.create_bucket(bucket_name)

print(f"Bucket {bucket.name} created.")

如要指定值區名稱,最簡單的方法就是使用專案的預設值區。您必須先為專案建立預設值區,對 get_default_gcs_bucket_name 發出的呼叫才會成功。

寫入 Cloud Storage

以下是如何寫入值區的範例:

from google.cloud import storage


def write_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.html
    with blob.open("w") as f:
        f.write("Hello world")

    with blob.open("r") as f:
        print(f.read())

注意事項:

  • 在呼叫開啟檔案進行寫入時,程式碼範例會指定特定 Cloud Storage 標頭,針對檔案寫入自訂中繼資料;這些中繼資料可使用 cloudstorage.stat() 擷取。您可以在 cloudstorage.open() 參考資料中找到受支援標頭的清單。

  • 未設定 x-goog-acl 標頭。也就是說,寫入值區時,系統會將公開讀取的預設 Cloud Storage ACL 套用至物件。

  • 請務必在完成寫入後呼叫函式來關閉檔案。如果沒有執行此操作,檔案就不會寫入至 Cloud Storage。請注意,呼叫 Python 檔案函式 close() 後,您就無法附加檔案。如需修改檔案,您必須呼叫 Python 檔案函式 open(),以便在寫入模式中再次開啟檔案。請注意,這樣做會覆寫原先內容,並非額外附加新內容。

從 Cloud Storage 讀取

以下範例說明如何從值區讀取完整檔案:

from google.cloud import storage


def write_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.html
    with blob.open("w") as f:
        f.write("Hello world")

    with blob.open("r") as f:
        print(f.read())

在兩個範例中,您傳遞至 cloudstorage.open()blob_name 引數,是檔案的路徑,格式為 YOUR_BUCKET_NAME/PATH_IN_GCS。請注意,cloudstorage.open() 的預設模式為唯讀模式。開啟檔案進行讀取時,您不需要指定模式。

列出值區內容

以下程式碼範例說明如何透過 blob 類型內容瀏覽資料集:

from google.cloud import storage


def list_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.
    for blob in blobs:
        print(blob.name)

請注意,完整的檔案名稱會顯示為單一字串,不會有目錄分隔字元。如果您想以更容易辨識的目錄階層顯示檔案,請將 delimiter 參數設為要使用的目錄分隔符。

刪除 Cloud Storage 中的檔案

以下程式碼示範如何使用 cloudstorage.delete() 方法 (匯入為 gcs) 刪除 Cloud Storage 中的檔案。

from google.cloud import storage


def delete_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.generation

    blob.delete(if_generation_match=generation_match_precondition)

    print(f"Blob {blob_name} deleted.")

這個範例會清除在寫入 Cloud Storage 一節中寫入至值區的檔案。

後續步驟