Skip to main content

Library that works with Flask & SqlAlchemy to store files in your database and server.

Project description

Documentation Status PyPI version PyPI - Python Version PyPI - License

FlaskFileUpload

Library that works with Flask & SqlAlchemy to store files on your server & in your database

Read the docs: Documentation

Installation

pip install flask-file-upload

Flask File Upload

General Flask config options
    # Important: The below configuration variables need to be set  before
    # initiating `FileUpload`
    UPLOAD_FOLDER = join(dirname(realpath(__file__)), "uploads/media")
    ALLOWED_EXTENSIONS = ["jpg", "png", "mov", "mp4", "mpg"]
    MAX_CONTENT_LENGTH` = 1000 * 1024 * 1024  # 1000mb
    SQLALCHEMY_DATABASE_URI = "postgresql://localhost:5432/blog_db"
Setup

We can either pass the instance to FileUpload(app) or to the init_app(app) method:

app = Flask(__name__)

db = SQLAlchemy()
# Important! See documentation for set up specifics
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
file_upload = FileUpload(app, db)

# An example using the Flask factory pattern
def create_app():
    db.init_app(app)
    file_upload.init_app(app)

# Or we can pass the Flask app instance directly & the Flask-SQLAlchemy instance:
db = SQLAlchemy(app)
file_upload = FileUpload(app, db)
app: Flask = None
Decorate your SqlAlchemy models

Flask-File-Upload (FFU) setup requires each SqlAlchemy model that wants to use FFU library to be decorated with @file_upload.Model .This will enable FFU to update your database with the extra columns required to store files in your database. Declare your attributes as normal but assign a value of file_upload.Column & pass the SqlAlchemy db instance: file_upload.Column(db). This is easy if you are using Flask-SqlAlchemy:

from flask_sqlalchemy import SqlAlchemy

db = SqlAlchemy()

Full example:

from my_app import db, file_upload

@file_upload.Model
class blogModel(db.Model):
   __tablename__ = "blogs"
   id = db.Column(db.Integer, primary_key=True)

   # Your files -  Notice how we pass in the SqlAlchemy instance
   # or `db` to the `file_uploads.Column` class:

   my_placeholder = file_upload.Column(db)
   my_video = file_upload.Column(db)
define files to be upload:
(This is an example of a video with placeholder image attached):
    my_video = request.files["my_video"]
    placeholder_img = request.files["placeholder_img"]
Save files
    file_upload.save_files(blog_post, files={
        "my_video": my_video,
        "placeholder_img": placeholder_img,
    })
Update files
    blog_post = file_upload.update_files(blog_post, files={
        "my_video": new_my_video,
        "placeholder_img": new_placeholder_img,
    })
Delete files
    file_upload.delete_files(BlogPostModel, files=["my_video"])
Stream a file
    file_upload.stream_file(blog_post, filename="my_video")
File Url paths
    file_upload.get_file_url(blog_post, filename="placeholder_img")

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

flask-file-upload-0.0.9.tar.gz (10.8 kB view hashes)

Uploaded Source

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