我想将本地文件复制到谷歌云存储桶。我可以在命令行中使用 gsutil cp 来做到这一点。但我想在 python 中执行此操作,因此要在 python 中运行类似的命令,我使用以下行:
os.system('gsutil cp '+ '/MY-LOCAL-FILE-NAME' +' gs://My-Bucket-Name')
它将文件发送到存储桶,但它是一个空文件!!! 我也试过 python subprocess 但同样的问题。 我认为问题是字符串连接。当我使用整个字符串时它可以工作并将文件发送到存储桶但是当我在“subprocess”或“os.system”中连接子字符串时,它只发送一个空文件,这太奇怪了,对此有什么想法吗???
请您参考如下方法:
从 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))
