Reputation: 97
I am accessing an object in Google Cloud Storage from my App Engine Standard Python 3 application, downloading the object to /tmp folder, editing it and then uploading this edited object back to the 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)
Is there a way to edit the object directly w/o downloading it to /tmp folder? I could not find a method for this
Upvotes: 0
Views: 248
Reputation: 1028
The blob object in Cloud Storage is immutable objects. So you need to download it somewhere and modify to create new object then upload it.
Upvotes: 1