为什么静态文件和图像在我的 django s3 存储桶项目中不起作用? [英] why static files and images are not working on my django s3 bucket project?

查看:22
本文介绍了为什么静态文件和图像在我的 django s3 存储桶项目中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在我的本地 django 项目上使用 s3 存储桶时它不起作用,在将静态文件和图像移动到 s3 存储桶后,它不会呈现其中任何一个.

while trying to use s3 bucket on my local django project it does not work, after moving the static files and images to the s3 bucket it is not rendering any of them.

我的项目结构是这样的

Project
   |-> app-name
   |-> static
   |      |-> img "folder of png images"
   |      |-> app-name
   |              |-> css "folder contain all css files"
   |              |-> js "folder contains all js files"
   |              |-> img "contains images used for styling"
   |-> project name
   |-> manage.py

settings.py中媒体、静态和s3存储桶设置是这样设置的"

in settings.py "media, static and s3 bucket settings are set like this"

STATIC_URL = '/static/'

MEDIA_URL = '/img/'

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

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


AWS_ACCESS_KEY_ID = '***************'
AWS_SECRET_ACCESS_KEY = '***************'
AWS_STORAGE_BUCKET_NAME = 'bucket-name'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None

AWS_S3_HOST = 's3.us-east-2.amazonaws.com'
AWS_S3_REGION_NAME = 'us-east-2'

AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

当我尝试检查元素以查看图像中的 src 或 css 链接时,我可以像这样看到它们https://bucket-name.s3.amazonaws.com/gallery/css/bootstrap.min.css"或图片https://bucket-name.s3.amazonaws.com/stockholm_Samit.jpg"

when I try to inspect element to see the src in images or links for css I can see them like this "https://bucket-name.s3.amazonaws.com/gallery/css/bootstrap.min.css" or for image "https://bucket-name.s3.amazonaws.com/stockholm_Samit.jpg"

你能帮我解决这个问题吗?

could you please help me with this?

推荐答案

如果您的存储桶名称不是bucket-name",请确保在行中指定您存储桶的存储桶名称:

If name of your bucket is not "bucket-name", make sure to specify bucket name of your bucket in line:

AWS_STORAGE_BUCKET_NAME = 'enter-your-bucket-name'

这篇关于为什么静态文件和图像在我的 django s3 存储桶项目中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆