最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sqlmodel - Dynamically set path in target cointaier when saving files using SQLAlchemy-files on Azure storage - Stack Overflow

programmeradmin0浏览0评论

I'm using SQLAlchemy file along with SQLModel to manage files on AZURE_BLOB storage. This works fine, but all the files gets saved in the top-level of the Azure Storage container, regardless of the provided file name, path or any extra parameters I could find.

Is there a way to save files to a different folders on the target container?

For example, in the following, I would like to save file1.pdf to /general/, and file2.pdf to /backups/ of the same container.

# storage.py
driver = driver_cls(key=settings.storage_key, secret=settings.storage_secret)

container = driver.get_container(container_name=settings.storage_container)
StorageManager.add_storage(settings.storage_name, container)
StorageManager.set_default(settings.storage_name)

# models.py
class File(SQLModel, table=True):
    id: int = Field(None, primary_key=True)
    file: Union[File, UploadFile, None] = Field(None, sa_column=Column(FileField))

# controler.py
content = ...
file1 = File(content=content, filename=name, extra={'meta_data': meta_data})
file2 = File(content=content, filename=name, extra={'meta_data': meta_data})
with Session(engine) as session:
    session.add(file1)  # <- save this to <container>/general/
    session.add(file2)  # <- save this to <container>/backups/
    sessionmit()

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论