Heroku 内部服务器错误。参数验证错误。无效的存储桶名称“”:Python/Django

Heroku Internal Server Error. ParamValidationError. Invalid bucket name "": Python/Django

这里绝对是初学者,将一个小网站部署到 Heroku 简直是一场噩梦。为了达到这一点,我已经解决了许多问题,但现在我似乎又碰壁了。我已将此作为 Heroku 的支持票提出,但除非这是他们方面的问题,否则我怀疑他们是否会给我一个明确的答案。

我可以成功部署到 Heroku,但是,当打开我的 Web 应用程序时,我只能看到 "Internal Server Error" 字样代替我的网站。我的 Sentry 日志收到 issue/error,如下所示:

MESSAGE
ParamValidationError: Parameter validation failed:
Invalid bucket name "": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"
EXCEPTION(most recent call first)App OnlyFullRaw
ParamValidationError
Parameter validation failed:
Invalid bucket name "": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"
storages/backends/s3boto3.py in exists at line 478
    def exists(self, name):
        name = self._normalize_name(self._clean_name(name))
        if self.entries:
            return name in self.entries
        try:
            self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
            return True
        except ClientError:
            return False
    def listdir(self, name):
name    
'CACHE/css/cf136be60f95.css'
self    
<storages.backends.s3boto3.S3Boto3Storage object at 0x7fbe4b7f4e80>
compressor/base.py in output_file at line 321
        """
        The output method that saves the content to a file and renders
        the appropriate template with the file's URL.
        """
        new_filepath = self.get_filepath(content, basename=basename)
        if not self.storage.exists(new_filepath) or forced:
            self.storage.save(new_filepath, ContentFile(content.encode(self.charset)))
        url = mark_safe(self.storage.url(new_filepath))
        return self.render_output(mode, {"url": url})
    def output_inline(self, mode, content, forced=False, basename=None):
basename    
None
content 
'/*!
 * Material Design for Bootstrap 4
 * Version: MDB Free 4.4.5
 *
 *
 * Copyright: Material Design for Bootstrap
 * https://mdbootstrap.com/
 *
 * Read the license: https://mdbootstrap.com/license/
 *
 *
 * Documentation: https://mdbootstrap.com/
 *
 * Getting started: https://mdbootstrap.com/getting-started/
 *
 * Tutorials: https://mdbootstrap.com/bootstrap-tutorial/
 *
 * Templates: https://'
forced  
False
mode    
'file'
new_filepath    
'CACHE/css/cf136be60f95.css'
self    
<compressor.css.CssCompressor object at 0x7fbe4aadf240>
compressor/base.py in handle_output at line 310
    def handle_output(self, mode, content, forced, basename=None):
        # Then check for the appropriate output method and call it
        output_func = getattr(self, "output_%s" % mode, None)
        if callable(output_func):
            return output_func(mode, content, forced, basename)
        # Total failure, raise a general exception
        raise CompressorError(
            "Couldn't find output method for mode '%s'" % mode)
    def output_file(self, mode, content, forced=False, basename=None):
basename    
None
content 
'/*!
 * Material Design for Bootstrap 4
 * Version: MDB Free 4.4.5
 *
 *
 * Copyright: Material Design for Bootstrap
 * https://mdbootstrap.com/
 *
 * Read the license: https://mdbootstrap.com/license/
 *
 *
 * Documentation: https://mdbootstrap.com/
 *
 * Getting started: https://mdbootstrap.com/getting-started/
 *
 * Tutorials: https://mdbootstrap.com/bootstrap-tutorial/
 *
 * Templates: https://'
forced  
False
mode    
'file'
output_func 
<bound method Compressor.output_file of <compressor.css.CssCompressor object at 0x7fbe4aadf240>>
self    
<compressor.css.CssCompressor object at 0x7fbe4aadf240>
compressor/base.py in output at line 302
        if not output:
            return ''
        if settings.COMPRESS_ENABLED or forced:
            filtered_output = self.filter_output(output)
            return self.handle_output(mode, filtered_output, forced)
        return output
    def handle_output(self, mode, content, forced, basename=None):
        # Then check for the appropriate output method and call it
filtered_output 
'/*!
 * Material Design for Bootstrap 4
 * Version: MDB Free 4.4.5
 *
 *
 * Copyright: Material Design for Bootstrap
 * https://mdbootstrap.com/
 *
 * Read the license: https://mdbootstrap.com/license/
 *
 *
 * Documentation: https://mdbootstrap.com/
 *
 * Getting started: https://mdbootstrap.com/getting-started/
 *
 * Tutorials: https://mdbootstrap.com/bootstrap-tutorial/
 *
 * Templates: https://'
forced  
False
mode    
'file'
output  
'/*!
 * Material Design for Bootstrap 4
 * Version: MDB Free 4.4.5
 *
 *
 * Copyright: Material Design for Bootstrap
 * https://mdbootstrap.com/
 *
 * Read the license: https://mdbootstrap.com/license/
 *
 *
 * Documentation: https://mdbootstrap.com/
 *
 * Getting started: https://mdbootstrap.com/getting-started/
 *
 * Tutorials: https://mdbootstrap.com/bootstrap-tutorial/
 *
 * Templates: https://'
self    
<compressor.css.CssCompressor object at 0x7fbe4aadf240>
compressor/css.py in output at line 51
                ret = []
                for media, subnode in self.media_nodes:
                    subnode.extra_context.update({'media': media})
                    ret.append(subnode.output(*args, **kwargs))
                return ''.join(ret)
        return super(CssCompressor, self).output(*args, **kwargs)
__class__   
<class 'compressor.css.CssCompressor'>
args    
[
'file'
]
kwargs  
{
'forced': False
}
self    
<compressor.css.CssCompressor object at 0x7fbe4aadf240>
compressor/css.py in output at line 49
                    ret.append(subnode.output(*args, **kwargs))
compressor/templatetags/compress.py in render_compressed at line 107
        rendered_output = compressor.output(mode, forced=forced)
compressor/templatetags/compress.py in render at line 131
        return self.render_compressed(context, self.kind, self.mode, forced=forced)
Called from: django/template/base.py in render_annotated

我使用 django-cookiecuttter 作为我网站的基本模板。就在昨天,我阅读了针对 django-cookiecutter 针对另一个 Internal Server Error issue 提出的单独问题,我希望它也能帮助我(即使细节看起来不同),但我已经尝试从 [= 中删除 "min" 41=] 文件没有成功。

由于错误消息提到了存储和压缩器,这可能是 Django-compressor 相关或 whitenoise(我都安装了)?我已经研究过尝试根据说明 here 进行离线编译,但这可能已经过时了,因为我的网站在本地运行良好,如果我能提供帮助,我想避免不必要的编码。将不胜感激。

我正在使用 windows 与 Python 3.6.2 和 Django 2.0.2。我的生产静态设置如下:

# Static Assets
# ------------------------
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# COMPRESSOR
# ------------------------------------------------------------------------------
COMPRESS_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
COMPRESS_URL = STATIC_URL
COMPRESS_ENABLED = env.bool('COMPRESS_ENABLED', default=True)

提前致谢。

更新:

我仍然看到内部服务器错误,但是,我确定这与我设置存储桶的方式有关。我已按照此 tutorial 添加 IAM 组和策略,但我一定是做错了什么,因为我收到一条配置不正确的错误消息,其中包含以下详细信息:

Client error:An error occurred (403) when calling the HeadBucket operation: Forbidden

我在 AWS 中的政策如下(我已经尝试过几次迭代):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:HeadBucket",
                "s3:ListObjects"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::mybucket/*",
                "arn:aws:s3:::mybucket"
            ]
        }
    ]
}

我很快就 运行 脱掉了头发,因为我似乎正在以惊人的速度拔掉它。毫无疑问,这又是一个小学生的错误,非常感谢您抽出时间来审阅。

错误消息表明您有一个无效的 AWS S3 存储桶名称:Invalid bucket name ""。空名称无效。

在cookiecutter-django中由this setting定义。

您需要在 AWS 中创建一个 S3 存储桶并在 Heroku 上设置环境变量DJANGO_AWS_STORAGE_BUCKET_NAME

heroku config:set DJANGO_AWS_STORAGE_BUCKET_NAME='the-bucket-name'

编辑:

我不确定 Whitenoise + django-compressor + Heroku 有多少 cookiecutter-django 是最新的,但是 django-compressor 的回购中有 an issue opened可能很有趣。

回答我的 "Update" 第二个问题。我从头到尾再次浏览了 AWS 教程,虽然我认为我做的和以前完全一样,但这次它似乎起作用了,因为我现在遇到了另一个内部服务器错误 - 哦,快乐。我能想到的唯一感觉不同的是将策略分配给组,所以可能是我以前做的方式,我没能 link 它。

在再次打扰大家之前,我会继续寻找这个问题的答案,但是看到我很快回来请不要感到惊讶...