Skip to content
Tomasz Szymański edited this page Sep 17, 2021 · 2 revisions

Basic usage

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.

from storages.provider import default_storage

Writing data

default_storage.write(
    name="example.txt",
    content="Lorem ipsum dolor sit amet...",
    mode="x",
)

Reading data

file_contents = default_storage.read(name="example.txt")

Deleting a resource

default_storage.delete(name="example.txt")

Check if a resource exists

if default_storage.exists(name="example.txt"):
    print("Hooray! The resource exists")

Check the size of a resource (in bytes)

size_in_bytes = default_storage.size(name="example.txt")
print(f"The file size is: {size_in_bytes} bytes")

Check the creation time

creation_datetime = default_storage.get_created_time(name="example.txt")

Check the last modification time

modification_datetime = default_storage.get_modified_time(name="example.txt")

Check the last access time

access_time = default_storage.get_access_time(name="example.txt")

Multiple storages

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:

from storages.provider import StorageProvider

file_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.