Skip to main content

Django view for AWS S3 signing

Project description

Build Status

django-s3sign

s3 sign view for django. Facilitates file uploads to AWS S3.

installation

$ pip install django-s3sign

usage

Add s3sign to INSTALLED_APPS. Subclass s3sign.views.SignS3View and override as needed.

Attributes you can override (and their default values):

    name_field = 's3_object_name'
    type_field = 's3_object_type'
    expiration_time = 10
    mime_type_extensions = [
        ('bmp', '.bmp'),
        ('gif', '.gif'),
        ('jpeg', '.jpg'),
        ('pdf', '.pdf'),
        ('png', '.png'),
        ('svg', '.svg'),
        ('webm', '.webm'),
        ('webp', '.webp'),
        ('heif', '.heif'),
        ('heic', '.heic'),
        ('avif', '.avif'),
    ]
    default_extension = '.obj'
    root = ''
    path_string = (
        "{root}{now.year:04d}/{now.month:02d}/"
        "{now.day:02d}/{basename}{extension}")

Methods you can override:

  • get_aws_access_key(self)
  • get_aws_secret_key(self)
  • get_bucket(self)
  • get_mimetype(self, request)
  • extension_from_mimetype(self, mime_type)
  • now() # useful for unit tests
  • now_time() # useful for unit tests
  • basename()
  • get_object_name(self, extension)

Most of those should be clear. Read the source if in doubt.

Eg to use a different root path:

from s3sign.views import SignS3View
...

class MySignS3View(LoggedInView, SignS3View):
    root = 'uploads/'

With a different S3 bucket:

class MySignS3View(LoggedInView, SignS3View):
    def get_bucket(self):
        return settings.DIFFERENT_BUCKET_NAME

Keeping the uploaded filename instead of doing a random one and whitelisted extension:

class MySignS3View(LoggedInView, SignS3View):
    def basename(self, request):
        filename = request.GET[self.get_name_field()]
        return os.path.basename(filename)

    def extension(self, request):
        filename = request.GET[self.get_name_field()]
        return os.path.splitext(filename)[1]

javascript/forms

The required javascript is also included, so you can include it in your page with:

{% load static %}

<script src="{% static 's3sign/js/s3upload.js' %}"></script>

Your form would then somewhere have a bit like:

<form method="post">
    <p id="status">
        <strong>Please select a file</strong>
    </p>

    <input type="hidden" name="s3_url" id="uploaded-url" />
    <input type="file" id="file" onchange="s3_upload();"/>
</form>

And

<script>
function s3_upload() {
    const s3upload = new S3Upload({
        file_dom_el: null, // Optional, and overrides file_dom_selector
                           // when present.
        file_dom_selector: '#file',
        s3_sign_put_url: '/sign_s3/', // change this if you route differently
        s3_object_name: $('#file')[0].value,

        onProgress: function(percent, message) {
            $('#status').text('Upload progress: ' + percent + '% ' + message);
        },
        onFinishS3Put: function(url) {
            $('#uploaded-url').val(url);
        },
        onError: function(status) {
            $('#status').text('Upload error: ' + status);
        }
    });
}
</script>

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_s3sign-0.6.1.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_s3sign-0.6.1-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file django_s3sign-0.6.1.tar.gz.

File metadata

  • Download URL: django_s3sign-0.6.1.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_s3sign-0.6.1.tar.gz
Algorithm Hash digest
SHA256 8b2a2653f21527ef7a1d6c122f487674e62f706a3116bf49935f9fb7c05bf66d
MD5 2ff4b5346cd05bdbbe1b9a54d177cfb2
BLAKE2b-256 09f44d9cbc3d159bb664c59da60177d173bcbd788e88bcf3fb4e56ecab82f647

See more details on using hashes here.

File details

Details for the file django_s3sign-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: django_s3sign-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_s3sign-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5358f11f613002103cd75b9346fd77dc302880bc391394df7b30cd9f043e3366
MD5 6bbf82f5c4aac826929dbc5ddd161a87
BLAKE2b-256 62fbbfee07b38543dc2e097fdb0771120e14e9e9667427961d0aac69bc0b3602

See more details on using hashes here.

Supported by

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