繁体   English   中英

通过python将JSON上传到谷歌云存储

[英]Uploading a JSON to google cloud storage via python

我正在尝试将 JSON 上传到谷歌云存储。 我可以手动完成,所以我知道它可以工作,但现在想编写一个可以自动完成的 python 脚本。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM