Django应用程序不会从AWS Bucket的媒体文件夹加载图像 [英] Django app does not load images from AWS bucket's media folder
问题描述
我正在使用 django-oscar
,并希望通过AWS S3提供我的静态文件.为了配置s3存储桶,我创建了一个名为 aws
的模块,其中包含 conf.py
和 utils.py
文件.
I'm using django-oscar
,and wanted to serve my static files with AWS S3.
To config my s3 bucket I've created a module called aws
with conf.py
and utils.py
files.
在我的网站上,当我将图像上传到产品时,它以正确的路径上传到了我的aws s3存储桶,但是在很短的时间后,该路径从 https://mybucketname.s3.amazonaws更改.com/media/cache/..../image.jpg
到 https://mybucketname.s3.amazonaws.com/cache/..../image.jpg
On my website when I upload an image to the product it gets uploaded well with the correct path to my aws s3 bucket, but then after very short time the path changes from https://mybucketname.s3.amazonaws.com/media/cache/..../image.jpg
to https://mybucketname.s3.amazonaws.com/cache/..../image.jpg
图像位于我的存储桶的 media
文件夹中.
The images are in the media
folder in my bucket.
我将我的Web应用托管在heroku上,静态文件可以正确提供,但问题出在媒体文件夹中.
I'm hosting my web app on heroku, the static files are served correctly but the issue happen in media folder.
这是我的代码-
from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
作为 static
和 media
是我的s3存储桶上的文件夹
as static
and media
are the folders on my s3 bucket
import datetime
AWS_ACCESS_KEY_ID = "xxx"
AWS_SECRET_ACCESS_KEY = "yyy"
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE =
'myproject.aws.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE =
'myproject.aws.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'mybucket-name'
S3DIRECT_REGION = 'us-east-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00
GMT")
AWS_HEADERS = {
'Expires': expires,
'Cache-Control': 'max-age=%d' %
(int(two_months.total_seconds()), ),
}
和我的settings.py我添加了这个
and my settings.py I added this
from myproject.aws.conf import *
我应该怎么做才能解决此问题?
What should I do to resolve this issue?
推荐答案
为Django应用配置的文件存储系统应该是实现 django.core.files.storage.Storage
[1]
The file storage system configured for your Django app should be a class that implements django.core.files.storage.Storage
[1]
storages.backends.s3boto3.S3Boto3Storage
已经实现了此存储接口. [2]
storages.backends.s3boto3.S3Boto3Storage
already implements this storage interface. [2]
将 utils.py
中的 StaticRootS3BotoStorage
设置为 lambda
, Storage
系统会使用适当的延迟进行实例化 location
值;但是存储类本身的 location
属性永远不会改变. [3]
Setting StaticRootS3BotoStorage
in utils.py
to a lambda
, the Storage
system is instantiated lazily with the proper location
value; but the location
attribute in the storage class itself is never changes. [3]
location = setting('AWS_LOCATION', '')
当项目设置更改时,Django清除存储实例的属性. [4] 以便在存储系统上解析 location
属性时,它可以有效地查找类属性1(上面代码段中显示了 location
值)因为实例中缺少 location
属性.
Django clears properties of storage instance when the project settings changes. [4] So that when the location
attribute is resolved on the storage system, it effectively looks up the class attribute one (location
value is shown in above snippet) because location
attribute is missing in the instance.
可以通过子类化 storages.backends.s3boto3.S3Boto3Storage
来解决这种情况.这样可以保证 location
的值永远不会改变,而与项目设置的改变无关.
This situation can be solved by subclassing storages.backends.s3boto3.S3Boto3Storage
instead. This guarantees that location
value never changes regardless of changes to project settings.
class StaticRootS3BotoStorage(S3Boto3Storage):
location = 'static'
class MediaRootS3BotoStorage(S3Boto3Storage):
location = 'media'
这篇关于Django应用程序不会从AWS Bucket的媒体文件夹加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!