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

时间:2018-07-25 00:43:32

标签: python google-cloud-platform cp gsutil os.system

我想将本地文件复制到Google云存储桶。我可以在命令行中使用pingInterval来做到这一点。但我想在python中执行此操作,因此要在python中运行类似的命令,请使用以下行:

gsutil cp

它将文件发送到存储桶,但这是一个空文件!!! 我也尝试了python子进程,但同样的问题。 我认为问题是字符串串联。当我使用整个字符串时,它可以工作并将文件发送到存储桶,但是当我在“ subprocess”或“ os.system”中连接子字符串时,它只是发送一个空文件,这很奇怪,关于此的任何想法? / p>

1 个答案:

答案 0 :(得分:0)

在Gcloud github上,您拥有snippets,这里是您感兴趣的地方:

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