1

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))

3
  • Any suggestions please ? Commented Aug 19, 2016 at 14:45
  • Could you add more information to what imports you've used to get this to work? Commented Mar 30, 2020 at 18:56
  • so i've found out by doing some digging. To get this to work you'll need ----- from googleapiclient.discovery import build from googleapiclient import http from oauth2client.client import GoogleCredentials import io for predefinedAcl options you can go here: cloud.google.com/storage/docs/access-control/… bucket needs to be the full name of your bucket. If you come from firebase it's "<name>.appspot.com" Commented Mar 31, 2020 at 11:18

2 Answers 2

3

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

content = json.dumps({'name': 'test'})
Sign up to request clarification or add additional context in comments.

2 Comments

It Worked. missed this simple thing. was trying number of different things for this. Thanks. :)
This above code is working absolutely fine in python manage.py shell . But not in local server. Can you suggest any possible reasons for the same ?
0

To answer my own comment question on how to get this to work:

To get this to work you'll need

from googleapiclient.discovery import build
from googleapiclient import http
from oauth2client.client import GoogleCredentials
import io

Also i needed to change this:

 media = http.MediaIoBaseUpload(StringIO(content), mimetype='plain/text')

in to this:

media = http.MediaIoBaseUpload(io.StringIO(json.dumps(content)), mimetype='plain/text')

Note the insert of 'io' besides the json.dumps recommended by Corey Goldberg.

for predefinedAcl options you can go here:

https://cloud.google.com/storage/docs/access-control/lists#predefined-acl

Bucket needs to be the full name of your bucket. If you come from firebase it's "[name].appspot.com"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.