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

时间:2019-02-11 10:31:17

标签: django python-3.x amazon-web-services amazon-s3 amazon-cloudfront

我刚刚在S3和CloudFront上上传了静态函数。 python manage.py collectstatic成功。 我现在知道正确的行为是,当我在本地运行我的应用程序时,所有静态变量都将从S3运行,因为静态URL已更改。但是这些都没有发生。 我尝试运行python manage.py runserver --nostatic:服务器正在运行,但是我的所有资产都在本地使用(即使我在没有--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>



2 个答案:

答案 0 :(得分:0)

您已将MEDIA_URL设置为指向您的S3域,但未指向STATIC_URL。

答案 1 :(得分:0)

我发现了这个问题,如果它可以帮助其他人。 我在S3控制台中未正确设置公共设置。访问被拒绝,因此我的服务器无法访问静态信息。 您需要检查权限: Amazon S3> your-bucket> permissions> public access settings,然后根据需要编辑设置。

希望这会有所帮助。