All Projects → bradleyg → Django S3direct

bradleyg / Django S3direct

Licence: mit
Directly upload files to S3 compatible services with Django.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django S3direct

Simple S3 Setup
Code examples used in the post "How to Setup Amazon S3 in a Django Project"
Stars: ✭ 46 (-91.93%)
Mutual labels:  aws, s3, django
Aws.s3
Amazon Simple Storage Service (S3) API Client
Stars: ✭ 302 (-47.02%)
Mutual labels:  aws, s3
Fluent Plugin S3
Amazon S3 input and output plugin for Fluentd
Stars: ✭ 276 (-51.58%)
Mutual labels:  aws, s3
Nodb
NoDB isn't a database.. but it sort of looks like one.
Stars: ✭ 353 (-38.07%)
Mutual labels:  aws, s3
Cookiecutter Django Vue Graphql Aws
A highly opinionated Cookiecutter template that fuses together Django, Vue.js, GraphQL, and AWS into one full-stack web application.
Stars: ✭ 213 (-62.63%)
Mutual labels:  aws, django
Nextjs Aws S3
Example Next.js app to upload photos to an S3 bucket.
Stars: ✭ 229 (-59.82%)
Mutual labels:  aws, s3
S3mock
A simple mock implementation of the AWS S3 API startable as Docker image, JUnit 4 rule, or JUnit Jupiter extension
Stars: ✭ 332 (-41.75%)
Mutual labels:  aws, s3
Slurp
Evaluate the security of S3 buckets
Stars: ✭ 183 (-67.89%)
Mutual labels:  aws, s3
Discharge
⚡️ A simple, easy way to deploy static websites to Amazon S3.
Stars: ✭ 483 (-15.26%)
Mutual labels:  aws, s3
Moto
A library that allows you to easily mock out tests based on AWS infrastructure.
Stars: ✭ 5,428 (+852.28%)
Mutual labels:  aws, s3
S3 Sync Action
🔄 GitHub Action to sync a directory with a remote S3 bucket 🧺
Stars: ✭ 497 (-12.81%)
Mutual labels:  aws, s3
Bucketstore
A simple library for interacting with Amazon S3.
Stars: ✭ 209 (-63.33%)
Mutual labels:  aws, s3
Aws Mobile React Native Starter
AWS Mobile React Native Starter App https://aws.amazon.com/mobile
Stars: ✭ 2,247 (+294.21%)
Mutual labels:  aws, s3
Node S3 Uploader
Flexible and efficient resize, rename, and upload images to Amazon S3 disk storage. Uses the official AWS Node SDK for transfer, and ImageMagick for image processing. Support for multiple image versions targets.
Stars: ✭ 237 (-58.42%)
Mutual labels:  aws, s3
Drone Cache
A Drone plugin for caching current workspace files between builds to reduce your build times
Stars: ✭ 194 (-65.96%)
Mutual labels:  aws, s3
Vue Cli Plugin S3 Deploy
A vue-cli plugin that uploads your built Vue.js project to an S3 bucket
Stars: ✭ 304 (-46.67%)
Mutual labels:  aws, s3
S3 Benchmark
Measure Amazon S3's performance from any location.
Stars: ✭ 525 (-7.89%)
Mutual labels:  aws, s3
Python Aws S3
Demo of AWS S3 Walkthrough using Python
Stars: ✭ 169 (-70.35%)
Mutual labels:  aws, s3
Lad
👦 Lad is the best Node.js framework. Made by a former Express TC and Koa team member.
Stars: ✭ 2,112 (+270.53%)
Mutual labels:  aws, s3
Mangum
AWS Lambda & API Gateway support for ASGI
Stars: ✭ 475 (-16.67%)
Mutual labels:  aws, django

django-s3direct

Directly upload files to S3 compatible services with Django.

Installation

Install with Pip:
pip install django-s3direct

Access setup

When setting up access credentials you have two options:

Option 1:

Generate access credentials and add them directly to your Django settings. If using Amazon S3 you'll also need to create an IAM policy which grants permission to upload to your bucket for your newly created credentials.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:PutObjectAcl",
        "s3:ListMultipartUploadParts",
        "s3:AbortMultipartUpload"
      ],
      "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
    }
  ]
}

Option 2:

Use the EC2 instance profile and its attached IAM role (AWS only)
Ensure the following trust policy is in place in addition to the policy above. You'll also need the boto3 package installed.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

CORS setup

Add a CORS policy to your bucket. Note the ETag header is important as it is used for multipart uploads. For more information see here.

If using Digital Ocean Spaces you must upload the CORS config via the API/s3cmd CLI (as you can't add the ExposeHeader rule). See here for more details.

<CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>http://YOURDOMAIN.COM:8080</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>ETag</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Django Setup

settings.py

INSTALLED_APPS = [
    ...
    's3direct',
    ...
]

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

# AWS

# If these are set to None, the EC2 instance profile and IAM role are used.
AWS_ACCESS_KEY_ID = 'your-aws-access-key-id'
AWS_SECRET_ACCESS_KEY = 'your-aws-secret-access-key'

# Bucket name
AWS_STORAGE_BUCKET_NAME = 'your-aws-s3-bucket-name'

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

# The endpoint of your bucket, more info:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
AWS_S3_ENDPOINT_URL = 'https://s3.eu-west-1.amazonaws.com'

S3DIRECT_DESTINATIONS = {
    'example_destination': {
        # "key" [required] The location to upload file
        #       1. String: folder path to upload to
        #       2. Function: generate folder path + filename using a function  
        'key': 'uploads/images',

        # "auth" [optional] Limit to specfic Django users
        #        Function: ACL function
        'auth': lambda u: u.is_staff,

        # "allowed" [optional] Limit to specific mime types
        #           List: list of mime types
        'allowed': ['image/jpeg', 'image/png', 'video/mp4'],

        # "bucket" [optional] Bucket if different from AWS_STORAGE_BUCKET_NAME
        #          String: bucket name
        'bucket': 'custom-bucket',

        # "endpoint" [optional] Endpoint if different from AWS_S3_ENDPOINT_URL
        #            String: endpoint URL
        'endpoint': 'custom-endpoint',

        # "region" [optional] Region if different from AWS_S3_REGION_NAME
        #          String: region name
        'region': 'custom-region', # Default is 'AWS_S3_REGION_NAME'

        # "acl" [optional] Custom ACL for object, default is 'public-read'
        #       String: ACL
        'acl': 'private',

        # "cache_control" [optional] Custom cache control header
        #                 String: header
        'cache_control': 'max-age=2592000',

        # "content_disposition" [optional] Custom content disposition header
        #                       String: header
        'content_disposition': lambda x: 'attachment; filename="{}"'.format(x),

        # "content_length_range" [optional] Limit file size
        #                        Tuple: (from, to) in bytes
        'content_length_range': (5000, 20000000),

        # "server_side_encryption" [optional] Use serverside encryption
        #                          String: encrytion standard
        'server_side_encryption': 'AES256',

        # "allow_existence_optimization" [optional] Checks to see if file already exists,
        #                                returns the URL to the object if so (no upload)
        #                                Boolean: True, False
        'allow_existence_optimization': False,
    },
    'example_destination_two': {
        'key': lambda filename, args: args + '/' + filename,
    	'key_args': 'uploads/images',
    }
}

urls.py

urlpatterns = [
    ...
    url(r'^s3direct/', include('s3direct.urls')),
    ...
]

Run python manage.py collectstatic if required.

Use in Django admin

models.py

from django.db import models
from s3direct.fields import S3DirectField

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

Use the widget in a custom form

forms.py

from django import forms
from s3direct.widgets import S3DirectWidget

class S3DirectUploadForm(forms.Form):
    images = forms.URLField(widget=S3DirectWidget(dest='example_destination'))

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

views.py

from django.views.generic import FormView
from .forms import S3DirectUploadForm

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

templates/form.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:

$ git clone [email protected]:bradleyg/django-s3direct.git
$ cd django-s3direct
$ python setup.py install
$ cd example

# Add config to your environment
export AWS_ACCESS_KEY_ID='…'
export AWS_SECRET_ACCESS_KEY='…'
export AWS_STORAGE_BUCKET_NAME='…'
export AWS_S3_REGION_NAME='…'
export AWS_S3_ENDPOINT_URL='…'

$ python manage.py migrate
$ python manage.py createsuperuser
$ python manage.py runserver

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

Development

$ git clone [email protected]:bradleyg/django-s3direct.git
$ cd django-s3direct

# Add your AWS keys/details to .env file and export
$ cp .env-dist .env

# Build docker image
$ docker build . --build-arg SKIP_TOX=true -t s3direct
$ docker run -itv $(pwd):/code -p 8000-8001:8000-8001 --env-file .env s3direct bash
$ npm i

# Install locally
$ python setup.py develop

# Run examples
$ python example/manage.py migrate
$ python example/manage.py createsuperuser
$ python example/manage.py runserver 0.0.0.0:8000

# Run tox tests
$ tox

# Run tests
$ npm run test

# Run frontend bundler and Django server
$ npm run dev

# Watch and build frontend (dev)
$ npm run watch

# Build frontend (prod)
$ npm run build

# Format python // PEP8
$ npm run yapf

# Upload to PYPI
$ npm run pypi
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].