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

标签 python json upload blob google-cloud-storage

我正在尝试上传 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)

关于python - 通过python上传JSON到谷歌云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44876235/

相关文章:

python - 如何在计算互补列表时正确处理重复项?

java - 如何使用 Retrofit 查询某些 JSON 项?

python - 使用 python ( 谷歌应用程序引擎) 获取上传文件的名称和扩展名

php - 将id插入MYSQL表以进行多图片上传

html - 如何使用 HTML 将文件上传到网站

python - 在 Pandas 中,如何将 bool 列转换为分类列?

python - 如何使用 M2Crypto 从 PKCS7 信封中获取签名内容?

python - 如何在不同数组的 For In 中访问数组的特定单元格?

java - 在Spring中读取Content-Type application/json

javascript - 访问 .json 文件导致银行假期访问被拒绝