Having Trouble Uploading Images To Google Cloud

I am trying to have my site deploy to heroku but it has media files and I decided to use google cloud but I am having a hard time uploading to it. I set things up but I am not sure what command I need to use to upload it to my bucket.

settings.py

#Getting crediatials

from google.oauth2 import service_account

GS_CREDENTIALS = service_account.Credentials.from_service_account_file

(os.path.join(BASE_DIR, 'credentials.json'))

#For Media in the bucket

DEFAULT_FILE_STORAGE = 'config.storage_backends.GoogleCloudMediaStorage'

GS_PROJECT_ID = "id of my project"

GS_BUCKET_NAME = "bucket name"

MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_BUCKET_NAME)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

I created a gcloud.py file that handles the transfer

gcloud.py

class GoogleCloudMediaFileStorage(GoogleCloudStorage):
    bucket_name = setting('GS_BUCKET_NAME')
    def url(self, name):
        return urljoin(settings.MEDIA_URL, name)

For your media files, it would occur when a user uploads a file. Or a background system, creates and saves a file. It should happen in the background automatically. The documentation has an example too.

I’m not seeing any objects in my bucket when I run my application locally. When I run my application I get a 500 error and I can’t print my default file storage either.

gcloud.py

class GoogleCloudMediaFileStorage(GoogleCloudStorage):
    bucket_name = setting('GS_BUCKET_NAME')
    #added to print to help me see if the process works
    print(default_storage.__class__)
    def url(self, name):
        return urljoin(settings.MEDIA_URL, name)

What’s the error and stacktrace?

It’s basically just a 500 error like this thats why it is hard for me to debug.

"GET / HTTP/1.1" 500 145

Where are you seeing that log? There should be a stacktrace somewhere.

Okay I can get a stacktrace for you from my google import. from google.oauth2 import service_account

settings.py", line 134, in
from google.oauth2 import service_account
ModuleNotFoundError: No module named ‘google’

It sounds like you may not have specified the dependency for the google SDK causing your heroku app to not have access to it.