This release is a pre-release and may not be stable for production use.
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.
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
sizevalidation. - Content-Type Validator : Built-in validator for file
mimetyperestrictions. - Image Validator : Built-in validator for image
mimetype,width,heightandratiovalidation. - 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
Python 3.11 or 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
uv
$ uv add sqlalchemy-file
Optional dependencies (extras)
Some features need third-party packages that are not installed by default:
| Extra | Installs | Required for |
|---|---|---|
sqlalchemy-file[pillow] |
Pillow |
ImageField, ImageValidator, ThumbnailGenerator |
sqlalchemy-file[local] |
fasteners |
LocalStorageDriver backend (required by libcloud's local storage) |
sqlalchemy-file[all] |
Pillow + fasteners |
Everything above |
$ pip install "sqlalchemy-file[local,pillow]"
$ uv add "sqlalchemy-file[all]"
Without the matching extra you get an explicit ImportError telling you which extra to install
(e.g. use ImageField without Pillow, or the local storage driver without fasteners).
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
Metadata
Release files for sqlalchemy-file 0.7.0rc2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sqlalchemy_file-0.7.0rc2.tar.gz | 18.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlalchemy_file-0.7.0rc2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 40.8 kB
Release files / sqlalchemy_file-0.7.0rc2.tar.gz
| Download URL | sqlalchemy_file-0.7.0rc2.tar.gz |
|---|---|
| Size | 18.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53e6924434ea0da5a968852e004a4aed86daa81b87674e579bba377a72454d78
|
|
BLAKE2b-256 checksum How to use checksums |
b06a682f323674cf053fc4b76ae7c8913f7ddedd3c92204cfffdf196e395afa2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / sqlalchemy_file-0.7.0rc2-py3-none-any.whl
| Download URL | sqlalchemy_file-0.7.0rc2-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
91e9c0e38bfdb09a6b88a77f21a380047b7a5189dbdde649e3bf9eecebc720a5
|
|
BLAKE2b-256 checksum How to use checksums |
60159398e5eb77a4ab5e1fdd38b07f0fbfc7b648fa36e0475a92e6cdc89e7f17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for sqlalchemy-file 0.7.0rc2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.