-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
63 lines (47 loc) · 1.88 KB
/
file.py
File metadata and controls
63 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
import asyncio
from sqlalchemy import create_engine
from sqlalchemy.orm import Mapped, Session, mapped_column
from brussels.base import DataclassBase
from brussels.mixins import PrimaryKeyMixin
try:
from obstore.store import MemoryStore # ty: ignore[unresolved-import]
from brussels.types.file import RemoteFile, RemoteMetadata, RemoteStorage
except ImportError as exc:
msg = "This example requires optional dependencies. Install with: pip install 'brussels[file]'"
raise SystemExit(msg) from exc
class Document(DataclassBase, PrimaryKeyMixin):
__tablename__ = "documents"
file: Mapped[RemoteMetadata | None] = mapped_column(
RemoteStorage(
store=MemoryStore(),
),
nullable=True,
default=None,
)
async def main() -> None:
engine = create_engine("sqlite:///:memory:")
DataclassBase.metadata.create_all(engine)
with Session(engine) as session:
doc = Document()
session.add(doc)
session.flush() # ensure doc.id exists before upload
remote_file = RemoteFile.from_metadata(doc, Document.file)
await remote_file.put_async(
b"hello world",
content_type="text/plain",
)
if remote_file.metadata is None or remote_file.metadata.key != f"{doc.id}/file":
msg = "Unexpected key generated for uploaded file."
raise RuntimeError(msg)
content = await remote_file.get_async()
if bytes(content.bytes()) != b"hello world":
msg = "Unexpected content returned from remote file download."
raise RuntimeError(msg)
await remote_file.delete_async()
session.commit()
if doc.file is not None:
msg = "File metadata should be cleared after delete."
raise RuntimeError(msg)
if __name__ == "__main__":
asyncio.run(main())