【问题标题】:How to upload a JSON to google cloud storage using python如何使用 python 将 JSON 上传到谷歌云存储
【发布时间】:2017-12-04 01:00:34
【问题描述】:

我正在尝试加载此代码以通过 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 = ('t.json', bucket)
with open('t.json', 'rb'):
  blob.upload_from_file('t.json')

我正在遵循here...上的指南...

我被困住了,不知道去哪里,所以任何帮助都将不胜感激。我已将 blob.upload_from_file('t.json') 更改为 blob.upload('t.json') 并遇到同样的问题。

【问题讨论】:

  • 请在此处发布您的错误代码。
  • upload_from_file 方法将file object 作为参数,您将文件名传递给upload_from_file 方法。
  • Traceback(最近一次调用最后):文件“uploadcloud.py”,第 16 行,在 blob.upload_from_file('t.json') AttributeError: 'tuple' object has no attribute' upload_from_file'

标签: python json upload blob google-cloud-storage


【解决方案1】:

看起来您正在尝试使用 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 json_file:
  blob.upload_from_file(json_file)

【讨论】:

  • 回溯(最近一次调用最后):文件“uploadcloud.py”,第 16 行,在 blob.upload_from_file('f') 文件“/Library/Python/2.7/site-packages /google/cloud/storage/blob.py”,第 891 行,在 upload_from_file 客户端,file_obj,content_type,大小,num_retries)文件“/Library/Python/2.7/site-packages/google/cloud/storage/blob.py” ,第 815 行,在 _do_upload 客户端,流,content_type,大小,num_retries) 文件“/Library/Python/2.7/site-packages/google/cloud/storage/blob.py”,第 634 行,在 _do_multipart_upload 数据 = stream.read () AttributeError: 'str' 对象没有属性 'read'
  • 我猜你还在调用upload_from_file('t.json')。应该是upload_from_file(f)。
猜你喜欢
  • 2017-12-06
  • 2020-03-19
  • 2021-06-09
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多