How can i use firebase cloud storage to handle static files

I am building a a Practice blog with django, after hosting it on heroku, i discovered that any images i upload using image field or fetch disappear after a while.

Now i started using firebase cloud storage via django-storage gloud backend and it is working fine since then , but when i tried hosting my staticfiles storage from there is get this error

cross origin request blocked the same-origin policy disallows reading the remote resource at?

how do i fix this?


enter image description here

    if not DEBUG:
    GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
        os.path.join(BASE_DIR, 'jojopage-123-firebase-adminsdk-l8qsw-810f7d7c00.json')
    )

    DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
    GS_BUCKET_NAME = 'jojopage-123.appspot.com'

    STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATIC_URL = '/static/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
    MEDIA_URL = '/media/'

Here is the error enter image description here

Upvotes: 1

Views: 418

Answers (1)

Ihor Pomaranskyy
Ihor Pomaranskyy

Reputation: 5651

You have a bunch of options:

  1. Enable CORS in Django
  2. or set up images proxying/routing using web server (such as nginx)
  3. or write some Django view which will do images proxying/routing with Python (it's relatively easy, but I'd not recommend it for production use)

The first option is probably the easiest one, you can do it by following a tutorias, for example this one.

Upvotes: 1

Related Questions