2016-08-19 69 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

相關問題