What is the right way to use S3 as a storage backend?

740 views
Skip to first unread message

Kurtis

unread,
Oct 5, 2011, 9:48:06 AM10/5/11
to Django users
I want to use Django-storages w/ Amazon S3 as my storage backend. Not
only do I want to use it for media files (user uploaded files) but I
also want to use it with the collectstatic functionality. Following
the Django-storages S3 documentation, I modified my settings.py
accordingly.

Now, I ran "manage.py collectstatic" and it uploaded the media to my
bucket without any problems. I looked in my S3 control panel and all
of the files are there.

This is a stub application so the only static files included were the
Admin and Admin doc files. However, when I access the admin section of
my Django Application, the CSS is obviously missing. I looked at the
source and it doesn't appear to be grabbing the bucket URL. I'm
guessing there's something I'm missing but I'm not sure exactly what
it is. I could specify the static_files and media_files URLs manually
but from reading the documentation, I had the feeling this would be
taken care of automagically. I'm assuming this is not the case.

Code and configuration as follows:
----------------------------------------------
# in settings.py
# Amazon Storages Backend
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = '<secret>'
AWS_SECRET_ACCESS_KEY = '<secret>'
AWS_STORAGE_BUCKET_NAME = 'kurtis'
from S3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

# top of HTML Source Code of Admin page.
<title>Site administration | Django site admin</title>
<link rel="stylesheet" type="text/css" href="/static/admin/css/
base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/
dashboard.css" />
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/static/
admin/css/ie.css" /><![endif]-->

<script type="text/javascript">window.__admin_media_prefix__ = "/
static/admin/";</script>

I'm going to try setting the Static Files URL manually in hopes that
it might take care of this problem. Then I'll test out setting up an
Image Model and hope that it does pull the URL automagically. In the
mean time, if anyone has recommendations on this that would be
awesome.

Thanks!

Shawn Milochik

unread,
Oct 5, 2011, 9:54:06 AM10/5/11
to django...@googlegroups.com
Could it be the ADMIN_MEDIA_PREFIX setting?

Kurtis

unread,
Oct 5, 2011, 10:01:53 AM10/5/11
to Django users
I'll give that a shot. I pushed my bucket to the CDN and tried to set
that URL manually using:

STATIC_URL = 'http://kurtis.s3.amazonaws.com/'

Unfortunately, it's still trying to use /static/... Hopefully your
idea will work. Thanks!

Kurtis

unread,
Oct 5, 2011, 10:07:16 AM10/5/11
to Django users
Okay, two things.

1. I saw I was potentionally using the wrong URL. I believe it should
be my CDN URL. Correct me if I'm wrong but the right URL *should* be
along the lines of: http://dw2u7t9lse636.cloudfront.net/

2. I included that ADMIN_MEDIA_PREFIX setting and it did update the
URLs for Admin media. Thanks!

Unfortunately, and this is probably an issue with Amazon -- I get a an
access denied error when I try to access my files. For example:

http://dw2u7t9lse636.cloudfront.net/css/base.css

Kurtis

unread,
Oct 5, 2011, 10:25:42 AM10/5/11
to Django users
Okay I figured it out. Another slip of mind on my part :) I had to
change my CDN url to include '/admin/' since that's the subdirectory
my admin media is uploaded to.

creecode

unread,
Oct 5, 2011, 10:36:19 AM10/5/11
to django...@googlegroups.com
Hello Kurtis,


On Wednesday, October 5, 2011 7:07:16 AM UTC-7, Kurtis wrote:
 
Unfortunately, and this is probably an issue with Amazon -- I get a an
access denied error when I try to access my files. For example:

http://dw2u7t9lse636.cloudfront.net/css/base.css

That response is most likely a setup issue with your permissions.  You need to make sure that everything along the path (bucket, folders, file) to your file has public access set for everyone to access via a browser.

Toodle-loooooooooooo........
creecode

Andre Terra

unread,
Oct 5, 2011, 2:32:49 PM10/5/11
to django...@googlegroups.com
I may be dreaming, but IIRC ADMIN_MEDIA_PREFIX is on its way to deprecation, so some googling might be in order.

Cheers,
AT


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Micky Hulse

unread,
Oct 5, 2011, 2:48:45 PM10/5/11
to django...@googlegroups.com
Not sure if this will be of any help, but I switched from
django-storages to this:

<https://github.com/kylemacfarlane/django-cuddlybuddly-storage-s3>

I switched mostly because SORL was not working well with my S3
buckets... CuddlyBuddly has this:

<https://github.com/kylemacfarlane/django-cuddlybuddly-thumbnail>

Not sure if CuddlyBuddly has more features... I have been very satisfied.

Anyway, just FYI.

Reply all
Reply to author
Forward
0 new messages