Flask-FileUpload
A simple file upload flask extension.
- Previews
- Easy link copy
- Delete and Upload with 3 clicks
- Renaming of the file before uploading
Install
pip install Flask-FileUpload
Configuration
Required
SECRET_KEY="Any Secret key u want"
Optional
FILEUPLOAD_LOCALSTORAGE_IMG_FOLDER="folder" # Where to store the images if used the default LocalStorage
FILEUPLOAD_PREFIX="/any/prefix/u/want" # Blueprint prefix
FILEUPLOAD_ALLOWED_EXTENSIONS=["list", "of", "file", "extensions"] # Allow only these extensions
FILEUPLOAD_ALLOW_ALL_EXTENSIONS=True # Allow all extensions
FILEUPLOAD_RANDOM_FILE_APPENDIX = True # Append a random 6 hash string to selected file
FILEUPLOAD_CONVERT_TO_SNAKE_CASE = True # Converts filenames to snake_case
S3 Storage
FILEUPLOAD_S3_BUCKET = 'sample-bucket-name' # name of the S3 bucket
FILEUPLOAD_S3_ACL = 'public-read' # S3 permission
S3 permission options:
- public-read
- private
- public-read-write
- authenticated-read
- aws-exec-read
- bucket-owner-read
- bucket-owner-full-control
jinja2 method and filter
fu_get_existing_files
fu_filename
They can be used everywhere in your jinja template for e.g: list all available files. Combined you can do something like:
<ul>
{% for file in fu_get_existing_files() %}
<li><a href="{{ file }}">{{ file|fu_filename }} </a></li>
{% endfor %}
</ul>
HowTo
from flask import Flask
from flask_fileupload import FlaskFileUpload
app = Flask(__name__)
app.config.from_object("config")
ffu = FlaskFileUpload(app)
lm = LoginManager(app)
class User(UserMixin):
def __init__(self, user_id):
self.id = user_id
@lm.user_loader
def load_user(user_id):
return User(user_id)
Change Storage
from flask_fileupload import FlaskFileUpload
from flask_fileupload.storage.s3storage import S3Storage
...
s3storage = S3Storage()
ffu = FlaskFileUpload(app, s3storage)
If no storage is provided, the default storage will be taken