[英]Copying contents from one folder to another in same GCS Bucket
I have a Python script that does some processing files on the input folder on my GCS Bucket.我有一个 Python 脚本,它在我的 GCS Bucket 上的输入文件夹中执行一些处理文件。 After processing completes, I want to move these files to another folder in the same GCS Bucket.
处理完成后,我想将这些文件移动到同一个 GCS Bucket 中的另一个文件夹。 Any idea how it can be implemented.
任何想法如何实施。
There is no such thing as "folders" in cloud storage buckets.云存储桶中没有“文件夹”这样的东西。 You just need to copy the blob file to a new destination file name, and delete the former one, as follows :
你只需要将blob文件复制到一个新的目标文件名,并删除前一个,如下:
import logging
from google.cloud import storage
STORAGE_BUCKET=""
def move_image_within_bucket(source_blob_name: str, origin_folder: str, destination_folder: str):
""" Move image file from one folder to another folder within same Storage bucket """
storage_client = storage.Client()
bucket = storage_client.bucket(STORAGE_BUCKET)
source_blob = bucket.get_blob(source_blob_name)
destination_blob_name = source_blob_name.replace(origin_folder, destination_folder, 1)
blob_copy = bucket.copy_blob(source_blob, bucket, new_name=destination_blob_name)
logging.info(f"Image {source_blob} moved from folder {origin_folder} to {destination_folder} within bucket {STORAGE_BUCKET}")
source_blob.delete()
# example : move_image_within_bucket(source_blob_name="dev/image.jpg", origin_folder="dev", destination_folder="prod")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.