Configuration

Before you can begin using Flask S3Viewer, you should set up authentication credentials. Credentials for your AWS account can be found in the IAM Console. You can create or use an existing user. Go to manage access keys and generate a new set of keys.

Configure credentials

Install AWS CLI.

pip install awscli

If you have the AWS CLI installed, then you can use it to configure your credentials file:

aws configure

Alternatively, you can create the credential file yourself. By default, its location is at ~/.aws/credentials. and Flask S3Viewer is going to use the credential file.

Minimum settings

This is a minimal setup for using flask s3viewer. First install the dependency packages.

pip install flask flask_s3_viewer

Import flask and flask_s3_viewer

1from flask import Flask
2
3from flask_s3_viewer import FlaskS3Viewer
4from flask_s3_viewer.aws.ref import Region

Initiailize Flask application and FlaskS3Viewer.

 1# Init Flask
 2app = Flask(__name__)
 3
 4# Init Flask S3Viewer
 5s3viewer = FlaskS3Viewer(
 6    # Flask App
 7    app,
 8    # Namespace must be unique
 9    namespace='flask-s3-viewer',
10    # Hostname, e.g. Cloudfront endpoint
11    object_hostname='http://flask-s3-viewer.com',
12    # Put your AWS's profile name and Bucket name
13    config={
14        'profile_name': 'PROFILE_NAME',
15        'bucket_name': 'S3_BUCKET_NAME'
16    }
17)
18
19# Register Flask S3Viewer's router
20s3viewer.register()
21
22if __name__ == '__main__':
23    app.run(debug=True, port=3000)

The values in the code above are mandatory. If the setting is finished, run your Flask application and visit http://localhost/{namespace}/files, e.g. http://localhost:3000/flask-s3-viewer/files.

You can get example codes over here.


User Guides

It is about various advanced settings.

Multiple bucket settings

You can also initiailize multiple bucket.

 1...
 2
 3s3viewer = FlaskS3Viewer(
 4    ...
 5)
 6
 7# Init another bucket
 8s3viewer.add_new_one(
 9    namespace='another_namespace',
10    object_hostname='http://anotherbucket.com',
11    config={
12        'profile_name': 'PROFILE_NAME',
13        'bucket_name': 'S3_BUCKET_NAME'
14    }
15)
16s3viewer.register()

Mount a specific path in a bucket for browsing

You can mount a specific path in the bucket to the browser. ( Be careful not to end the path with / )

 1...
 2
 3s3viewer = FlaskS3Viewer(
 4    ...
 5)
 6
 7# Init another bucket
 8s3viewer.add_new_one(
 9    namespace='another_namespace',
10    object_hostname='http://anotherbucket.com',
11    config={
12        'profile_name': 'PROFILE_NAME',
13        'bucket_name': 'S3_BUCKET_NAME'
14        'base_path': 'path/to/your/folder'
15    }
16)
17s3viewer.register()

Limit the file extensions

You can limit the file extensions that are uploaded, if you want.

1s3viewer = FlaskS3Viewer(
2    ...
3
4    # allowed extension
5    allowed_extensions={'jpg', 'jpeg'},
6    config={
7        ...
8    }
9)

Choose the design template

Flask S3 Viewer supports the templates below.

Template namespace

Design type

Description

base

Default

Not designed at all

mdl

Material Design Lite

link

1s3viewer = FlaskS3Viewer(
2    ...
3    # Enter template namespace (default: base)
4    template_namespace='mdl',
5    config={
6        ...
7    }
8)
9s3viewer.register()

Controll large files

If you want to controll large files (maybe larger than 5MB ~ maximum 5TB), I recommand to set like below. Flask S3Viewer is going to use S3’s presigned URL. It’s nice to controll large files.

1s3viewer = FlaskS3Viewer(
2    ...
3    # Change upload type to 'presign'
4    upload_type='presign',
5    config={
6        ...
7    }
8)
9s3viewer.register()

but you must do S3’s CORS settings before like set above.

 1[
 2    {
 3        "AllowedHeaders": [
 4            "*"
 5        ],
 6        "AllowedMethods": [
 7            "POST",
 8            "PUT",
 9            "GET",
10            "HEAD",
11            "DELETE"
12        ],
13        "AllowedOrigins": [
14            "http://localhost:3000"
15        ],
16    }
17]

Use Caching

S3 is charged per call. Therefore, Flask S3Viewer supports caching (currently only supports file caching, in-memory database will be supported later).

 1s3viewer = FlaskS3Viewer(
 2    ...
 3    config={
 4        ...
 5        # Flask S3Viewer will cache the list of s3 objects, if you set True
 6        'use_cache': True,
 7        # Where cached files will be written
 8        'cache_dir': '/tmp/flask_s3_viewer',
 9        # Time To Live
10        'ttl': 86400
11    }
12)
13s3viewer.register()

Full example

 1...
 2
 3 s3viewer = FlaskS3Viewer(
 4     # Flask app
 5     app,
 6     # Namespace must be unique
 7     namespace='flask-s3-viewer',
 8     # Enter template namespace(default: base)
 9     template_namespace='mdl',
10     # File's hostname
11     object_hostname='http://flask-s3-viewer.com',
12     # Allowed extension
13     allowed_extensions={},
14     # Bucket configs and else
15     config={
16         # Required
17         'profile_name': 'PROFILE_NAME',
18         # Required
19         'bucket_name': 'S3_BUCKET_NAME',
20         'region_name': Region.SEOUL.value,
21         # Not necessary, if you configure aws settings, e.g. ~/.aws
22         'access_key': 'AWS_IAM_ACCESS_KEY',
23         'secret_key': 'AWS_IAM_SECRET_KEY',
24         # For S3 compatible
25         'endpoint_url': None,
26         # Flask S3Viewer will cache the list of s3 objects, if you set True
27         'use_cache': True,
28         # Where cached files will be written
29         'cache_dir': '/tmp/flask_s3_viewer',
30         # Time To Live
31         'ttl': 86400,
32     }
33 )

Things to know

Searching

  • Search only working in EN, because of JMESPath.