简体   繁体   English

在 Elastic Beanstalk Aws React Django 上托管站点时如何从单独的 S3 存储桶加载 static 文件

[英]How to load static files from separate S3 bucket when hosting site on Elastic Beanstalk Aws React Django

Setup: React+Django hosted on Elastic Beanstalk.设置:在 Elastic Beanstalk 上托管的 React+Django。 Static files hosted on separate S3 Bucket. Static 文件托管在单独的 S3 存储桶上。

I'm trying to load an image using src="/static/images/logo.png" .我正在尝试使用src="/static/images/logo.png"加载图像。

In development it works perfectly, but in production it sends a request to XXX.elasticbeanstalk.com/static/images/logo.png while it should be requesting Bucket.amazonaws.com/static/images/logo.png .在开发中它工作得很好,但在生产中它会向XXX.elasticbeanstalk.com/static/images/logo.png发送请求,而它应该请求Bucket.amazonaws.com/static/images/logo.png

Meanwhile the user uploaded media is working perfectly for both POST and GET requests, images are stored and fetched from the Bucket /media/ path.同时,用户上传的媒体对于 POST 和 GET 请求都非常有效,图像是从 Bucket /media/路径存储和获取的。 I want to avoid conditionally coding the absolute url path depending on the environment.我想避免根据环境有条件地编码绝对 url 路径。

I have a django production_settings.py file:我有一个 django production_settings.py文件:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

AWS_STORAGE_BUCKET_NAME = '****************'
AWS_S3_REGION_NAME = '****************' 
AWS_ACCESS_KEY_ID = '****************'
AWS_SECRET_ACCESS_KEY = '****************'


AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME



STATICFILES_LOCATION = 'static'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'

MEDIAFILES_LOCATION = 'media'
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

custom_storages.py : custom_storages.py

from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage


class StaticStorage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3Boto3Storage):
    location = settings.MEDIAFILES_LOCATION

Thanks谢谢

It seems like your STATIC_URL set incorrectly.似乎您的STATIC_URL设置不正确。 It refers to your web app project path instead of S3 bucket URL.它指的是您的 web 应用程序项目路径,而不是 S3 存储桶 URL。 You can try using this below:您可以尝试在下面使用它:

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

What you can try..你可以尝试什么..

1. Make sure you have the right S3 Permissions and Policy in place. 1. 确保您拥有正确的 S3 权限和策略。 Access 'Policy' via permissions tab in AWS S3 console for your specific bucket.通过 AWS S3 控制台中的权限选项卡访问特定存储桶的“策略”。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<your_bucket_name>/*"
        },
        {
            "Sid": "Statement2",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::<your_bucket_name>/images/"
        }
    ]
}

2. Install WhiteNoiseMiddleware & django-storages in your project environment. 2. 在你的项目环境中安装 WhiteNoiseMiddleware & django-storages。

pip install whitenoise
pip install django-storages

3. Add the following to MIDDLEWARE= in settings.py 3. 在settings.py中将以下内容添加到MIDDLEWARE=

'whitenoise.middleware.WhiteNoiseMiddleware',

4. Following additions in settings.py are required to handle URLs from S3 correctly. 4. 需要在settings.py中添加以下内容才能正确处理来自 S3 的 URL。 The handling is done by django middleware & django-storages automatically处理由 django 中间件和 django-storages 自动完成

STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % os.environ['BUCKET_NAME']
AWS_ACCESS_KEY_ID = os.environ['AWS_KEY']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_ACC_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['BUCKET_NAME']
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

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

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

5. For aiming uploads into a precise S3 Bucket folder. 5. 用于瞄准上传到精确的 S3 Bucket 文件夹。 (Additional) (额外的)

In setting.py set media root:在 setting.py 中设置媒体根目录:

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

In models.py use ImageFiled and add upload_to= takes in a folder name and creates it with the first upload::在 models.py 中使用ImageFiled并添加upload_to=获取文件夹名称并在第一次上传时创建它:

image_variable = models.ImageField(null=True, default="{default_filename)", upload_to='uploads/') 

Reference: django-storages , whiteNoiseMiddelware , S3 Access Troubleshooting参考: django- storages , whiteNoiseMiddelware , S3 Access Troubleshooting

You have already extended S3Boto3Storage and you could see there is multiple parameters, you could set additional env key for static storage class:您已经扩展了 S3Boto3Storage ,您可以看到有多个参数,您可以为 static 存储 class 设置额外的 env 密钥:

class StaticStorage(S3Boto3Storage):
    location = settings.STATICFILES_LOCATION
    custom_domain = setting('AWS_S3_CUSTOM_DOMAIN_STATIC')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在aws弹性beanstalk中托管的django应用程序的s3桶中保存django日志文件 - How to save django log file in s3 bucket of a django app hosted in aws elastic beanstalk Elastic Beanstalk Aws Django static 和媒体文件 - Elastic Beanstalk Aws Django static and media files Django 和 S3 存储桶 aws 管理员 Static 文件 - Django and S3 Bucket aws Admin Static files Django 使用 Aws S3 存储桶获取 static 文件 - Django using Aws S3 bucket to get static files AWS S3 存储桶上 static 文件的 Django 问题 - Django problems with static files on AWS S3 Bucket 如何在 AWS Elastic Beanstalk 上使用 Nginx、React、Webpack、Gunicorn、PostgreSQL、Django 和 DRF 部署应用程序? 如何使用此应用程序处理静态文件? - How to deploy app with Nginx, React, Webpack, Gunicorn, PostgreSQL, Django & DRF on AWS Elastic Beanstalk? How to handle static files with this app? collectstatic 命令在部署到弹性 beantalk 时失败 | 使用 django-storages 在 S3 上存储静态文件 - collectstatic command fails while deploying to elastic beanstalk | using django-storages for storing static files on S3 将图像从 aws s3 存储桶加载到 django 应用程序 - Load an image from aws s3 bucket to django application 从S3存储桶Django导入静态文件时出错 - Error importing static files from S3 Bucket Django AWS Elastic Beanstalk和Django 1.5:S3的Collectstatic失败 - AWS Elastic Beanstalk & Django 1.5: Collectstatic to S3 fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM