【发布时间】: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