2016-08-19 67 views
0

我正尝试在Google云端平台上传原始JSON数据。但我收到此错误:使用Python代码在Google云端存储上传原始JSON数据

TypeError: must be string or buffer, not dict 

代码:

def upload_data(destination_path): 
    credentials = GoogleCredentials.get_application_default() 
    service = discovery.build('storage', 'v1', credentials=credentials) 

    content = {'name': 'test'} 
    media = http.MediaIoBaseUpload(StringIO(content), mimetype='plain/text') 
    req = service.objects().insert(
     bucket=settings.GOOGLE_CLOUD_STORAGE_BUCKET, 
     body={"cacheControl": "public,max-age=31536000"}, 
     media_body=media, 
     predefinedAcl='publicRead', 
     name=destination_path, 
    ) 
    resp = req.execute() 

    return resp 

代码通过改变StringIO(content)合作StringIO(json.dumps(content))

+0

有什么建议吗? – Naveen

回答

3

在你的榜样,content是一个字典。也许你想使用json?

content = json.dumps({'name': 'test'}) 
+0

它工作。错过了这件简单的事。为此尝试了许多不同的事情。谢谢。 :) – Naveen

+0

上面的代码在python manage.py shell中工作得很好。但不在本地服务器。你能提出一些可能的原因吗? – Naveen

相关问题