Aman Gupta
Aman Gupta

Reputation: 45

Static files serving from Google Cloud Console in Django

I am using GCP to serve static files. But Django is using a signed URL which contains expire googleserveraccessId and etc. Please tell me how I can use an unsigned URL which only has a path. my settings.py file

DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = 'xyzxyzxyx'

GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    "project/filename.json"
)
STATICFILES_DIRS =[
    os.path.join(BASE_DIR, 'myapp/static')
]

STATIC_URL = '/static/'
STATIC_ROOT = 'https://storage.googleapis.com/{}/static/'.format(GS_BUCKET_NAME)

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

Upvotes: 0

Views: 424

Answers (2)

Gene S
Gene S

Reputation: 553

Add these two strings to your settings.py:

GS_QUERYSTRING_AUTH = False
GS_DEFAULT_ACL = None

Upvotes: 0

John Hanley
John Hanley

Reputation: 81464

If you want do not want to use authorization or signed urls, make the bucket or objects public and use the following URL format:

https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME

For details on making objects public via the Google Cloud Console:

Making individual objects publicly readable

You can also make objects public with the CLI:

gsutil acl ch -u AllUsers:R gs://BUCKET_NAME/OBJECT_NAME

Change object ACLs

Before trying those commands first check if this is supported for your bucket configuration:

  • Login to the Google Cloud Console.
  • Go to Cloud Storage.
  • Check the Access control column for the bucket containing the object you want to make public.
  • If the column reads Uniform then you cannot control individual objects.

If the above verfication is true use this link to make the entire bucket public:

Making all objects in a bucket publicly readable

Make sure that there is nothing in the bucket that should be private.

Upvotes: 1

Related Questions