Skip to main content

Django view for AWS S3 signing

Project description

Build Status Coverage Status

django-s3sign

s3 sign view for django

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'),
        ('webp', '.webp'),
    ]
    default_extension = '.obj'
    root = ''
    path_string = (
        "{root}{now.year:04d}/{now.month:02d}/"
        "{now.day:02d}/{basename}{extension}")
    acl = "public-read"

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(self) # useful for unit tests
  • now_time(self) # useful for unit tests
  • basename(self)
  • 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() {
    var s3upload = new S3Upload({
        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.3.3.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

django_s3sign-0.3.3-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file django-s3sign-0.3.3.tar.gz.

File metadata

  • Download URL: django-s3sign-0.3.3.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for django-s3sign-0.3.3.tar.gz
Algorithm Hash digest
SHA256 827c3694687446bf1858c2a26159b73b9297158a1ce1bfa61550c98abf9b3e52
MD5 f5c5081e4dc1ed22c8b26dec588a05a1
BLAKE2b-256 5faee6399913463097bc617d5c676b89c4d5a81530a2a48dc1c6b0e2d49f5896

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_s3sign-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 46d3e18a2e3e53e32ef03d0b16977a364f03c506f1a5e7914b655353f7184785
MD5 badb6d684df2584ad9ecb3517f8c4117
BLAKE2b-256 a850f09ce0b4a9d1fb6d404d2384f0e8284bbc9b3d85f18085338b0324cbf5e3

See more details on using hashes here.

Provenance

Supported by

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