Naveen Naveen - 3 months ago 21
Python Question

Upload raw JSON data on Google Cloud Storage using Python code

I am trying to upload raw JSON data on Google Cloud Platform. But I am getting this error:

TypeError: must be string or buffer, not dict


Code:

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


Code Worked by changing
StringIO(content)
to
StringIO(json.dumps(content))

Answer

in your example, content is a dict. Perhaps you want to use json?

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