Question

botocore.exceptions.ClientError: An error occurred (InvalidArgument) when calling the PutObject operation: Unknown

I was following a tutorial by digital ocean on linking spaces with my django project,

I am currently getting this error:

  File "G:\Projects\Python\Django\laterevision\src\manage.py", line 22, in <module>
    main()
  File "G:\Projects\Python\Django\laterevision\src\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\core\management\base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 209, in handle
    collected = self.collect()
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 378, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\django\core\files\storage.py", line 56, in save
    name = self._save(name, content)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\storages\backends\s3boto3.py", line 457, in _save
    obj.upload_fileobj(content, ExtraArgs=params, Config=self._transfer_config)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\boto3\s3\inject.py", line 725, in object_upload_fileobj
    return self.meta.client.upload_fileobj(
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\boto3\s3\inject.py", line 636, in upload_fileobj
    return future.result()
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\s3transfer\futures.py", line 103, in result
    return self._coordinator.result()
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\s3transfer\futures.py", line 266, in result
    raise self._exception
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\s3transfer\tasks.py", line 139, in __call__
    return self._execute_main(kwargs)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\s3transfer\tasks.py", line 162, in _execute_main
    return_value = self._main(**kwargs)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\s3transfer\upload.py", line 758, in _main
    client.put_object(Bucket=bucket, Key=key, Body=body, **extra_args)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\botocore\client.py", line 514, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "G:\Projects\Python\Django\laterevision\venv\lib\site-packages\botocore\client.py", line 938, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidArgument) when calling the PutObject operation: Unknown

My settings for spaces look like this:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

AWS_ACCESS_KEY_ID = 'XXX' 
AWS_SECRET_ACCESS_KEY = 'XXXXXXX'

AWS_STORAGE_BUCKET_NAME = 'lrev'
AWS_S3_ENDPOINT_URL = 'https://fra1.digitaloceanspaces.com'
# I enabled the CDN, so you get a custom domain. Use the end point in the AWS_S3_CUSTOM_DOMAIN setting. 
AWS_S3_CUSTOM_DOMAIN = 'lrev.fra1.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

AWS_DEFAULT_ACL = 'x-amz-acl'

STATICFILES_STORAGE = 'custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

# Use AWS_S3_ENDPOINT_URL here if you haven't enabled the CDN and got a custom domain. 
STATIC_URL = '{}/{}/'.format(AWS_S3_CUSTOM_DOMAIN, 'static')
STATIC_ROOT = 'static/'

MEDIA_URL = '{}/{}/'.format(AWS_S3_CUSTOM_DOMAIN, 'media')
MEDIA_ROOT = 'media/'

and custom_storages looks like this:

from storages.backends.s3boto3 import S3Boto3Storage

class StaticStorage(S3Boto3Storage):
	bucket_name = 'lrev'
	location = 'static'

class MediaStorage(S3Boto3Storage):
	bucket_name = 'lrev'
	location = 'media'

Any help would be appriciated!


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
October 21, 2022
Accepted Answer

Hi there,

I believe that the problem might be the following:

AWS_S3_CUSTOM_DOMAIN = 'lrev.fra1.digitaloceanspaces.com'

I think that lrev.fra1.digitaloceanspaces.com is not a valid endpoint.

I would recommend using the following:

...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

AWS_ACCESS_KEY_ID = 'your-spaces-access-key'
AWS_SECRET_ACCESS_KEY = 'your-spaces-secret-access-key'
AWS_STORAGE_BUCKET_NAME = 'your-storage-bucket-name'
AWS_S3_ENDPOINT_URL = 'https://nyc3.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'your-spaces-files-folder'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'mysite/static'),
]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

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