Skip to main content

Flask utility for Amazon S3.

Project description

Build Status

Flask utility for signing Amazon S3 POST requests and validating Amazon S3 files. Both Python 2.7 and 3.4 are supported.

Upgrade note: Pontus 1.x branch uses Boto3. If you are still using boto, use 0.x.x versions. Check Git branch `version-0`.

Installation

Install with pip:

pip install Pontus

Dependencies

Pontus has the following dependencies:

Moreover python-magic depends on the libmagic file type identification library.

Examples

Signed POST request

Creating form fields for a signed Amazon S3 POST request

import boto3
from flask import current_app
from pontus import AmazonS3SignedRequest

session = boto3.session.Session(
    aws_access_key_id=current_app.config.get('AWS_ACCESS_KEY_ID'),
    aws_secret_access_key=current_app.config.get('AWS_SECRET_ACCESS_KEY'),
    region_name=current_app.config.get('AWS_REGION_NAME')
)
bucket = session.resource('s3').Bucket('testbucket')

signed_request = AmazonS3SignedRequest(
    key_name=u'my/file.jpg',
    mime_type=u'image/jpeg',
    bucket=bucket,
    session=session
)

signed_request.form_fields

# {
#     'x-amz-algorithm': 'AWS4-HMAC-SHA256',
#     'x-amz-credential': 'your-aws-access-key-id/date/region-name/s3/aws4_request',
#     'x-amz-date': 'date',
#     'x-amz-signature': 'generated-signature',
#     'success_action_status': '201',
#     'acl': 'public-read',
#     'Content-Type': 'image/png',
#     'key': u'f6c157e1-1a1a-4418-99fe-3362dcf7b1ea/images/my-image.jpg',
#     'policy': 'generated-policy-document'
# }

These form fields can be used to POST files to Amazon S3 as described in Amazon’s documentation.

Amazon S3 file validation

Validating that an image file is less than 2MB and is of image/jpeg MIME type.

import boto3
from flask import current_app
from pontus import AmazonS3FileValidator
from pontus.validators import FileSize, MimeType

session = boto3.session.Session(
    aws_access_key_id=current_app.config.get('AWS_ACCESS_KEY_ID'),
    aws_secret_access_key=current_app.config.get('AWS_SECRET_ACCESS_KEY')
)
bucket = session.resource('s3').Bucket('testbucket')

validator = AmazonS3FileValidator(
    key_name='images/my-image.jpg',
    bucket=bucket,
    validators=[FileSize(max=2097152), MimeType('image/jpeg')],
    session=session
)

if validator.validate():
    # File is <2MB image/jpeg
    pass
else:
    # File was invalid, printing errors
    print validator.errors

Validators can either be instances of a class inheriting pontus.validators.BaseValidator or callable functions that take one parameter obj, which is a boto.S3.Object instance.

from pontus.exceptions import ValidationError
from pontus.validators import BaseValidator

def name_starts_with_images(obj):
    if not obj.key.startswith('images/'):
        raise ValidationError()

# OR

class NameStartsWith(BaseValidator):
    def __init__(self, starts_with_str):
        self.starts_with_str = starts_with_str

    def __call__(self, obj):
        if not obj.key.startswith(starts_with_str):
            raise ValidationError()

name_starts_with_images = NameStartsWith('images/')

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

Pontus-4.0.0.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

Pontus-4.0.0-py2.py3-none-any.whl (10.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Pontus-4.0.0.tar.gz.

File metadata

  • Download URL: Pontus-4.0.0.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for Pontus-4.0.0.tar.gz
Algorithm Hash digest
SHA256 0bfbdacab68c864ac495c76d066547a4731a1011aee072c243b77b4c04225505
MD5 7be61656134daf0a917f6628b24e8338
BLAKE2b-256 37b7e0631b97e998c7f6e4a045e0a708e017986f32b243a6d039af4ec12ca38f

See more details on using hashes here.

File details

Details for the file Pontus-4.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: Pontus-4.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for Pontus-4.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6e8997f94cead2e5e857fb2cc711a43fafb0042c32e464a56421f576bf666104
MD5 8c4d56481a1839e817f17599e5bc3228
BLAKE2b-256 154457ea6cf718cb934c503ad7f588d00b0e45bfcd91c57da76ac680b1ecd850

See more details on using hashes here.

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