Skip to main content

SQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage.

Project description

sqlalchemy-file

SQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage such as Local Storage, Amazon S3, Rackspace CloudFiles, Google Storage and others using Apache Libcloud.

Test suite Publish Codecov Package version Supported Python versions

The key features are:

  • Multiple Storage : Use Object Storage API provided by Apache Libcloud to store files. Therefore, you can store your files on Local Storage, Amazon S3, Google Cloud Storage, MinIO etc, and easily switch between them. For a full list of supported providers visit supported providers page from Apache Libcloud documentation.
  • Validator : Provide an interface for validating each files before saving them.
  • Size Validator : Built-in validator for file maximum size validation.
  • Content-Type Validator : Built-in validator for file mimetype restrictions.
  • Image Validator : Built-in validator for image mimetype, width, height and ratio validation.
  • Processor : Provide an interface to easily save multiple transformation of the original files.
  • ThumbnailGenerator : Built-in processor to auto generate thumbnail
  • Multiple Files : You can attach multiple files directly to a Model.
  • Session awareness : Whenever an object is deleted or a rollback is performed the files uploaded during the unit of work or attached to the deleted objects are automatically deleted.
  • Meant for Evolution : Change the storage provider anytime you want, old data will continue to work
  • SQLModel Support: Tested with SQLModel

Documentation: https://jowilf.github.io/sqlalchemy-file

Source Code: https://github.com/jowilf/sqlalchemy-file


Requirements

A recent and currently supported version of Python (right now, Python supports versions 3.7 and above).

As SQLAlchemy-file is based on Apache Libcloud and SQLAlchemy, it requires them. They will be automatically installed when you install SQLAlchemy-file.

Installation

PIP

$ pip install sqlalchemy-file

Poetry

$ poetry add sqlalchemy-file

Example

Attaching files to models is as simple as declaring a field on the model itself

import os

from libcloud.storage.drivers.local import LocalStorageDriver
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from sqlalchemy_file import File, FileField
from sqlalchemy_file.storage import StorageManager

Base = declarative_base()


# Define your model
class Attachment(Base):
    __tablename__ = "attachment"

    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(String(50), unique=True)
    content = Column(FileField)


# Configure Storage
os.makedirs("./upload_dir/attachment", 0o777, exist_ok=True)
container = LocalStorageDriver("./upload_dir").get_container("attachment")
StorageManager.add_storage("default", container)

# Save your model
engine = create_engine(
    "sqlite:///example.db", connect_args={"check_same_thread": False}
)
Base.metadata.create_all(engine)

with Session(engine) as session, open("./example.txt", "rb") as local_file:
    # from an opened local file
    session.add(Attachment(name="attachment1", content=local_file))

    # from bytes
    session.add(Attachment(name="attachment2", content=b"Hello world"))

    # from string
    session.add(Attachment(name="attachment3", content="Hello world"))

    # from a File object with custom filename and content_type
    file = File(content="Hello World", filename="hello.txt", content_type="text/plain")
    session.add(Attachment(name="attachment4", content=file))

    # from a File object specifying a content path
    session.add(Attachment(name="attachment5", content=File(content_path="./example.txt")))

    session.commit()

Related projects and inspirations

  • filedepot: When I was looking for a library like this, depot was the best I saw. This project inspired SQLAlchemy-file extensively and some features are implemented the same.
  • sqlalchemy-media: Another attachment extension for SqlAlchemy to manage assets which are associated with database models

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

sqlalchemy_file-0.6.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_file-0.6.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_file-0.6.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_file-0.6.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for sqlalchemy_file-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3d578d1b968bdf755f432305845e1831178f351032d8ae61e0441f3522c5bba8
MD5 3e0e661e559e994d683f73bc4e4bde6d
BLAKE2b-256 8c9b81c240992d7691412975f8cc3cd60087713f3f72a49fe22bf7314809b97e

See more details on using hashes here.

File details

Details for the file sqlalchemy_file-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_file-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7297d3d74142bd0e73496450c4664f6d67d13ad375ecf0b1c9f73deed8a870f3
MD5 efb64d1642642d46675a5a0a2e5753f7
BLAKE2b-256 92cd4c2e93ca055ea401528927f0e7f36f8fa45eaa34999291a61a0a943b79de

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