You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EllarStorage Module adds support for cloud and local file storage
13
-
management using [apache `libcloud`](https://github.com/apache/libcloud) package to your Ellar application.
12
+
The EllarStorage Module enriches your Ellar application with robust support for managing both cloud and
13
+
local file storage.
14
+
Leveraging the capabilities of the [Apache `libcloud`](https://github.com/apache/libcloud) package
15
+
to simplify file storage operations within your Ellar-powered projects.
14
16
15
17
## Installation
16
18
```shell
17
19
$(venv) pip install ellar-storage
18
20
```
19
21
20
-
This library was inspired by[sqlalchemy-file](https://github.com/jowilf/sqlalchemy-file)
22
+
This library drew inspiration from[sqlalchemy-file](https://github.com/jowilf/sqlalchemy-file).
21
23
22
-
23
-
## **Usage**
24
-
Follow Ellar project scaffold here, then you configure your module.
24
+
## Usage
25
+
To integrate EllarStorage into your project, follow the standard Ellar project structure and then configure the module as follows:
25
26
26
27
### StorageModule
27
-
Just like every other ellar `Module`s, `StorageModule`
28
-
can be configured directly in where its used or through application config.
29
-
30
-
### **StorageModule.setup**
31
-
Quick example using `StorageModule.setup` method.
28
+
Similar to other Ellar modules, the `StorageModule` can be configured directly where it's used or through the application configuration.
32
29
33
-
Pattern of configuring Storages are in key-word patterns
34
-
where the `key` is `Folder name/Container` and value is `StorageDriver` init properties.
35
-
Example is shown below:
30
+
### StorageModule.setup
31
+
You can set up the `StorageModule` using the `setup` method. Here's a quick example:
36
32
37
33
```python
38
34
import os
@@ -64,23 +60,11 @@ class ApplicationModule(ModuleBase):
64
60
pass
65
61
```
66
62
67
-
In the above illustration, When application initialization is complete,
68
-
`files`, `images` and `documents` will be created in `os.path.join(BASE_DIRS, "media")`.
69
-
Each is configured to be managed by Local Storage Driver.
70
-
See other supported [storage drivers](https://libcloud.readthedocs.io/en/stable/storage/supported_providers.html#provider-matrix)
71
-
72
-
Each storage required `key` and some other parameters for object instantiation,
73
-
so those should be provided in the `options` as a key-value pair.
63
+
In this example, after application initialization, folders for `files`, `images`, and `documents` will be created in the specified directory. Each folder is configured to be managed by a local storage driver. You can explore other supported [storage drivers](https://libcloud.readthedocs.io/en/stable/storage/supported_providers.html#provider-matrix).
74
64
75
-
Also, `default` parameter defines container/folder of choice when saving/retrieving
76
-
a file if storage container was specified.
77
-
It is important to note that if `default` is not set,
78
-
it will default to the first storage container which in this can is `files`.
65
+
### StorageModule.register_setup
66
+
Alternatively, you can move the storage configuration to the application config:
79
67
80
-
81
-
### **StorageModule.register_setup**
82
-
Alternatively, we can move the storage configuration to application Config and everything will still work fine.
83
-
For example:
84
68
```python
85
69
## project_name/root_module.py
86
70
@@ -93,7 +77,7 @@ class ApplicationModule(ModuleBase):
93
77
pass
94
78
```
95
79
96
-
Then in `config.py` add the following code:
80
+
Then, in `config.py`, you can define the storage configurations:
97
81
98
82
```python
99
83
import os
@@ -103,7 +87,6 @@ from ellar_storage import get_driver, Provider
103
87
104
88
BASE_DIRS= Path(__file__).parent
105
89
106
-
107
90
classDevelopmentConfig(ConfigDefaultTypesMixin):
108
91
DEBUG=True
109
92
@@ -127,10 +110,7 @@ class DevelopmentConfig(ConfigDefaultTypesMixin):
127
110
```
128
111
129
112
### StorageService
130
-
131
-
At the end of `StorageModule` setup, `StorageService` is registered into an Ellar DI system.
132
-
A quick way to test this would be through application instance.
133
-
For example:
113
+
At the end of the `StorageModule` setup, `StorageService` is registered into the Ellar DI system. Here's a quick example of how to use it:
134
114
135
115
```python
136
116
## project_name/server.py
@@ -141,7 +121,6 @@ from ellar.common import datastructures, constants
141
121
from ellar.core import LazyModuleImport as lazyLoad
"path": res.get_cdn_url(), # since we are using a local storage, this will return a path to the file
235
-
"filename": res.filename,
236
-
'media_type': res.content_type
237
-
}
238
-
239
-
@delete("/", response=dict)
240
-
defdelete_file(self, path: Query[str]):
241
-
self._storage_service.delete(path)
242
-
return""
243
-
```
244
-
245
-
See [Sample Project]()
173
+
### StorageService
246
174
175
+
-**_save(self, file: UploadFile, upload_storage: Optional[str] = None) -> StoredFile_**: Saves a file from an `UploadFile` object.
176
+
-**_save_async(self, file: UploadFile, upload_storage: Optional[str] = None) -> StoredFile_**: Asynchronously saves a file from an `UploadFile` object.
177
+
-**_save_content(self, **kwargs) -> StoredFile_**: Saves a file from content/bytes or through a file path.
178
+
-**_save_content_async(self, **kwargs) -> StoredFile_**: Asynchronously saves a file from content/bytes or through a file path.
179
+
-**_get(self, path: str) -> StoredFile_**: Retrieves a saved file if the specified `path` exists. The `path` can be in the format `container/filename.extension` or `filename.extension`.
180
+
-**_get_async(self, path: str) -> StoredFile_**: Asynchronously retrieves a saved file if the specified `path` exists.
181
+
-**_delete(self, path: str) -> bool_**: Deletes a saved file if the specified `path` exists.
182
+
-**_delete_async(self, path: str) -> bool_**: Asynchronously deletes a saved file if the specified `path` exists.
183
+
-**_get_container(self, name: Optional[str] = None) -> Container_**: Gets a `libcloud.storage.base.Container` instance for a configured storage setup.
247
184
248
185
### StoredFile
249
-
`StoredFile` is file-like object returned from saving and retrieving saved files.
250
-
Its also extends some `libcloud` Object methods
251
-
and has reference to the `libcloud` Object retrieved from the `libcloud` storage container.
0 commit comments