0

我对 django 有点陌生,我正在尝试从终端(python manage.py collectstatic)运行 collectstatic 以收集 S3 存储桶中的静态文件,但出现以下错误:

$ 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 "

您已请求在设置中指定的目标位置收集静态文件。这将覆盖现有文件!你确定要这么做吗?

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) 

我遵循的步骤:

  1. 我已经安装了 django-storages 和 boto3
pip install django-storages
pip install boto3
  1. 我在 settings.py 中将“存储”添加到 INSTALLED_APPS

  2. 我创建了“custom_storage.py”

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage

class StaticStorage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION
  1. 我已将以下内容添加到 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"),)
  1. 我已经运行了以下命令:
python manage.py collectstatic
4

1 回答 1

0

问题出在您提到的设置文件中,因为eu-west-2AWS_S3_REGION_NAME = 'eu-west-2',后面有一个逗号,删除它就可以了。字符串后面的逗号创建一个像这样的元组,('eu-west-2',)而不是字符串,而重新匹配需要一个字符串

我刚刚提到了一个样本

>>> AWS_S3_REGION_NAME  = 'eu-west-2',
>>> print(AWS_S3_REGION_NAME)
('eu-west-2',)

>>> AWS_S3_REGION_NAME  = 'eu-west-2'
>>> print(AWS_S3_REGION_NAME)
eu-west-2

参考:https ://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

于 2019-09-08T20:26:27.727 回答