上传到 s3 的静态文件不会在本地显示

Static files uploaded on s3 won't show locally

我刚刚在 S3 和 CloudFront 上上传了我的静态数据。 python manage.py collectstatic 成功。 我知道现在正确的行为是,当我在本地 运行 我的应用程序时,由于静态 url 已更改,所有静态将从 S3 运行ning。但是 none 这种情况发生了。 我试过 运行ning python manage.py runserver --nostatic :服务器 运行s 但我所有的资产都在本地消失了(即使我 运行 这个命令没有 --nostatic)。

这是我的 settings.py :

import os
from django.utils.translation import ugettext_lazy as _
import boto3

...

# AWS CloudFront
AWS_S3_REGION_NAME = 'us-east-2'  # e.g. us-east-2
AWS_ACCESS_KEY_ID = '*******'
AWS_SECRET_ACCESS_KEY = '*******'
AWS_S3_HOST = 's3-us-east-2.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=1209600, no-transform'
}

# static
BUCKET_STATIC_NAME = '*******'
CLOUDFRONT_STATIC_DOMAIN = '*******.cloudfront.net'

# media
BUCKET_MEDIA_NAME = '*******'
CLOUDFRONT_MEDIA_DOMAIN = '*******.cloudfront.net'

# storage
DEFAULT_FILE_STORAGE = 'elef.custom_storage.MediaS3BotoStorage'
STATICFILES_STORAGE = 'elef.custom_storage.CachedStaticS3BotoStorage'

MEDIA_URL = 'https://%s/' % CLOUDFRONT_MEDIA_DOMAIN
STATIC_URL = 'https://%s/' % CLOUDFRONT_STATIC_DOMAIN

AWS_DEFAULT_ACL = None

我也有一个 custom_storage.py :

from storages.backends.s3boto3 import S3Boto3Storage, SpooledTemporaryFile
import boto3
import os

from django.conf import settings
from django.contrib.staticfiles.storage import CachedFilesMixin


class CachedStaticS3BotoStorage(CachedFilesMixin, S3Boto3Storage):
    """
    S3BotoStorage backend which also saves a hashed copies of the files it saves.
    """
    bucket_name = settings.BUCKET_STATIC_NAME
    custom_domain = settings.CLOUDFRONT_STATIC_DOMAIN

    def _save_content(self, obj, content, parameters):
        content.seek(0, os.SEEK_SET)
        content_autoclose = SpooledTemporaryFile()
        content_autoclose.write(content.read())
        super(CachedStaticS3BotoStorage, self)._save_content(obj, content_autoclose, parameters)

        if not content_autoclose.closed:
            content_autoclose.close()


class MediaS3BotoStorage(S3Boto3Storage):
    """
    S3BotoStorage backend for media files.
    """
    bucket_name = settings.BUCKET_MEDIA_NAME
    custom_domain = settings.CLOUDFRONT_MEDIA_DOMAIN

这是运行宁python manage.py runserver(或python manage.py runserver --nostatic)时的服务器回溯:

^C(*****) djeustinezzz:***** justine_dev$ python manage.py runserver --nostatic
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

February 11, 2019 - 11:28:11
Django version 2.1.5, using settings 'elefmarket.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[11/Feb/2019 11:28:16] "GET / HTTP/1.1" 200 5775
Not Found: /static/images/logos/logo_bronze_2.png
[11/Feb/2019 11:28:16] "GET /static/images/logos/logo_bronze_2.png HTTP/1.1" 404 3082
Not Found: /static/images/pictos/btc-5.png
[11/Feb/2019 11:28:16] "GET /static/images/pictos/btc-5.png HTTP/1.1" 404 3061
Not Found: /static/images/pictos/margin-5.png
Not Found: /static/images/pictos/aws.png
[11/Feb/2019 11:28:16] "GET /static/images/pictos/aws.png HTTP/1.1" 404 3055
[11/Feb/2019 11:28:16] "GET /static/images/pictos/margin-5.png HTTP/1.1" 404 3070

不要介意未应用的迁移,我使用的是 nosql 数据库。

此外,这是我在 home.html 中调用静力学的方式:

{% load static %}
{% load i18n %}

<!DOCTYPE html>
<html>
    <head>
        <title>******</title>
        <link rel="shortcut icon" type="image/png" href="static/images/logos/elef.png"/>
        <link rel="stylesheet" href="{% static 'css/app.css' %}">
        <link rel="stylesheet" href="{% static 'css/home.css' %}">
    </head>
    <body></body>
</html>



您已将 MEDIA_URL 设置为指向您的 S3 域,而不是 STATIC_URL。

我已经找到问题了,希望能帮到别人。 我的 public 设置未在 S3 控制台中正确设置。访问被拒绝,所以我的服务器无法访问静态信息。 您需要检查权限: Amazon S3 > your-bucket > permissions > public access settings 然后,根据需要编辑设置。

希望这会有所帮助。