@@ -52,12 +52,104 @@ uploaded file is a valid image.
52
52
title = Column(String(100), unique=True)
53
53
cover = Column(ImageField(thumbnail_size=(128, 128)))
54
54
```
55
- ## Uploaded Files Information
55
+ ## Upload File
56
+
57
+ Let's say you defined your model like this
58
+ ``` python
59
+ class Attachment (Base ):
60
+ __tablename__ = " attachment"
61
+
62
+ id = Column(Integer, autoincrement = True , primary_key = True )
63
+ name = Column(String(50 ), unique = True )
64
+ content = Column(FileField)
65
+ ```
66
+ and configure your storage like this
67
+ ``` python
68
+ container = LocalStorageDriver(" /tmp/storage" ).get_container(" attachment" )
69
+ StorageManager.add_storage(" default" , container)
70
+ ```
71
+
72
+ ### Save file object
73
+
56
74
Whenever a supported object is assigned to a [ FileField] [ sqlalchemy_file.types.FileField ] or [ ImageField] [ sqlalchemy_file.types.ImageField ]
57
75
it will be converted to a [ File] [ sqlalchemy_file.file.File ] object.
76
+ ``` python
77
+ with Session(engine) as session:
78
+ session.add(Attachment(name = " attachment1" , content = open (" ./example.txt" , " rb" )))
79
+ session.add(Attachment(name = " attachment2" , content = b " Hello world" ))
80
+ session.add(Attachment(name = " attachment3" , content = " Hello world" ))
81
+ file = File(content = " Hello World" , filename = " hello.txt" , content_type = " text/plain" )
82
+ session.add(Attachment(name = " attachment4" , content = file ))
83
+ session.commit()
84
+ ```
85
+ The file itself will be uploaded to your configured storage, and only the [ File] [ sqlalchemy_file.file.File ]
86
+ information will be stored on the database as JSON.
58
87
59
- This is the same object you will get back when reloading the models from database and apart from the file itself which is accessible
60
- through the ` .file ` property, it provides additional attributes described into the [ File] [ sqlalchemy_file.file.File ] documentation itself.
88
+ ### Retrieve file object
89
+
90
+ This is the same [ File] [ sqlalchemy_file.file.File ] object you will get back when reloading the models from database and the file itself is accessible
91
+ through the ` .file ` property.
92
+
93
+ !!! note
94
+ Check the [ File] [ sqlalchemy_file.file.File ] documentation for all default attributes save into the database.
95
+
96
+ ``` python
97
+ with Session(engine) as session:
98
+ attachment = session.execute(
99
+ select(Attachment).where(Attachment.name == " attachment3" )
100
+ ).scalar_one()
101
+ assert attachment.content.saved # saved is True for saved file
102
+ assert attachment.content.file.read() == b " Hello world" # access file content
103
+ assert attachment.content[" filename" ] is not None # `unnamed` when no filename are provided
104
+ assert attachment.content[" file_id" ] is not None # uuid v4
105
+ assert attachment.content[" upload_storage" ] == " default"
106
+ assert attachment.content[" content_type" ] is not None
107
+ assert attachment.content[" uploaded_at" ] is not None
108
+ ```
109
+
110
+ ### Save additional information
111
+
112
+ It's important to note that [ File] [ sqlalchemy_file.file.File ] object inherit from python ` dict ` .
113
+ Therefore, you can add additional information to your file object like a dict object. Just make sure to not use
114
+ the default attributes used by [ File] [ sqlalchemy_file.file.File ] object internally.
115
+
116
+ !!! Example
117
+ ```python
118
+ content = File(open("./example.txt", "rb"),custom_key1="custom_value1", custom_key2="custom_value2")
119
+ content[ "custom_key3"] = "custom_value3"
120
+ attachment = Attachment(name="Dummy", content=content)
121
+
122
+ session.add(attachment)
123
+ session.commit()
124
+ session.refresh(attachment)
125
+
126
+ assert attachment.custom_key1 == "custom_value1"
127
+ assert attachment.custom_key2 == "custom_value2"
128
+ assert attachment["custom_key3"] == "custom_value3"
129
+ ```
130
+
131
+ !!! important
132
+ [ File] [ sqlalchemy_file.file.File ] provides also attribute style access.
133
+ You can access your keys as attributes.
134
+
135
+ ### Metadata
136
+
137
+ * SQLAlchemy-file* store the uploaded file with some metadata. Only ` filename ` and ` content_type ` are sent by default,
138
+ . You can complete with ` metadata ` key inside your [ File] [ sqlalchemy_file.file.File ] object.
139
+
140
+ !!! Example
141
+ ```py hl_lines="2"
142
+ with Session(engine) as session:
143
+ content = File(DummyFile(), metadata={"key1": "val1", "key2": "val2"})
144
+ attachment = Attachment(name="Additional metadata", content=content)
145
+ session.add(attachment)
146
+ session.commit()
147
+ attachment = session.execute(
148
+ select(Attachment).where(Attachment.name == "Additional metadata")
149
+ ).scalar_one()
150
+ assert attachment.content.file.object.meta_data[ "key1"] == "val1"
151
+ assert attachment.content.file.object.meta_data[ "key2"] == "val2"
152
+ ```
61
153
62
154
## Uploading on a Specific Storage
63
155
@@ -119,7 +211,7 @@ Validators can add additional properties to the file object. For example
119
211
the file object.
120
212
121
213
** SQLAlchemy-file** has built-in validators to get started, but you can create your own validator
122
- by extending [ ValidationError ] [ sqlalchemy_file.exceptions.ValidationError ] base class.
214
+ by extending [ Validator ] [ sqlalchemy_file.validators.Validator ] base class.
123
215
124
216
Built-in validators:
125
217
0 commit comments