【发布时间】:2020-12-23 01:57:57
【问题描述】:
我正在尝试编写一个脚本来将文件上传到 Google Cloud Storage。我注意到有两种方法可以做到这一点:
a) 使用 gsutil 并通过子进程从 python 调用它 b) 将 from google.cloud 导入存储与“本机”方法一起使用。
每种方法的优点/缺点是什么? (a) 方法似乎更容易,但我不知道与 b) 方法相比是否有任何缺点。
谢谢!
(a)的例子
filename='myfile.csv'
gs_bucket='my/bucket'
parallel_threshold='150M' # minimum size for parallel upload; 0 to disable
subprocess.check_call([
'gsutil',
'-o', 'GSUtil:parallel_composite_upload_threshold=%s' % (parallel_threshold,),
'cp', filename, 'gs://%s/%s' % (gs_bucket, filename)
])
(b)的例子
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.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
)
)
【问题讨论】:
标签: python google-cloud-platform google-cloud-storage