sqlalchemy-file


Namesqlalchemy-file JSON
Version 0.6.0 PyPI version JSON
download
home_pageNone
SummarySQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage.
upload_time2023-10-07 22:57:09
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords apache-libcloud file-upload sqlalchemy sqlmodel
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqlalchemy-file


**SQLAlchemy-file** is a [SQLAlchemy](https://www.sqlalchemy.org/) 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](https://github.com/apache/libcloud).

<p align="center">
<a href="https://github.com/jowilf/sqlalchemy-file/actions">
    <img src="https://github.com/jowilf/sqlalchemy-file/actions/workflows/test.yml/badge.svg" alt="Test suite">
</a>
<a href="https://github.com/jowilf/sqlalchemy-file/actions">
    <img src="https://github.com/jowilf/sqlalchemy-file/actions/workflows/publish.yml/badge.svg" alt="Publish">
</a>
<a href="https://codecov.io/gh/jowilf/sqlalchemy-file">
    <img src="https://codecov.io/gh/jowilf/sqlalchemy-file/branch/main/graph/badge.svg" alt="Codecov">
</a>
<a href="https://pypi.org/project/sqlalchemy-file/">
    <img src="https://badge.fury.io/py/sqlalchemy-file.svg" alt="Package version">
</a>
<a href="https://pypi.org/project/sqlalchemy-file/">
    <img src="https://img.shields.io/pypi/pyversions/sqlalchemy-file?color=2334D058" alt="Supported Python versions">
</a>
</p>


The key features are:

* **Multiple Storage :** Use Object Storage API provided by [Apache Libcloud](https://github.com/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](https://libcloud.readthedocs.io/en/stable/storage/supported_providers.html) 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](https://github.com/tiangolo/sqlmodel)

---

**Documentation**: [https://jowilf.github.io/sqlalchemy-file](https://jowilf.github.io/sqlalchemy-file/)

**Source Code**: [https://github.com/jowilf/sqlalchemy-file](https://github.com/jowilf/sqlalchemy-file)

---

## Requirements

A recent and currently supported version of Python (right
now, <a href="https://www.python.org/downloads/" class="external-link" target="_blank">Python supports versions 3.7 and
above</a>).

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

```shell
$ pip install sqlalchemy-file
```

### Poetry

```shell
$ poetry add sqlalchemy-file
```

## Example

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

```Python
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: ](https://github.com/amol-/depot) 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: ](https://github.com/pylover/sqlalchemy-media) Another attachment extension for SqlAlchemy
to manage assets which are associated with database models

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sqlalchemy-file",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "apache-libcloud,file-upload,sqlalchemy,sqlmodel",
    "author": null,
    "author_email": "Jocelin Hounon <hounonj@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/9b/81c240992d7691412975f8cc3cd60087713f3f72a49fe22bf7314809b97e/sqlalchemy_file-0.6.0.tar.gz",
    "platform": null,
    "description": "# sqlalchemy-file\n\n\n**SQLAlchemy-file** is a [SQLAlchemy](https://www.sqlalchemy.org/) extension for attaching files to SQLAlchemy model and\nuploading them to various storage such as Local Storage, Amazon S3, Rackspace CloudFiles, Google Storage and others\nusing [Apache Libcloud](https://github.com/apache/libcloud).\n\n<p align=\"center\">\n<a href=\"https://github.com/jowilf/sqlalchemy-file/actions\">\n    <img src=\"https://github.com/jowilf/sqlalchemy-file/actions/workflows/test.yml/badge.svg\" alt=\"Test suite\">\n</a>\n<a href=\"https://github.com/jowilf/sqlalchemy-file/actions\">\n    <img src=\"https://github.com/jowilf/sqlalchemy-file/actions/workflows/publish.yml/badge.svg\" alt=\"Publish\">\n</a>\n<a href=\"https://codecov.io/gh/jowilf/sqlalchemy-file\">\n    <img src=\"https://codecov.io/gh/jowilf/sqlalchemy-file/branch/main/graph/badge.svg\" alt=\"Codecov\">\n</a>\n<a href=\"https://pypi.org/project/sqlalchemy-file/\">\n    <img src=\"https://badge.fury.io/py/sqlalchemy-file.svg\" alt=\"Package version\">\n</a>\n<a href=\"https://pypi.org/project/sqlalchemy-file/\">\n    <img src=\"https://img.shields.io/pypi/pyversions/sqlalchemy-file?color=2334D058\" alt=\"Supported Python versions\">\n</a>\n</p>\n\n\nThe key features are:\n\n* **Multiple Storage :** Use Object Storage API provided by [Apache Libcloud](https://github.com/apache/libcloud) to\n  store files. Therefore, you can store your files on Local Storage, Amazon S3, Google Cloud Storage, MinIO etc, and\n  easily switch between them. For a full list of supported providers\n  visit [supported providers page](https://libcloud.readthedocs.io/en/stable/storage/supported_providers.html) from Apache\n  Libcloud documentation.\n* **Validator :**  Provide an interface for validating each files before saving them.\n* **Size Validator :** Built-in validator for file maximum `size` validation.\n* **Content-Type Validator :** Built-in validator for file ``mimetype`` restrictions.\n* **Image Validator :** Built-in validator for image `mimetype`, `width`, `height` and `ratio` validation.\n* **Processor :** Provide an interface to easily save multiple transformation of the original files.\n* **ThumbnailGenerator :** Built-in processor to auto generate thumbnail\n* **Multiple Files :** You can attach multiple files directly to a Model.\n* **Session awareness :** Whenever an object is deleted or a rollback is performed the files uploaded during the unit of\n  work or attached to the deleted objects are automatically deleted.\n* **Meant for Evolution :** Change the storage provider anytime you want, old data will continue to work\n* **SQLModel Support:** Tested with [SQLModel](https://github.com/tiangolo/sqlmodel)\n\n---\n\n**Documentation**: [https://jowilf.github.io/sqlalchemy-file](https://jowilf.github.io/sqlalchemy-file/)\n\n**Source Code**: [https://github.com/jowilf/sqlalchemy-file](https://github.com/jowilf/sqlalchemy-file)\n\n---\n\n## Requirements\n\nA recent and currently supported version of Python (right\nnow, <a href=\"https://www.python.org/downloads/\" class=\"external-link\" target=\"_blank\">Python supports versions 3.7 and\nabove</a>).\n\nAs **SQLAlchemy-file** is based on **Apache Libcloud** and **SQLAlchemy**, it requires them. They will be automatically\ninstalled when you install SQLAlchemy-file.\n\n## Installation\n\n### PIP\n\n```shell\n$ pip install sqlalchemy-file\n```\n\n### Poetry\n\n```shell\n$ poetry add sqlalchemy-file\n```\n\n## Example\n\nAttaching files to models is as simple as declaring a field on the model itself\n\n```Python\nimport os\n\nfrom libcloud.storage.drivers.local import LocalStorageDriver\nfrom sqlalchemy import Column, Integer, String, create_engine\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import Session\nfrom sqlalchemy_file import File, FileField\nfrom sqlalchemy_file.storage import StorageManager\n\nBase = declarative_base()\n\n\n# Define your model\nclass Attachment(Base):\n    __tablename__ = \"attachment\"\n\n    id = Column(Integer, autoincrement=True, primary_key=True)\n    name = Column(String(50), unique=True)\n    content = Column(FileField)\n\n\n# Configure Storage\nos.makedirs(\"./upload_dir/attachment\", 0o777, exist_ok=True)\ncontainer = LocalStorageDriver(\"./upload_dir\").get_container(\"attachment\")\nStorageManager.add_storage(\"default\", container)\n\n# Save your model\nengine = create_engine(\n    \"sqlite:///example.db\", connect_args={\"check_same_thread\": False}\n)\nBase.metadata.create_all(engine)\n\nwith Session(engine) as session, open(\"./example.txt\", \"rb\") as local_file:\n    # from an opened local file\n    session.add(Attachment(name=\"attachment1\", content=local_file))\n\n    # from bytes\n    session.add(Attachment(name=\"attachment2\", content=b\"Hello world\"))\n\n    # from string\n    session.add(Attachment(name=\"attachment3\", content=\"Hello world\"))\n\n    # from a File object with custom filename and content_type\n    file = File(content=\"Hello World\", filename=\"hello.txt\", content_type=\"text/plain\")\n    session.add(Attachment(name=\"attachment4\", content=file))\n\n    # from a File object specifying a content path\n    session.add(Attachment(name=\"attachment5\", content=File(content_path=\"./example.txt\")))\n\n    session.commit()\n\n\n```\n\n## Related projects and inspirations\n\n* [filedepot: ](https://github.com/amol-/depot) When I was looking for a library like this, depot was the\nbest I saw. This project inspired **SQLAlchemy-file** extensively\nand some features are implemented the same.\n* [sqlalchemy-media: ](https://github.com/pylover/sqlalchemy-media) Another attachment extension for SqlAlchemy\nto manage assets which are associated with database models\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "SQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage.",
    "version": "0.6.0",
    "project_urls": {
        "Changelog": "https://jowilf.github.io/sqlalchemy-file/changelog/",
        "Documentation": "https://jowilf.github.io/sqlalchemy-file",
        "Homepage": "https://jowilf.github.io/sqlalchemy-file",
        "Repository": "https://github.com/jowilf/sqlalchemy-file"
    },
    "split_keywords": [
        "apache-libcloud",
        "file-upload",
        "sqlalchemy",
        "sqlmodel"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "92cd4c2e93ca055ea401528927f0e7f36f8fa45eaa34999291a61a0a943b79de",
                "md5": "efb64d1642642d46675a5a0a2e5753f7",
                "sha256": "7297d3d74142bd0e73496450c4664f6d67d13ad375ecf0b1c9f73deed8a870f3"
            },
            "downloads": -1,
            "filename": "sqlalchemy_file-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "efb64d1642642d46675a5a0a2e5753f7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 21427,
            "upload_time": "2023-10-07T22:57:11",
            "upload_time_iso_8601": "2023-10-07T22:57:11.527473Z",
            "url": "https://files.pythonhosted.org/packages/92/cd/4c2e93ca055ea401528927f0e7f36f8fa45eaa34999291a61a0a943b79de/sqlalchemy_file-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8c9b81c240992d7691412975f8cc3cd60087713f3f72a49fe22bf7314809b97e",
                "md5": "3e0e661e559e994d683f73bc4e4bde6d",
                "sha256": "3d578d1b968bdf755f432305845e1831178f351032d8ae61e0441f3522c5bba8"
            },
            "downloads": -1,
            "filename": "sqlalchemy_file-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3e0e661e559e994d683f73bc4e4bde6d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 17316,
            "upload_time": "2023-10-07T22:57:09",
            "upload_time_iso_8601": "2023-10-07T22:57:09.823451Z",
            "url": "https://files.pythonhosted.org/packages/8c/9b/81c240992d7691412975f8cc3cd60087713f3f72a49fe22bf7314809b97e/sqlalchemy_file-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-07 22:57:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jowilf",
    "github_project": "sqlalchemy-file",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlalchemy-file"
}
        
Elapsed time: 0.12340s