簡體   English   中英

Collectstatic 失敗 - botocore.exceptions.ClientError:調用 HeadObject 操作時發生錯誤 (404):未找到

[英]Collectstatic failing - botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

我正在嘗試在部署到 AWS 的階段運行 collectstatic。 我收到以下錯誤,並且沒有文件被放置在存儲桶中:

Traceback (most recent call last):
  File "/home/hcc/.local/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 523, in _open
    f = S3Boto3StorageFile(name, mode, self)
  File "/home/hcc/.local/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 74, in __init__
    self.obj.load()
  File "/home/hcc/.local/lib/python3.6/site-packages/boto3/resources/factory.py", line 505, in do_action
    response = action(self, *args, **kwargs)
  File "/home/hcc/.local/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(*args, **params)
  File "/home/hcc/.local/lib/python3.6/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/hcc/.local/lib/python3.6/site-packages/botocore/client.py", line 635, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hcc/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/hcc/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hcc/.local/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/hcc/.local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 161, in handle
    if self.is_local_storage() and self.storage.location:
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 215, in is_local_storage
    return isinstance(self.storage, FileSystemStorage)
  File "/home/hcc/.local/lib/python3.6/site-packages/django/utils/functional.py", line 224, in inner
    self._setup()
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 501, in _setup
    self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 379, in __init__
    self.hashed_files = self.load_manifest()
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 389, in load_manifest
    content = self.read_manifest()
  File "/home/hcc/.local/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 383, in read_manifest
    with self.open(self.manifest_name) as manifest:
  File "/home/hcc/.local/lib/python3.6/site-packages/django/core/files/storage.py", line 36, in open
    return self._open(name, mode)
  File "/home/hcc/.local/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 526, in _open
    raise IOError('File does not exist: %s' % name)
OSError: File does not exist: static/staticfiles.json

我的static文件設置在settings.py中如下:

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

STATICFILES_DIRS = [join(BASE_DIR, "assets", "build")]

DEFAULT_FILE_STORAGE = "staticfiles.storage.MediaStorage"
STATICFILES_STORAGE = "staticfiles.storage.ManifestStaticFilesStorage"

STATIC_FILES_BUCKET = "stage-bucket"
STATIC_FILES_LOCATION = "static"

STATIC_ROOT = join(BASE_DIR, "static")
STATIC_URL = f"https://{STATIC_FILES_BUCKET}.s3.amazonaws.com/{STATIC_FILES_LOCATION}/"

MEDIA_FILES_BUCKET = ""
MEDIA_FILES_LOCATION = ""

MEDIA_ROOT = join(BASE_DIR, "media")
MEDIA_URL = "/media/"

我正在使用的自定義存儲類是:

from django.contrib.staticfiles.storage import ManifestFilesMixin
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage


class ManifestStaticFilesStorage(ManifestFilesMixin, S3Boto3Storage):
    bucket_name = settings.STATIC_FILES_BUCKET
    location = settings.STATIC_FILES_LOCATION


class MediaStorage(S3Boto3Storage):
    bucket_name = settings.MEDIA_FILES_BUCKET
    location = settings.MEDIA_FILES_LOCATION
    file_overwrite = False

Requirements.txt 包括:

Django==3.0.3
gunicorn==20.0.4

django-model-utils==4.0.0
django-storages==1.9.1
boto3==1.13.15
htmlmin==0.1.12

sentry-sdk==0.14.3
slackclient==2.5.0
structlog==18.2.0
ddtrace==0.31.0
colorama==0.3.9
json-logging-py==0.2

當我從ManifestFilesMixin class 中刪除ManifestStaticFilesStorage時,collectstatic 有效,但顯然不適用於散列文件或我需要的 staticfiles.json。

我花了好幾天時間試圖弄清楚這個問題,但我對可能是什么原因感到困惑。 任何信息,將不勝感激。

另一個有類似問題的人那里找到了答案。 似乎 S3Boto3Storage 拋出 IOError 但 ManifestFilesMixin 期待 FileNotFound。 這解決了我的問題,我現在在我的 S3 存儲桶中看到散列文件以及 staticfiles.json。

class ManifestStaticFilesStorage(ManifestFilesMixin, S3Boto3Storage):
    bucket_name = settings.STATIC_FILES_BUCKET
    location = settings.STATIC_FILES_LOCATION

    def read_manifest(self):
        try:
            return super(ManifestStaticFilesStorage,self).read_manifest()
        except IOError:
            return None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM