0

部署失败并出现以下错误

  Traceback (most recent call last):
  File "/opt/python/ondeck/app/manage.py", line 21, in <module>
  main()
  File "/opt/python/ondeck/app/manage.py", line 17, in main
  execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
  utility.execute()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
  self.execute(*args, **cmd_options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
  output = self.handle(*args, **options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
  collected = self.collect()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
  handler(path, prefixed_path, storage)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 342, in copy_file
  if not self.delete_file(path, prefixed_path, source_storage):
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 249, in delete_file
  if self.storage.exists(prefixed_path):
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 524, in exists
  self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/client.py", line 272, in _api_call
  return self._make_api_call(operation_name, kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/client.py", line 549, in _make_api_call
  api_params, operation_model, context=request_context)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/client.py", line 595, in _convert_to_request_dict
  api_params, operation_model, context)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/client.py", line 627, in _emit_api_params
  params=api_params, model=operation_model, context=context)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/hooks.py", line 356, in emit
  return self._emitter.emit(aliased_event_name, **kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/hooks.py", line 228, in emit
  return self._emit(event_name, kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/hooks.py", line 211, in _emit
  response = handler(**kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/botocore/handlers.py", line 223, in validate_bucket_name
  if not VALID_BUCKET.search(bucket) and not VALID_S3_ARN.search(bucket):
  TypeError: expected string or bytes-like object
   (ElasticBeanstalk::ExternalInvocationError)

错误跟踪没有帮助我弄清楚这里到底是什么问题

设置.py

AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
AWS_S3_REGION_NAME = os.environ['AWS_S3_REGION_NAME']  # e.g. us-east-2
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']

# Tell django-storages the domain to use to refer to static files.
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

# Tell the staticfiles app to use S3Boto3 storage when writing the collected static files (when
# you run `collectstatic`).
# STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

DEFAULT_FILE_STORAGE = custom_storages.MediaStorage
STATICFILES_STORAGE = custom_storages.StaticStorage

STATICFILES_LOCATION = os.environ['STATICFILES_LOCATION']
MEDIAFILES_LOCATION = os.environ['MEDIAFILES_LOCATION']

STATIC_DIRECTORY = '/static/'
STATIC_URL = AWS_S3_CUSTOM_DOMAIN + STATIC_DIRECTORY

MEDIA_DIRECTORY = '/media/'
MEDIA_URL = AWS_S3_CUSTOM_DOMAIN + MEDIA_DIRECTORY

custom_storages.py

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


class StaticStorage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION
    default_acl = 'public-read'

class MediaStorage(S3Boto3Storage):
    location = settings.MEDIAFILES_LOCATION
    default_acl = 'private'
    file_overwrite = False

我尝试重新排列的顺序MEDIA_ROOTSTATIC_ROOT没有帮助。

当前的代码在我的 Mac 笔记本电脑上运行良好。

任何帮助解决这个问题都非常感谢。谢谢!

4

1 回答 1

0

我通过添加AWS_DEFAULT_ACL = None到 settings.py解决了这个问题

我认为 custom_storages.py 中的 default_acl 设置就足够了,但显然仅此还不够。只需将其添加到 settings.py 并再次部署它就像魔术一样。

于 2019-12-19T20:06:24.960 回答