当我运行Collectic以使用django将静态数据保存在S3存储桶中时出现TypeError

当我运行Collectic以使用django将静态数据保存在S3存储桶中时出现TypeError,django,amazon-s3,typeerror,boto3,collectstatic,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: T

我对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) 
  • 我已在settings.py中将“存储”添加到已安装的应用程序中

  • 我已经创建了“custom_storage.py”

  • 我已将以下内容添加到settings.py
  • 我已经运行了以下命令:

  • 问题出在您提到的设置文件中,
    AWS\u S3\u REGION\u 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
    

    参考:

    非常感谢Sajeer Noohukannu!删除逗号解决了问题:)
    from django.conf import settings
    from storages.backends.s3boto3 import S3Boto3Storage
    
    class StaticStorage(S3Boto3Storage):
        location = settings.STATICFILES_LOCATION
    
       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"),)
    
    python manage.py collectstatic
    
    >>> 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