I can't figure out how to set up django-storages. All of the directions seem to be incomplete or something.
I've tried: http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/ and a couple others that I cannot find now.
By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you're using these defaults. However, Django provides ways to write custom file storage systems that allow you to completely customize where and how Django stores files.
Building a simple Django Rest API application Execute the commands below to set up the project. Add the code snippet below to urls.py file in the dropboxer project directory. Create serializers.py and urls.py files in the uploader app. In models.py file, we create a simple model that represents a single file.
This is a setup checklist that I made for my colleagues.
settings_local.py
.Still in IAM, set user access permissions:
{
"Version": "2013-08-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": ["*"]
}
]
}
This policy allows access to all buckets to the user group:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:ListAllMyBuckets"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::*"
},
{
"Action": [
"cloudfront:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
Now, the bucket is set, the users have access to it. You can try setting and testing access to the bucket from Django.
Install the following packages:
django-storages==1.1.8
boto==2.9.7
I added this code to settings_local.py
to not expose it to those who view commits:
USE_AMAZON = False # Set this to True when ready
STATIC_URL = 'your-bucket-s3-url'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
This code goes to settings.py
:
from settings_local import *
if USE_AMAZON:
BASE_MEDIA_URL = 'static'
MEDIA_URL = '/media/'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATIC_ROOT = os.path.join(settings.BASE_PATH, 'static')
MEDIA_ROOT = os.path.join(settings.BASE_PATH, 'old_media')
STATICFILES_DIRS = (
('img', os.path.join(STATIC_ROOT, 'img'),
('js', os.path.join(STATIC_ROOT, 'js'),
)
INSTALLED_APPS += (
'django.contrib.staticfiles',
'storages',
)
Go to AWS S3 section and get the url for your bucket, paste it into settings files, set STATIC_URL
accordingly.
Paste keys from credentials into settings_local.py. Now Django should be able to upload static files to the storage.
Run this command:
$ uenv/bin/python your_project/manage.py collectstatic
If it uploads files, then everything is correct. If not, check the error messages.
pyflakes your_project/settings*.py
).Just to check the files are accessible from the web, paste the bucket's S3 web url into STATIC_URL. Run Django and see where the statc assets come from.
If you want CloudFront, it's some more steps.
In AWS, go to Services > Storage & Content Delivery > CloudFront. Create a distribution. Distribution is like a virtual web server with access to a folder.
Choose:
Go to the new distribution settings and copy the domain name. Paste it as STATIC_URL
in settings_local.py
file.
The new STATIC_URL
from CloudFront should not contain the bucket name, because this domain name is specifically for that bucket.
This URL is a sensitive data in the sense that access to it costs you real money and is slower than a local dev server, so probably it should not run on development environment.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With