I'm a bit new to django and I'm trying to run collectstatic from the terminal (python manage.py collectstatic) in order to collect the static files in the S3 bucket but I getting the following error:
$ python manage.py collectstatic
C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\storages\backends\s3boto3.py:282: UserWarning: The default behavior of S3Boto3Storage is insecure and will change in django-storages 2.0. By default files and new buckets are saved with an ACL of 'public-read' (globally publicly readable). Version 2.0 will default to using the bucket's ACL. To opt into the new behavior set AWS_DEFAULT_ACL = None, otherwise to silence this warning explicitly set AWS_DEFAULT_ACL.
"The default behavior of S3Boto3Storage is insecure and will change "
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 "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
utility.execute()
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\core\management\__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 199, in handle
collected = self.collect()
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 124, in collect
handler(path, prefixed_path, storage)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 354, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 260, in delete_file
if self.storage.exists(prefixed_path):
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\storages\backends\s3boto3.py", line 532, in exists
self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\storages\backends\s3boto3.py", line 315, in connection
verify=self.verify,
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\boto3\session.py", line 389, in resource
aws_session_token=aws_session_token, config=config)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\boto3\session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\session.py", line 839, in create_client
client_config=config, api_version=api_version)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\client.py", line 86, in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\client.py", line 328, in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\args.py", line 47, in get_client_args
endpoint_url, is_secure, scoped_config)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\args.py", line 117, in compute_client_args
service_name, region_name, endpoint_url, is_secure)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\client.py", line 402, in resolve
service_name, region_name)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\regions.py", line 122, in construct_endpoint
partition, service_name, region_name)
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\regions.py", line 141, in _endpoint_for_partition
if self._region_match(partition, region_name):
File "C:\Users\Elena\Desktop\Curso\4th Module\ecommerce2\.venv\lib\site-packages\botocore\regions.py", line 159, in _region_match
return re.compile(partition['regionRegex']).match(region_name)
TypeError: expected string or bytes-like object
(.venv)
Steps that I have followed:
- I have installed django-storages and boto3
pip install django-storages
pip install boto3
I have added 'storages' to INSTALLED_APPS in settings.py
I have created 'custom_storage.py'
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class StaticStorage(S3Boto3Storage):
location = settings.STATICFILES_LOCATION
- I have added the following to settings.py
AWS_S3_OBJECT_PARAMETERS = {
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'CacheControl': 'max-age=94608000'
}
AWS_STORAGE_BUCKET_NAME = 'elena-ecommerce'
AWS_S3_REGION_NAME = 'eu-west-2',
AWS_ACCESS_KEY_ID = os.environ.get("AWS_SECRET_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'custom_storage.StaticStorage'
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,"static"),)
- And the I have run the following command:
python manage.py collectstatic