Skip to main content

Field types for allowing file and image uploads to Amazon S3 (as well as default local storage) in Flask-Admin.

Project description

Field types for allowing file and image uploads to Amazon S3 (as well as default local storage) in Flask-Admin.

Example

For a complete, working Flask app that demonstrates flask-admin-s3-upload in action, have a look at flask-s3-save-example.

Usage

Use with a Flask-Admin ModelView by overriding field types, and by passing in special arguments to those fields:

from flask.ext.admin.contrib.sqla import ModelView

class MyView(ModelView):
    form_overrides = dict(
        some_image=S3ImageUploadField,
        some_file=S3FileUploadField)

    form_args = dict(
        some_image=dict(
            base_path='/some/folder/static',
            relative_path='some_image/',
            url_relative_path='uploads/',
            namegen=your_namegen_func_here,
            storage_type_field='some_image_storage_type',
            bucket_name_field='some_image_storage_bucket_name',
        ),
        some_file=dict(
            base_path='/some/folder/static',
            relative_path='some_file/',
            namegen=your_namegen_func_here,
            allowed_extensions=('pdf', 'txt'),
            storage_type_field='some_file_storage_type',
            bucket_name_field='some_file_storage_bucket_name',
        ))

    def scaffold_form(self):
        # Note: assuming that we have Flask-S3 config values to pass
        # to fields below. Flask-S3 is not required, you can pass
        # values from elsewhere if you want.
        from flask import current_app as app

        form_class = super(MyView, self).scaffold_form()
        static_root_parent = '/some/folder'

        if app.config['USE_S3']:
            form_class.some_image.kwargs['storage_type'] = 's3'
            form_class.some_file.kwargs['storage_type'] = 's3'

        form_class.some_image.kwargs['bucket_name'] = app.config['S3_BUCKET_NAME']
        form_class.some_image.kwargs['access_key_id'] = app.config['AWS_ACCESS_KEY_ID']
        form_class.some_image.kwargs['access_key_secret'] = app.config['AWS_SECRET_ACCESS_KEY']
        form_class.some_image.kwargs['static_root_parent'] = static_root_parent

        form_class.some_file.kwargs['bucket_name'] = app.config['S3_BUCKET_NAME']
        form_class.some_file.kwargs['access_key_id'] = app.config['AWS_ACCESS_KEY_ID']
        form_class.some_file.kwargs['access_key_secret'] = app.config['AWS_SECRET_ACCESS_KEY']
        form_class.some_file.kwargs['static_root_parent'] = static_root_parent

        return form_class

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

flask_admin_s3_upload-0.1.2-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flask_admin_s3_upload-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flask_admin_s3_upload-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1abe1c18851b83165419243eadb9fdc353607d23e4eec39b5190390ebf0e869
MD5 191c404b1c0be5288556d2f561468337
BLAKE2b-256 4b00d8c299fa76a3f5217db6f9847ca4bc0e99352f692d23f0ad2b601a1128b0

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