Google 云存储对象 Read/Write 从 App Engine 操作

Google Cloud Storage Object Read/Write Operation from App Engine

我正在从我的 App Engine Standard Python 3 应用程序访问 Google Cloud Storage 中的一个对象,将该对象下载到 /tmp 文件夹,编辑它,然后将这个编辑过的对象上传回Google 存储空间:

from google.cloud import storage

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    # bucket_name = "your-bucket-name"
    # source_blob_name = "storage-object-name"
    # destination_file_name = "local/path/to/file"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    # bucket_name = "your-bucket-name"
    # source_file_name = "local/path/to/file"
    # destination_blob_name = "storage-object-name"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename(source_file_name)

def edit_file(request):
        ... skipped for brevity ...
        download_blob(bucket_name, source_blob_name, destination_file_name)
        with open(source_file_name, "a") as f:
            f.write(f"{x}: {y}: {z}<br>")
        upload_blob(bucket_name, source_file_name, destination_blob_name)

有没有办法直接编辑对象 w/o 下载到 /tmp 文件夹?我找不到这个方法

Cloud Storage 中的 blob 对象是不可变对象。所以你需要在某个地方下载它并修改以创建新对象然后上传它。