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 = [
        ('jpeg', '.jpg'),
        ('png', '.png'),
        ('gif', '.gif'),
        ('pdf', '.pdf'),
        ('svg', '.svg'),
        ('webp', '.webp'),
    ]
    default_extension = '.obj'
    root = ''
    path_string = (
        "{root}{now.year:04d}/{now.month:02d}/"
        "{now.day:02d}/{basename}{extension}")
    amz_headers = "x-amz-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:

<script src="{{STATIC_URL}}s3sign/js/s3upload.js"></script>

Your form would then somewhere have a bit like:

<p id="status"><strong>Please select a file</strong></p>
<input type="file" id="file" onchange="s3_upload();"/>
<input type="hidden" name="s3_url" id="uploaded-url" />

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').html('Upload progress: ' + percent + '%' + message);
        },
        onFinishS3Put: function(url) {
            $('#uploaded-url').val(url);
        },
        onError: function(status) {
            $('#status').html('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.1.7.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

django_s3sign-0.1.7-py2.py3-none-any.whl (19.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django-s3sign-0.1.7.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.6.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.6

File hashes

Hashes for django-s3sign-0.1.7.tar.gz
Algorithm Hash digest
SHA256 0fa3f116c6e5e34859bb55ef162ada351701fbc76c3a43fdc245d7e54e3bfa6b
MD5 e10136e2717cfd90ae35f26083de12d5
BLAKE2b-256 fdcc5d3fff23cb0d1530140c8ddcd3e68f60d8fa3b46da478a213687979b88f0

See more details on using hashes here.

Provenance

File details

Details for the file django_s3sign-0.1.7-py2.py3-none-any.whl.

File metadata

  • Download URL: django_s3sign-0.1.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.6.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.6

File hashes

Hashes for django_s3sign-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cd594d82e7227b0954feeaca0933d723a825aa5cf1cb6be941cf72e4dea032d3
MD5 f10c6ff9d35545ede8827ca841bd1e25
BLAKE2b-256 e61a6d3ff5b81f47385fb8a2d779a7658b9186704c7bb0990f61e5c332ef0bbc

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