Google Cloud Storage - Move file from one folder to another - By using Python(Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python)
问题描述
我想将文件列表从谷歌存储移动到另一个文件夹:
I would like to move a list of file from google storage to another folder:
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
我在 Google 文档中找到的唯一有用信息是:
The only useful information which I found in Google Documentation is:
- https://cloud.google.com/storage/docs/重命名复制移动对象
根据本文档,唯一的方法是从一个文件夹复制到另一个文件夹并删除它.
By this documentation, the only method is to copy from one folder to another and delete it.
- 有什么方法可以实际移动文件?
- 根据前缀移动所有文件的最佳方法是什么,例如
*BLABLA*.csv
附:不想用
"gsutil mv gs://[SOURCE_BUCKET_NAME]/[SOURCE_OBJECT_NAME]gs://[DESTINATION_BUCKET_NAME]/[DESTINATION_OBJECT_NAME]"
推荐答案
这可能是一个可能的解决方案,因为 google.cloud.storage 中没有 move_blob 函数:
This could be a possible solution, as there is no move_blob function in google.cloud.storage:
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)
这篇关于Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01