【问题标题】:How to download entire folder of blobs (images) from google storage bucket to AI Platform Notebooks in one go?如何一次性将整个 blob(图像)文件夹从谷歌存储桶下载到 AI Platform Notebooks?
【发布时间】:2020-10-24 07:38:06
【问题描述】:

我是 GCP 的新手,因此提前感谢您的耐心等待。我已将一个文件夹(其中包含包含图像的文件夹)上传到我的谷歌云存储桶,现在我想在 jupyter 笔记本的 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'

单个图像的路径示例是:

五个手势/00/01_palm/frame_00_01_0001.png

还有 01_palm 包含数百张这样的图像。

【问题讨论】:

  • 显示你的代码。

标签: google-cloud-platform jupyter-notebook google-cloud-storage blob gsutil


【解决方案1】:

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

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包含整个目录结构+文件名,所以如果你想要和bucket中相同的文件名,你可能需要先提取它(而不是用_替换/

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 2019-04-13
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2016-12-22
    • 2019-09-22
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多