Python - download entire directory from Google Cloud Storage(Python - 从 Google Cloud Storage 下载整个目录)
问题描述
在下一页
https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blob.html
有所有可用于 Python & 的 API 调用.谷歌云存储.即使在 github 上的官方"示例中
there are all the API calls which can be used for Python & Google Cloud storage. Even in the "official" samples on github
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/cloud-client/snippets.py
没有相关的例子.
最后,用与下载文件相同的方法下载目录会报错
Finally, downloading a directory with the same method used for download files gives the error
Error: [Errno 21] Is a directory:
推荐答案
你只需要先列出一个目录下的所有文件,然后一个一个下载:
You just have to first list all the files in a directory and then download them one by one:
bucket_name = 'your-bucket-name'
prefix = 'your-bucket-directory/'
dl_dir = 'your-local-directory/'
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name=bucket_name)
blobs = bucket.list_blobs(prefix=prefix) # Get list of files
for blob in blobs:
filename = blob.name.replace('/', '_')
blob.download_to_filename(dl_dir + filename) # Download
blob.name 包括整个目录结构+文件名,所以如果你想要和bucket中相同的文件名,你可能想先提取它(而不是替换/ 与 _)
blob.name includes the entire directory structure + filename, so if you want the same file name as in the bucket, you might want to extract it first (instead of replacing / with _)
这篇关于Python - 从 Google Cloud Storage 下载整个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - 从 Google Cloud Storage 下载整个目录
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
