以下功能非常适合将单个文件复制到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()
以复制目录时是否需要提供任何其他参数?
这是一些您可以用来完成此操作的代码:
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] 删除。
我来说两句