【问题标题】:How to copy a directory to google cloud storage using google cloud Python API?如何使用谷歌云 Python API 将目录复制到谷歌云存储?
【发布时间】:2018-07-08 22:56:06
【问题描述】:

以下函数非常适合将单个文件复制到谷歌云存储。

#!/usr/bin/python3.5
import googleapiclient.discovery

from google.cloud import storage

def upload_blob(bucket_name, source_file_name, destination_blob_name, project):
  storage_client = storage.Client(project=project)
  bucket = storage_client.get_bucket(bucket_name)
  blob = bucket.blob(destination_blob_name)

blob.upload_from_filename(source_file_name)

print('File {} uploaded to {}.'.format(
    source_file_name,
    destination_blob_name))

现在我没有给出文件名,而是尝试输入目录名upload_blob('mybucket','/data/inputdata/', 'myapp/inputdata/','myapp') 但后来我得到这个错误:

AttributeError: 'str' 对象没有属性 'read'

调用函数blob.upload_from_file()复制目录时是否需要额外提供参数?

【问题讨论】:

  • 文件上传正常吗??
  • 是的,效果很好。

标签: python python-3.x google-app-engine google-cloud-platform google-cloud-storage


【解决方案1】:

一次上传多个文件不是 API 的内置功能。您可以循环复制多个文件,也可以改用命令行实用程序,它可以复制整个目录。

【讨论】:

    【解决方案2】:

    您可以使用以下代码来完成此操作:

    import os
    import glob
    
    def copy_local_directory_to_gcs(local_path, bucket, gcs_path):
        """Recursively copy a directory of files to GCS.
    
        local_path should be a directory and not have a trailing slash.
        """
        assert os.path.isdir(local_path)
        for local_file in glob.glob(local_path + '/**'):
            if not os.path.isfile(local_file):
                continue
            remote_path = os.path.join(gcs_path, local_file[1 + len(local_path) :])
            blob = bucket.blob(remote_path)
            blob.upload_from_filename(local_file)
    

    像这样使用它:

    copy_local_directory_to_gcs('path/to/foo', bucket, 'remote/path/to/foo')
    

    bucket 是来自 Google Cloud Storage API 的常用对象:

    from google.cloud import storage    
    client = storage.Client(project='your-project')
    bucket = client.get_bucket('bucket-name')
    

    【讨论】:

    • 函数中的“bucket”参数是什么类型的对象?是字符串吗?
    • @DominiquePaul 它是 Cloud Storage Python API 返回的存储桶对象。
    • 需要迭代您自己的文件来复制目录似乎是 API 应该为用户处理的事情。它可能是第二种最常见的使用模式,并且几乎是任何需要以编程方式访问 GCS 的端到端应用程序的一部分。我认为这不仅仅是 API 的内置功能,这太疯狂了。
    猜你喜欢
    • 2016-04-26
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多