当我运行collectatic使用django将静态数据保存在S3存储桶中时发生TypeError

时间:2019-09-07 11:32:25

标签: django amazon-s3 typeerror boto3 collectstatic

我对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

  2. 的INSTALLED_APPS中添加了“存储”
  3. 我已经创建了'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

1 个答案:

答案 0 :(得分:0)

问题出在您提到的AWS_S3_REGION_NAME = 'eu-west-2',设置文件中,在 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