如何一次性从 google 存储桶下载整个文件夹的 blob(图像)到 AI Platform Notebooks?

How to download entire folder of blobs (images) from google storage bucket to AI Platform Notebooks in one go?

我是 GCP 的新手,在此先感谢您的耐心等待。我已经将一个文件夹上传到我的 google 云存储桶,其中包含包含图像的文件夹,现在我想在 jupyter notebooks 的 AI 平台实例上训练一个使用该数据的模型。我已经能够毫无问题地下载单个 blob,但是当需要下载我需要的整个图像数据文件夹时,无法识别该文件夹(我知道它不是 blob,但我仍然需要 jupyter lab 本地数据有效地训练模型,对吧?)。我已经看到 FUSE 不是一种选择,因为成本。我猜想在 GCP 环境中有一种方法可以做到这一点,但我还没弄明白。再次感谢您的帮助!

编辑:

这是(可以理解的)给我一个错误的代码:

blob_name = "five_gestures/"
blob = bucket.get_blob(blob_name)

output_file_name = "gestures/"
blob.download_to_filename(output_file_name)

print("Downloaded blob {} to {}.".format(blob.name, output_file_name))

输出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-31-9de8423ff843> in <module>
      3 
      4 output_file_name = "gestures/"
----> 5 blob.download_to_filename(output_file_name)
      6 
      7 print("Downloaded blob {} to {}.".format(blob.name, output_file_name))

AttributeError: 'NoneType' object has no attribute 'download_to_filename'

单个图像的路径示例是:

five_gestures/00/01_palm/frame_00_01_0001.png

并且 01_palm 包含数百张这样的图片。

你只需要先列出一个目录下的所有文件,然后一个一个下载:

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)
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 包括整个目录结构 + 文件名,因此如果您想要与存储桶中相同的文件名,您可能需要先提取它(而不是将 / 替换为 _)