Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python

2024-02-09

我想将文件列表从谷歌存储移动到另一个文件夹:

storage_client = storage.Client()
count = 0

# Retrieve all blobs with a prefix matching the file.
bucket=storage_client.get_bucket(BUCKET_NAME)
# List blobs iterate in folder 
blobs=bucket.list_blobs(prefix=GS_FILES_PATH, delimiter='/') # Excluding folder inside bucket
for blob in blobs:
if fnmatch.fnmatch(blob.name, FILE_PREF):
         WHAT CAN GO HERE?
         count += 1   

我在谷歌文档中找到的唯一有用的信息是:

  • https://cloud.google.com/storage/docs/renaming-copying-moving-objects https://cloud.google.com/storage/docs/renaming-copying-moving-objects

根据本文档,唯一的方法是将一个文件夹复制到另一个文件夹并将其删除。

  1. 有什么方法可以实际移动文件吗?
  2. 基于前缀移动所有文件的最佳方法是什么*BLABLA*.csv

附:不想使用

  • "gsutil mv gs://[SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME] gs://[DESTINATION_BUCKET_NAME]/[DESTINATION_OBJECT_NAME]"

这可能是一个可能的解决方案,因为 google.cloud.storage 中没有 move_blob 函数:

from google.cloud import storage  

dest_bucket = storage_client.create_bucket(bucket_to)
source_bucket = storage_client.get_bucket(bucket_from)
blobs = source_bucket.list_blobs(prefix=GS_FILES_PATH, delimiter='/') #assuming this is tested

for blob in blobs:
    if fnmatch.fnmatch(blob.name, FILE_PREF): #assuming this is tested
        source_bucket.copy_blob(blob,dest_bucket,new_name = blob.name)
        source_bucket.delete_blob(blob.name)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python 的相关文章

随机推荐