如何使用Google Cloud Python API将目录复制到Google Cloud Storage?

毗湿奴(PN)

以下功能非常适合将单个文件复制到Google云存储。

#!/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()以复制目录时是否需要提供任何其他参数

Danvk

这是一些您可以用来完成此操作的代码:

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')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Cloud Function(Python)写入Google Cloud Storage

如何从python函数登录Google Cloud Storage?

如何使用Google Cloud Dataflow将压缩文件写入Google Cloud Storage?

Google Cloud Storage Force下载

Google Cloud Storage ACL混淆

使用Python将文件上传到Google Cloud Storage Bucket子目录

如何使用python遍历Google Cloud Storage子目录中的所有文件名?

Python-从Google Cloud Storage下载整个目录

使用Python从Google Cloud Storage逐行读取巨大的JSON

使用Python向Google Cloud Storage写流

如何使用Deployment Manager将文件写入Google Cloud Storage?

使用Dataflow + Beam + Python从Google Cloud Storage读取Shapefile

从Python将TFRecord输出到Google Cloud Storage

如何从Google Cloud Functions NodeJS连接到Google Cloud Storage

使用Python脚本中的Google Cloud Functions从Google Cloud Storage中读取CSV

从Google Cloud Function(Python)将新文件写入Google Cloud Storage存储桶

Google Cloud Storage与Google Cloud CDN

如何使用Google API从Google Cloud Storage获取报告

使用Google Cloud Dataflow合并Google Cloud Storage中的文件

将文件从 S3 存储桶复制到 Google Cloud Storage

Google Cloud Storage - 权限不足

Google Cloud Firestore:如何将 Firestore 集合复制到 Cloud Storage

简单的 Google Cloud 部署:将 Python 文件从 Google Cloud 存储库复制到应用引擎

如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件

PHP 的 Google Cloud Storage 问题

如何使用 gsutil 将文件从 Google Cloud Storage 存储桶 1 复制到存储桶 2,同时保留 ACL

使用 Django 将目录从 Google Cloud Storage Bucket 递归复制到另一个 Google Cloud Storage Bucket

如何使用 Cloud Scheduler 将数据存储到 Cloud Storage?

在 Google Cloud Build 中使用 Google Cloud Storage