I'm trying to configure Django (5.0.4) to use Google Cloud S3 as my file storage using django-stores.
Here is what my settings.py configuration for object storage looks like:
from google.oauth2.service_account import Credentials
STORAGES = {
"default": {
"BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
"OPTIONS": {
"GS_BUCKET_NAME": "BUCKET",
"GS_PROJECT_ID": "PROJECT",
"GS_CREDENTIALS": Credentials.from_service_account_file("service-account.json"),
},
},
"staticfiles": "storages.backends.gcloud.GoogleCloudStorage"
}
After running python manage.py collectstatic
I'm getting the following AttributeError
Traceback (most recent call last):
File "/home/victor/BmLabs/site/manage.py", line 22, in <module>
main()
File "/home/victor/BmLabs/site/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/core/management/__init__.py", line 382, in execute
settings.INSTALLED_APPS
File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 89, in __getattr__
self._setup(name)
File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 76, in _setup
self._wrapped = Settings(settings_module)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 262, in __init__
self.STORAGES.get(STATICFILES_STORAGE_ALIAS, {}).get("BACKEND"),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'
As per the guide I've set the staticfiles
key to storages.backends.gcloud.GoogleCloudStorage
.
In old Django versions, there are the independent top-level variables DEFAULT_FILE_STORAGE
and/or STATICFILES_STORAGE
, which accept a single string.
In modern Django versions, there's STORAGES
, which requires a dict of dicts with the BACKEND
key. Make the staticfiles
storage config look just as you made the default
config look, not just a single string.