【问题标题】:Downloading folders from Google Cloud Storage Bucket从 Google Cloud Storage Bucket 下载文件夹
【发布时间】:2017-11-08 10:58:46
【问题描述】:

我是 Google Cloud Platform 的新手。我已经在 datalab 上训练了我的模型,并将模型文件夹保存在我的存储桶中的云存储中。通过右键单击文件->另存为链接,我可以将存储桶中的现有文件下载到本地计算机。但是当我尝试通过与上述相同的过程下载文件夹时,我得到的不是文件夹而是它的图像。无论如何我可以下载整个文件夹及其内容吗?是否有任何 gsutil 命令可以将文件夹从云存储复制到本地目录?

【问题讨论】:

  • 不是这个问题的正确位置。

标签: google-cloud-platform google-cloud-storage


【解决方案1】:

您可以在 gsutil 工具 here 上找到文档,更具体地说是在 here 上找到您的问题。

你要使用的命令是:

gsutil cp -r gs://bucket/folder .

【讨论】:

  • 这不是我真正想要的。我已经设法使用 gsutil 命令将我的文件夹从 Google Cloud Datalab 复制到云存储。我的问题是有什么方法可以将文件夹下载到我的本地机器上,以便我可以离线使用它?
  • 这个命令,当在你的本地命令行上执行时,将完全做到这一点。 -r 标志后面的两个选项指定: 1. 您要下载的文件夹的 GCS 路径 2. 您要下载到的文件夹(这将是您在命令行会话中使用“. " 但也可以是 C:/Users/username/Documents 或 /home/username/ 之类的东西
  • 每当我将本地目录的路径作为目标时,例如 C:/Users/username/Documents ,它都会出现此错误。 “CommandException:目标 URL 必须为 cp 命令的多源形式命名一个目录、存储桶或存储桶子目录。”
  • gsutil cp -r gs://api-project-921234036675cancer-data-7617/cancer_model7617 C:/Users/sanghamitra.rc
  • 我遇到了与@JSnow 相同的错误,我在我的情况下修复了它。原因是目标文件夹不存在,我希望命令创建它,但它却给出了该错误。因此,只需创建目录即可为我修复它。希望这对正在寻找相同答案的人有所帮助。
【解决方案2】:

先决条件: 已安装并初始化 Google Cloud SDK ($ glcoud init)

命令:

gsutil -m cp -r  gs://bucket-name .

这将使用更快的多线程复制所有文件。我发现官方 Gsutil Docs 中指示使用的“dir”命令不起作用。

【讨论】:

  • 先决条件很有帮助
【解决方案3】:

这是从 Google Cloud Storage Bucket 下载文件夹的方法

运行以下命令将其从存储桶存储下载到您的 Google Cloud Console 本地路径

gsutil -m cp -r gs://{bucketname}/{folderPath} {localpath}

运行该命令后,通过运行ls 命令列出本地路径上的文件和目录,确认您的文件夹位于本地路径中

现在通过运行以下命令压缩您的文件夹

zip -r foldername.zp yourfolder/*

压缩过程完成后,点击 Google Cloud Console 右侧的更多下拉菜单,

然后选择“下载文件”选项。系统会提示你输入要下载的文件名,输入zip文件名-“foldername.zp”

【讨论】:

  • 是的,这适用于小文件。我写了相同的解决方案here,但是模组删除了那个问题和答案集(我猜是因为 SO 是用于编程而不是云管理问题)。无论如何,如果有人对此方法有疑问,我的链接答案会提供更多详细信息/选项。
【解决方案4】:

如果您使用 python 从谷歌云存储中下载数据并希望保持相同的文件夹结构,请按照我在 python 中编写的这段代码。

选项 1

from google.cloud import storage

def findOccurrences(s, ch): # to find position of '/' in blob path ,used to create folders in local storage
    return [i for i, letter in enumerate(s) if letter == ch]

def download_from_bucket(bucket_name, blob_path, local_path):    
    # Create this folder locally
    if not os.path.exists(local_path):
        os.makedirs(local_path)        

    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blobs=list(bucket.list_blobs(prefix=blob_path))

    startloc = 0
    for blob in blobs:
        startloc = 0
        folderloc = findOccurrences(blob.name.replace(blob_path, ''), '/') 
        if(not blob.name.endswith("/")):
            if(blob.name.replace(blob_path, '').find("/") == -1):
                downloadpath=local_path + '/' + blob.name.replace(blob_path, '')
                logging.info(downloadpath)
                blob.download_to_filename(downloadpath)
            else:
                for folder in folderloc:
                    
                    if not os.path.exists(local_path + '/' + blob.name.replace(blob_path, '')[startloc:folder]):
                        create_folder=local_path + '/' +blob.name.replace(blob_path, '')[0:startloc]+ '/' +blob.name.replace(blob_path, '')[startloc:folder]
                        startloc = folder + 1
                        os.makedirs(create_folder)
                    
                downloadpath=local_path + '/' + blob.name.replace(blob_path, '')

                blob.download_to_filename(downloadpath)
                logging.info(blob.name.replace(blob_path, '')[0:blob.name.replace(blob_path, '').find("/")])

    logging.info('Blob {} downloaded to {}.'.format(blob_path, local_path))


bucket_name = 'google-cloud-storage-bucket-name' # do not use gs://
blob_path = 'training/data' # blob path in bucket where data is stored 
local_dir = 'local-folder name' #trainingData folder in local
download_from_bucket(bucket_name, blob_path, local_dir)

选项 2:使用 gsutil sdk 下面是通过 python 程序执行此操作的另一种选择。

def download_bucket_objects(bucket_name, blob_path, local_path):
    # blob path is bucket folder name
    command = "gsutil cp -r gs://{bucketname}/{blobpath} {localpath}".format(bucketname = bucket_name, blobpath = blob_path, localpath = local_path)
    os.system(command)
    return command

选项 3 - 没有 python,直接使用终端和谷歌 SDK 先决条件:Google Cloud SDK 已安装并初始化($ glcoud init) 命令参考以下链接:

https://cloud.google.com/storage/docs/gsutil/commands/cp

【讨论】:

    【解决方案5】:

    gsutil -m cp -r gs://bucket-name "{本地现有文件夹的路径}"

    确实有效。

    【讨论】:

      【解决方案6】:

      这是我编写的代码。 这会将完整的目录结构下载到您的虚拟机/本地存储中。

      from google.cloud import storage
      import os
      bucket_name = "ar-data"
          
      storage_client = storage.Client()
      bucket = storage_client.get_bucket(bucket_name)
      
      dirName='Data_03_09/' #***folder in bucket whose content you want to download
      blobs = bucket.list_blobs(prefix = dirName)#, delimiter = '/')
      destpath=r'/home/jupyter/DATA_test/' #***path on your vm/local where you want to download the bucket directory
      for blob in blobs:
          #print(blob.name.lstrip(dirName).split('/'))
          currpath=destpath
          if not os.path.exists(os.path.join(destpath,'/'.join(blob.name.lstrip(dirName)).split('/')[:-1])):
              for n in blob.name.lstrip(dirName).split('/')[:-1]:
                  currpath=os.path.join(currpath,n)
                  if not os.path.exists(currpath):
                      print('creating directory- ', n , 'On path-', currpath)
                      os.mkdir(currpath)
          print("downloading ... ",blob.name.lstrip(dirName))
          blob.download_to_filename(os.path.join(destpath,blob.name.lstrip(dirName)))
      

      或者只是在终端中使用:

      gsutil -m cp -r gs://{bucketname}/{folderPath} {localpath}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 2014-11-14
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多