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