Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up django-storages?

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.

like image 338
Zacharoo Avatar asked Aug 17 '13 05:08

Zacharoo


People also ask

What is default storage in Django?

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.

How do I upload files to AWS s3 using Django REST framework?

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.


1 Answers

This is a setup checklist that I made for my colleagues.

  1. Set up an account on Amazon and get Amazon Web Services. (It checks your credit card, but will not charge you initially.)
  2. Go to AWS web console, goto S3 (file storage). Create a new bucket.
  3. In bucket properties add write premissions to Authenticated Users.
  4. In AWS console go to Services > Deployment & Management > IAM (Identity and Access Management). Create a user group and one user in it. DO NOT CLOSE THE POPUP WINDOW! In the popup window, click "Download Credentials" to save the keys. They are needed for Django settings_local.py.
  5. If you forgot or lost the keys, go to that user > Security Credentials > Access Credentials > Manage Access Keys. Create a key and save the credentials.
  6. 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.

  7. Install the following packages:

    django-storages==1.1.8
    boto==2.9.7
    
  8. 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'
    
  9. 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',
        )
    
  10. Go to AWS S3 section and get the url for your bucket, paste it into settings files, set STATIC_URL accordingly.

  11. Paste keys from credentials into settings_local.py. Now Django should be able to upload static files to the storage.

  12. Run this command:

    $ uenv/bin/python your_project/manage.py collectstatic
    
  13. If it uploads files, then everything is correct. If not, check the error messages.

    1. If Django crashes, then it's local setup issues (most probably import errors - then do pyflakes your_project/settings*.py).
    2. If remote server responds 403, then it's user access policy, issue. You need to go to user and set up the access, and to bucket and check if authenticated users have permissions.
  14. 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.

  15. 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:

    • Download
    • Origin domain name = the domain name of the bucket you created in the first steps.
    • Distribution state = Enabled
  16. 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.

like image 110
culebrón Avatar answered Oct 20 '22 08:10

culebrón