从S3提供Django的静态和媒体文件

时间:2023-02-11 23:07:29

I'm having a weird problem and I have no idea what's causing it. Here's my current config (using Heroku):

我有一个奇怪的问题,我不知道是什么导致它。这是我当前的配置(使用Heroku):

MEDIA_URL = '/media/'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, '../static'),
)

#Determine if local or deployed.
if 'DATABASE_URL' in os.environ:
    STATICFILES_STORAGE     = 'storages.backends.s3boto.S3BotoStorage'
    AWS_ACCESS_KEY_ID       = 'A...A' 
    AWS_SECRET_ACCESS_KEY   = '...R' 
    DEFAULT_FILE_STORAGE    = 'storages.backends.s3boto.S3BotoStorage'

    # PRODUCTION OR STAGING
    if 'PROD' in os.environ:
        AWS_STORAGE_BUCKET_NAME = 's3.project.com'
        STATIC_URL = "https://s3.amazonaws.com/project/"
    else:
        AWS_STORAGE_BUCKET_NAME = 'project-staging'
        STATIC_URL = "https://s3.amazonaws.com/project-staging/"
else:
    STATIC_ROOT = ''

The thing is, static files are working fine, the links look like this: https://s3.amazonaws.com/project-staging/img/homepage_left.png.

问题是,静态文件工作正常,链接如下所示:https://s3.amazonaws.com/project-staging/img/homepage_left.png。

First problem (Admin half broken):

第一个问题(Admin半破):

The admin is half working. CSS files are served fine: https://project-staging.s3.amazonaws.com/admin/js/core.js?Signature=asdasdasd%3D&Expires=1352430664&AWSAccessKeyId=asdasdasA but the all the images are broken:

管理员工作一半。 CSS文件很好:https://project-staging.s3.amazonaws.com/admin/js/core.js?Signature = asdasdasd%3D&Expires = 1352430664&AWSAccessKeyId = asdasdasA但所有图像都被破坏了:

https://project-staging.s3.amazonaws.com/admin?Signature=%2asdasdad%3D&Expires=1352430664&AWSAccessKeyId=asdasdaimg/icon_calendar.gif

I don't know why the Signature and AWSAccesskey are there :(. If I remove them (by hand) the URL works fine!

我不知道为什么Signature和AWSAccesskey在那里:(。如果我(手动)删除它们,URL工作正常!

Second problem (uploaded media via ImageField/FileField):

第二个问题(通过ImageField / FileField上传的媒体):

I'm using {{MEDIA_URL}}{{object.flyer}}, it translates to /media/media/flyers/poster_1.png, how do I point it out to s3? It should be like this: https://project-staging.s3.amazonaws.com/media/flyers/poster_1.png

我正在使用{{MEDIA_URL}} {{object.flyer}},它转换为/media/media/flyers/poster_1.png,如何将其指向s3?它应该是这样的:https://project-staging.s3.amazonaws.com/media/flyers/poster_1.png

2 个解决方案

#1


0  

Answer to your second question: you need to set MEDIA_URL = https://project-staging.s3.amazonaws.com/media/ just like you're doing for STATIC_URL

回答你的第二个问题:你需要设置MEDIA_URL = https://project-staging.s3.amazonaws.com/media/,就像你正在为STATIC_URL做的那样

#2


0  

For your first question, I think you need to do:

对于你的第一个问题,我认为你需要这样做:

AWS_QUERYSTRING_AUTH = False

#1


0  

Answer to your second question: you need to set MEDIA_URL = https://project-staging.s3.amazonaws.com/media/ just like you're doing for STATIC_URL

回答你的第二个问题:你需要设置MEDIA_URL = https://project-staging.s3.amazonaws.com/media/,就像你正在为STATIC_URL做的那样

#2


0  

For your first question, I think you need to do:

对于你的第一个问题,我认为你需要这样做:

AWS_QUERYSTRING_AUTH = False