Question

Spaces Object Storage in Django Project Getting errorraise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An

1 file changed, 2 insertions(+), 1 deletion(-)
(env) root@djangoproject:~/ebook# python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "/root/ebook/manage.py", line 22, in <module>
    main()
  File "/root/ebook/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
                ^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/storages/backends/s3.py", line 514, in exists
    self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
  File "/root/ebook/env/lib/python3.11/site-packages/botocore/client.py", line 553, in _api_call
    return self._make_api_call(operation_name, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/botocore/client.py", line 1009, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
(env) root@djangoproject:~/ebook# cd /ete/nginx/sites-available/
-bash: cd: /ete/nginx/sites-available/: No such file or directory
(env) root@djangoproject:~/ebook# tmux
[exited]
(env) root@djangoproject:~/ebook# git pull origin master
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 419 bytes | 209.00 KiB/s, done.
From https://bitbucket.org/veena03253/ebook
 * branch            master     -> FETCH_HEAD
   9aa5f4a..93a425f  master     -> origin/master
Updating 9aa5f4a..93a425f
Fast-forward
 ebook/settings.py | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
(env) root@djangoproject:~/ebook# python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "/root/ebook/manage.py", line 22, in <module>
    main()
  File "/root/ebook/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/ebook/env/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
                ^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/storages/backends/s3.py", line 514, in exists
    self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
  File "/root/ebook/env/lib/python3.11/site-packages/botocore/client.py", line 553, in _api_call
    return self._make_api_call(operation_name, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/ebook/env/lib/python3.11/site-packages/botocore/client.py", line 1009, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

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.

Bobby Iliev
Site Moderator
Site Moderator badge
January 8, 2024

Hello,

The error you’re encountering with botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden during python manage.py collectstatic in a Django project connected to DigitalOcean Spaces indicates a configuration issue. In the context of DigitalOcean Spaces, this usually involves incorrect configuration of the Spaces access keys or Spaces URL, or permission settings in your Django application.

Here are steps to troubleshoot and resolve the issue:

  1. Check DigitalOcean Spaces Access Keys:

    • Ensure that your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are correctly set in your Django settings. These keys should correspond to a DigitalOcean API token with sufficient permissions to access the Space.
  2. Verify Space Name and Endpoint:

    • Confirm that AWS_STORAGE_BUCKET_NAME is set to your DigitalOcean Space’s name and that AWS_S3_ENDPOINT_URL points to the correct endpoint URL for your Space.
  3. CORS Settings:

    • On the DigitalOcean Spaces dashboard, check if the CORS settings for your Space are configured correctly. Misconfigured CORS settings can lead to permission issues.

Here is also a very good step-by-step article that might be helpful:

https://www.digitalocean.com/community/tutorials/how-to-set-up-object-storage-with-django

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

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