通过os.system在python中使用gsutil cp



我想将本地文件复制到谷歌云存储桶。我可以在命令行中使用gsutil cp来完成此操作。但我想在python中这样做,因此要在python中运行类似的命令,我使用以下行:

os.system('gsutil cp '+ '/MY-LOCAL-FILE-NAME' +' gs://My-Bucket-Name')

它将文件发送到bucket,但它是一个空文件!!!我也尝试了python子流程,但同样的问题。我认为问题出在字符串连接上。当我使用整个字符串时,它会工作并将文件发送到bucket,但当我在"subprocess"或"os.system"中连接子字符串时,只会发送一个空文件,这太奇怪了,你知道吗???

从Gcloud github中,您有感兴趣的片段:

def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
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))

最新更新