【问题标题】:I can't get AWS S3 to serve media files on my Django/Heroku App我无法让 AWS S3 在我的 Django/Heroku 应用程序上提供媒体文件
【发布时间】:2021-05-02 21:24:41
【问题描述】:

在这个问题上停留了一段时间。在我的 django 网站的管理页面中,我可以将照片与我的博客文章一起上传。照片将出现在我的 S3 存储桶中,但不会在博客文章中呈现。只是一个小照片图标。

遵循 simpleisbetterthancomplex 教程,并阅读了我能找到的所有相关的 stackoverflow 问题。谁能看到我错过了什么?

谢谢。

这是我的 settings.py:

AWS_ACCESS_KEY_ID = '**********'
AWS_SECRET_ACCESS_KEY = '*********'
AWS_STORAGE_BUCKET_NAME = 'matt-george-portfolio'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

AWS_LOCATION = 'static'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)

AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

STATIC_URL = '/static/'
MEDIA_URL = '/images/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

import django_heroku
django_heroku.settings(locals())

这是我的存储桶政策:

    {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::964481289861:user/my-user"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-bucket/*"
        },
        {
            "Sid": "AllObjectActions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::964481289861:user/my-user"
            },
            "Action": "s3:*Object",
            "Resource": "arn:aws:s3:::my-bucket/*"
        }
    ]
}

这是我的用户政策:

    {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:ListAllMyBuckets",
            "Resource": [
                "arn:aws:s3:::*"
            ]
        },
        {
            "Effect":"Allow",
            "Action":["s3:ListBucket","s3:GetBucketLocation"],
            "Resource":"arn:aws:s3:::my-bucket"
        },
        {
            "Effect":"Allow",
            "Action":[
            "s3:PutObject",
            "s3:PutObjectAcl",
            "s3:GetObject",
            "s3:GetObjectAcl",
            "s3:DeleteObject"
                ],
            "Resource":"arn:aws:s3:::my-bucket"
        }
    ]
}

【问题讨论】:

  • 您的最后一个屏幕截图似乎是 CORS 策略。您可以检查(或在此处添加)s3 存储桶策略和 Iam 用户或 Iam 角色的策略吗?
  • @BaluVyamajala 我添加了 IAM 政策。

标签: python django amazon-web-services amazon-s3 heroku


【解决方案1】:

想出了解决办法。问题不在于我的 settings.py 文件,也不是 IAM、CORS 或存储桶配置的问题。我最终遵循了本教程并将 Cloudfront 添加到我的存储桶中: https://www.ordinarycoders.com/blog/article/serve-django-static-and-media-files-in-production

我的问题的解决方案其实很简单。我用这样的标签调用照片:

{% for post in post_list %}
       <div class="img"><img src="{{ post.image }}"></div>
{% endfor %}

但显然,我忽略了一个事实,即如果图像来自存储桶,您需要以不同的方式调用图像。我在标签末尾添加了 .url,所以现在看起来像这样:

{% for post in post_list %}
          <div class="img"><img src="{{ post.image.url }}"></div>
{% endfor %}

现在我的 django 网站可以毫无问题地提供媒体文件。

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 2019-12-18
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2020-12-23
    相关资源
    最近更新 更多