【发布时间】:2019-05-21 11:08:19
【问题描述】:
我正在使用django-oscar,并希望使用 AWS S3 提供我的静态文件。
为了配置我的 s3 存储桶,我创建了一个名为 aws 的模块,其中包含 conf.py 和 utils.py 文件。
在我的网站上,当我将图像上传到产品时,它会以正确的路径上传到我的 aws s3 存储桶,但在很短的时间后,路径从 https://mybucketname.s3.amazonaws.com/media/cache/..../image.jpg 变为 https://mybucketname.s3.amazonaws.com/cache/..../image.jpg
图片位于我存储桶的media 文件夹中。
我在 heroku 上托管我的网络应用程序,静态文件已正确提供,但问题发生在媒体文件夹中。
这是我的代码 -
utils.py 文件
from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
static 和 media 是我的 s3 存储桶上的文件夹
conf.py
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 我添加了这个
from myproject.aws.conf import *
我应该怎么做才能解决这个问题?
【问题讨论】:
标签: python django amazon-s3 django-oscar django-storage