简体   繁体   English

将内容从一个文件夹复制到同一 GCS Bucket 中的另一个文件夹

[英]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.

相关问题 使用boto3,从整个文件夹或文件从一个s3存储桶复制到同一区域的另一个存储桶时,如何提供访问密钥和秘密访问密钥? - Using boto3, while copying from whole folder or file from one s3 bucket to another in same region, how to provide access key and secret access key? 将文件从一个存储桶移动到另一个存储桶中的文件夹 - Move files from one bucket to a folder inside another bucket 为什么将一个文件从一个 S3 存储桶复制到另一个存储桶时,boto3 会静默失败? - Why is boto3 silently failing when copying one file from one S3 bucket to another? 使用 python 将文件上传到 gcs 存储桶中的文件夹 - upload file to a folder in gcs bucket using python 将列表中的多个图像从一个文件夹复制到另一个文件夹而不复制所有图像 - Copying multiple images in a list from one folder to another without copying all images IsADirectoryError: [Errno 21] 是目录:'/' 将文件从一个文件夹复制到另一个现有文件夹时 Python - IsADirectoryError: [Errno 21] Is a directory: '/' while copying files from one folder to another existing folder Python 仅当另一个文件夹中不存在子文件夹和文件时才从一个文件夹复制子文件夹和文件 - Copying subfolders and files from one folder only if they don't exist in another folder 从 python 中的 GCS 存储桶加载具有多个 json 对象的 json 文件,并一次处理一个 - Load json file with multiple json objects from GCS bucket in python and process them one at a time Boto3:将大文件的元数据从一个存储桶复制到另一个存储桶的问题 - Boto3: Issue Copying metadata for large files from one bucket to other bucket numpy 将数据从一个阵列复制到另一个阵列 - numpy copying data from one array to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM