Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Usage
Tomasz Szymański edited this page Sep 17, 2021
·
2 revisions
After setting up the storage using environment variables import the default_storage object from storages.provider module and use it as an interface for storing and retrieving your data.
fromstorages.providerimportdefault_storagedefault_storage.write(
name="example.txt",
content="Lorem ipsum dolor sit amet...",
mode="x",
)file_contents=default_storage.read(name="example.txt")default_storage.delete(name="example.txt")ifdefault_storage.exists(name="example.txt"):
print("Hooray! The resource exists")size_in_bytes=default_storage.size(name="example.txt")
print(f"The file size is: {size_in_bytes} bytes")creation_datetime=default_storage.get_created_time(name="example.txt")modification_datetime=default_storage.get_modified_time(name="example.txt")access_time=default_storage.get_access_time(name="example.txt")It is also possible to use many storages at the same time. In this case you should use the storages.StorageProvider class as a proxy to access the actual storage:
fromstorages.providerimportStorageProviderfile_system_stoarge=StorageProvider.provide(
backend_path="storages.backends.file_system.FileSystemStorage"
)
aws_s3_storage=StorageProvider.provide(
backend_path="storages.backends.amazon_s3.AmazonS3Storage"
)And then you can use any of these storages the same way you'd use the default_storage.
© Merixstudio 2021