通过python上传JSON到谷歌云存储
问题描述:
我想上传一个JSON我必须谷歌云存储。我可以手动完成,所以我知道它的工作原理,但现在想写一个自动执行它的python脚本。通过python上传JSON到谷歌云存储
import boto
import gcs_oauth2_boto_plugin
import os
import shutil
import StringIO
import tempfile
import time
from google.cloud import storage
from google.cloud.storage import blob
client = storage.Client(project='dataworks-356fa')
bucket = client.get_bucket('dataworks-356fa-backups')
blob = bucket.blob('t.json')
with open('t.json', 'rb') as f:
blob.upload_from_file('f')
是我到目前为止的代码,这是我得到的错误。
Traceback (most recent call last):
File "uploadcloud.py", line 16, in <module>
blob.upload_from_file('f')
File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 891, in upload_from_file
client, file_obj, content_type, size, num_retries)
File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 815, in _do_upload
client, stream, content_type, size, num_retries)
File "/Library/Python/2.7/site-packages/google/cloud/storage/blob.py", line 634, in _do_multipart_upload
data = stream.read()
AttributeError: 'str' object has no attribute 'read'
答
你应该传递一个打开的文件,以upload_from_file不串,只是改变
with open('t.json', 'rb') as f:
blob.upload_from_file(f)
谢谢你这个工作! –