Python-从Google Cloud Storage下载整个目录 [英] Python - download entire directory from Google Cloud Storage

查看:288
本文介绍了Python-从Google Cloud Storage下载整个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下一页

https://googlecloudplatform.github.io/google- cloud-python/latest/storage/blobs.html

所有可用于Python&的API调用Google云端存储.甚至在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包含整个目录结构+文件名,因此,如果要与存储桶中的文件名相同,则可能要先解压缩(而不是将/替换为_)

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下载整个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆