【问题标题】:Google Cloud Storage Object Read/Write Operation from App Engine来自 App Engine 的 Google Cloud Storage 对象读/写操作
【发布时间】:2020-08-05 08:12:34
【问题描述】:

我正在从我的 App Engine 标准 Python 3 应用程序访问 Google Cloud Storage 中的一个对象,将该对象下载到 /tmp 文件夹,对其进行编辑,然后将此编辑后的对象上传回 Google Storage:

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)

有没有办法直接编辑对象而无需将其下载到 /tmp 文件夹?我找不到这个方法

【问题讨论】:

    标签: google-app-engine google-cloud-storage


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2018-07-22
      • 2015-07-07
      • 2012-01-02
      • 2021-11-14
      • 2014-01-16
      • 2011-12-25
      相关资源
      最近更新 更多