Skip to content

Amazon S3 configuration

Ana Sustic edited this page Mar 30, 2015 · 3 revisions

S3 Amazon configuration
Konzola na Amazon S3: https://console.aws.amazon.com/s3/home?region=us-west-2

navodila:
http://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/

✳ Create a bucket
✳ Create a new user: Go to AWS IAM. Click “Create new users” and follow the prompts. Leave “Generate an access key for each User” selected.
✳ Get the credentials
User ID: trackcat
Access Key ID: XXXXXXXXXXXXXX
Secret Access Key: XXXXXXXXXXXXX
✳ Go to the new user’s Security Credentials tab.
✳ Click “Manage access keys”,
✳ Download the credentials for the access key that was created, and
✳ Save them somewhere because no one will ever be able to download them again.
✳ Get the new user’s ARN (Amazon Resource Name) by going to the user’s Summary tab. It’ll look like this: arn:aws:iam::452326330757:user/trackcat
✳ Go to the bucket properties in the S3 management console.
✳ Add a bucket policy that looks like this, but change “BUCKET-NAME” to the bucket name, and “USER-ARN” to your new user’s ARN. The first statement makes the contents publicly readable (so you can serve the files on the web), and the second grants full access to the bucket and its contents to the specified user::

{
    "Statement": [
        {
            "Sid": "PublicReadForGetBucketObjects",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::trackcat/*"
            ]
        },
        {
            "Action": "s3:*",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::trackcat",
                "arn:aws:s3:::trackcat/*"
            ],
            "Principal": {
                "AWS": [
                    "XXXXXXXXXXXXXXXX"
                ]
            }
        }
    ]
}

Go to your S3 bucket properties, and under “Permissions”, click on “Add CORS Configuration”. Paste this in:

 <CORSConfiguration>
        <CORSRule>
            <AllowedOrigin>*</AllowedOrigin>
            <AllowedMethod>GET</AllowedMethod>
            <MaxAgeSeconds>3000</MaxAgeSeconds>
            <AllowedHeader>Authorization</AllowedHeader>

requirements.txt
$ pip install django-storages boto

settings.py
Add ‘storages’ to INSTALLED_APPS:

 
  INSTALLED_APPS = (
          ...,
          'storages',
     )

																		
  1. AMAZON S3 TO STORE MEDIA FILES CONFIGURATION

AWS_STORAGE_BUCKET_NAME = ‘trackcat’
AWS_ACCESS_KEY_ID = ‘XXXXXXXXXX
AWS_SECRET_ACCESS_KEY = ‘XXXXXXXXXXXXXXXXXXX

  1. Tell django-storages that when coming up with the URL for an item in S3 storage, keep
  2. it simple – just use this domain plus the path. (If this isn’t set, things get complicated).
  3. This controls how the `static` template tag from `staticfiles` gets expanded, if you’re using it.
  4. We also use it in the next setting.
    AWS_S3_CUSTOM_DOMAIN = ‘%s.s3.amazonaws.com’ % AWS_STORAGE_BUCKET_NAME

MEDIA_URL = “https://%s/” % AWS_S3_CUSTOM_DOMAIN
DEFAULT_FILE_STORAGE = ‘storages.backends.s3boto.S3BotoStorage’

  1. END OF AMAZON S3 CONFIGURATION

commented perviously defined media_url definition

Clone this wiki locally