Skip to main content

Add direct uploads to S3 to file input fields.

Project description

django-s3-upload
================

Upload files directly to S3 from Django
-------------------------------------

[![Build Status](https://travis-ci.org/yunojuno/django-s3upload.svg?branch=master)](https://travis-ci.org/yunojuno/django-s3upload)

Add direct uploads to AWS S3 functionality with a progress bar to file input fields.

<img src="https://raw.githubusercontent.com/bradleyg/django-s3-upload/master/screenshot.png" width="381"/>

## Installation

Install with Pip:

```pip install django-s3-upload```

## AWS Setup

### Access Credentials

You have two options of providing access to AWS resources:

1. Add credentials of an IAM user to your Django settings (see below)
2. Use the EC2 instance profile and its attached IAM role

Whether you are using an IAM user or a role, there needs to be an IAM policy
in effect that grants permission to upload to S3:

```json
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:PutObjectAcl"],
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
```

If the instance profile is to be used, the IAM role needs to have a
Trust Relationship configuration applied:

```json
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
```

Note that in order to use the EC2 instance profile, django-s3-upload needs
to query the EC2 instance metadata using utility functions from the
[botocore] [] package. You already have `botocore` installed if `boto3`
is a dependency of your project.

### S3 CORS

Setup a CORS policy on your S3 bucket.

```xml
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>http://yourdomain.com:8080</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
```

## Django Setup

### settings.py

```python
INSTALLED_APPS = [
...
's3upload',
...
]

TEMPLATES = [{
...
'APP_DIRS': True,
...
}]

# AWS

# If these are not defined, the EC2 instance profile and IAM role are used.
# This requires you to add boto3 (or botocore, which is a dependency of boto3)
# to your project dependencies.
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''

AWS_STORAGE_BUCKET_NAME = ''

# The region of your bucket, more info:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
S3DIRECT_REGION = 'us-east-1'

# Destinations, with the following keys:
#
# key [required] Where to upload the file to, can be either:
# 1. '/' = Upload to root with the original filename.
# 2. 'some/path' = Upload to some/path with the original filename.
# 3. functionName = Pass a function and create your own path/filename.
# auth [optional] An ACL function to whether the current Django user can perform this action.
# allowed [optional] List of allowed MIME types.
# acl [optional] Give the object another ACL rather than 'public-read'.
# cache_control [optional] Cache control headers, eg 'max-age=2592000'.
# content_disposition [optional] Useful for sending files as attachments.
# bucket [optional] Specify a different bucket for this particular object.
# server_side_encryption [optional] Encryption headers for buckets that require it.

S3UPLOAD_DESTINATIONS = {
'example_destination': {
# REQUIRED
'key': 'uploads/images',

# OPTIONAL
'auth': lambda u: u.is_staff, # Default allow anybody to upload
'allowed_types': ['image/jpeg', 'image/png', 'video/mp4'], # Default allow all mime types
'allowed_extensions': ('.jpg', '.jpeg', '.png'), # Defaults to all extensions
'bucket': 'pdf-bucket', # Default is 'AWS_STORAGE_BUCKET_NAME'
'acl': 'private', # Defaults to 'public-read'
'cache_control': 'max-age=2592000', # Default no cache-control
'content_disposition': 'attachment', # Default no content disposition
'content_length_range': (5000, 20000000), # Default allow any size
'server_side_encryption': 'AES256', # Default no encryption
}
}
```

### urls.py

```python
urlpatterns = [
url(r'^s3upload/', include('s3upload.urls')),
]
```

Run ```python manage.py collectstatic``` if required.

## Use in Django admin

### models.py

```python
from django.db import models
from s3upload.fields import S3UploadField

class Example(models.Model):
video = S3UploadField(dest='example_destination')
```

## Use the widget in a custom form

### forms.py

```python
from django import forms
from s3upload.widgets import S3UploadWidget

class S3UploadForm(forms.Form):
images = forms.URLField(widget=S3UploadWidget(dest='example_destination'))
```

__*Optional.__ You can modify the HTML of the widget by overiding template __s3direct/templates/s3direct-widget.tpl__

### views.py

```python
from django.views.generic import FormView
from .forms import S3UploadForm

class MyView(FormView):
template_name = 'form.html'
form_class = S3UploadForm
```

### templates/form.html

```html
<html>
<head>
<meta charset="utf-8">
<title>s3direct</title>
{{ form.media }}
</head>
<body>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
</form>
</body>
</html>
```


## Examples

Examples of both approaches can be found in the examples folder. To run them:
```shell
$ git clone git@github.com:yunojuno/django-s3-upload.git
$ cd django-s3-upload

# Add your AWS keys to your environment
export AWS_ACCESS_KEY_ID='...'
export AWS_SECRET_ACCESS_KEY='...'
export AWS_STORAGE_BUCKET_NAME='...'
export S3DIRECT_REGION='...' # e.g. 'eu-west-1'

$ docker-compose up
```

Visit ```http://localhost:8000/admin``` to view the admin widget and ```http://localhost:8000/form``` to view the custom form widget.

[botocore]: https://github.com/boto/botocore

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-s3-upload-0.1.1.tar.gz (68.4 kB view details)

Uploaded Source

Built Distribution

django_s3_upload-0.1.1-py3-none-any.whl (80.6 kB view details)

Uploaded Python 3

File details

Details for the file django-s3-upload-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-s3-upload-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1ea6f3c87142e613a7ccb588f64c7579cec3a2297ea8577676302a83ddedbc26
MD5 05b5f6e586caaec134cdf766ccfe7472
BLAKE2b-256 0eb498a1d99d14b920eb8b3dc2eb189f8f87dd0d0359e477d07656fc840553a2

See more details on using hashes here.

File details

Details for the file django_s3_upload-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_s3_upload-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 854b73589930864c01a5ad059aba921852867f087215413989a70ba6726c21ec
MD5 f538042efd1c3fa424bf7dc15b86d689
BLAKE2b-256 247f041886a2c1e7adec662cab6aad207f4312814dafe958dfd3618e77dc6936

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page