Question

botocore.exceptions.ClientError 403 HeadObject operation: Forbidden when using collectstatic, Django on App platform with Spaces

Hey everyone,

I have been struggling with this error for the past 3 days and could not find any further information on the web on how to fix it. Any help would be amazing!

I am running a Django project on the DigitalOcean app platform nicely, but setting up static file serving with the spaces S3 bucket raises the same error locally and in production:

botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

My settings.py contains the following configurations:

AWS_ACCESS_KEY_ID=os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY_ID=os.environ.get('AWS_SECRET_ACCESS_KEY_ID')
AWS_STORAGE_BUCKET_NAME=os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME=os.environ.get('AWS_S3_REGION_NAME')
AWS_S3_ENDPOINT_URL=os.environ.get('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_PARAMETERS={'CacheControl': 'max-age=86400'}
AWS_S3_SIGNATURE_VERSION='s3v4'
AWS_LOCATION='static'
AWS_DEFAULT_ACL='public-read'

I have checked the correctness of the API keys multiple times, but once I call collectstatic, the aforementioned error results. A full log from the DigitalOcean build logs after my packages have been installed:

[2023-11-01 09:45:32] │ -----> $ python manage.py collectstatic --noinput
[2023-11-01 09:45:32] │        /workspace/manage.py:9: UserWarning: Not reading /workspace/.env - it doesn't exist.
[2023-11-01 09:45:32] │          dotenv.read_dotenv()
[2023-11-01 09:45:33] │        Traceback (most recent call last):
[2023-11-01 09:45:33] │          File "/workspace/manage.py", line 23, in <module>
[2023-11-01 09:45:33] │            main()
[2023-11-01 09:45:33] │          File "/workspace/manage.py", line 19, in main
[2023-11-01 09:45:33] │            execute_from_command_line(sys.argv)
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
[2023-11-01 09:45:33] │            utility.execute()
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
[2023-11-01 09:45:33] │            self.fetch_command(subcommand).run_from_argv(self.argv)
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
[2023-11-01 09:45:33] │            self.execute(*args, **cmd_options)
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
[2023-11-01 09:45:33] │            output = self.handle(*args, **options)
[2023-11-01 09:45:33] │                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
[2023-11-01 09:45:33] │            collected = self.collect()
[2023-11-01 09:45:33] │                        ^^^^^^^^^^^^^^
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
[2023-11-01 09:45:33] │            handler(path, prefixed_path, storage)
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
[2023-11-01 09:45:33] │            if not self.delete_file(path, prefixed_path, source_storage):
[2023-11-01 09:45:33] │                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
[2023-11-01 09:45:33] │            if self.storage.exists(prefixed_path):
[2023-11-01 09:45:33] │               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/storages/backends/s3.py", line 514, in exists
[2023-11-01 09:45:33] │            self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/botocore/client.py", line 535, in _api_call
[2023-11-01 09:45:33] │            return self._make_api_call(operation_name, kwargs)
[2023-11-01 09:45:33] │                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2023-11-01 09:45:33] │          File "/app/.heroku/python/lib/python3.11/site-packages/botocore/client.py", line 980, in _make_api_call
[2023-11-01 09:45:33] │            raise error_class(parsed_response, operation_name)
[2023-11-01 09:45:33] │        botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │  !     Error while running '$ python manage.py collectstatic --noinput'.
[2023-11-01 09:45:33] │        See traceback above for details.
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │        You may need to update application code to resolve this error.
[2023-11-01 09:45:33] │        Or, you can disable collectstatic for this application:
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │           $ heroku config:set DISABLE_COLLECTSTATIC=1
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │        https://devcenter.heroku.com/articles/django-assets
[2023-11-01 09:45:33] │ ERROR: failed to build: exit status 1
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │ For documentation on the buildpacks used to build your app, please see:
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │    Python  v2.234.4  https://do.co/apps-buildpack-python
[2023-11-01 09:45:33] │ 
[2023-11-01 09:45:33] │  ✘ build failed
Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

I have finally resolved the error - it came down to a misconfiguration in my environment variables.

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel