Storage

The Storage API is responsible for handling object storage. It provides a simple interface for storing, retrieving and removing files.

Basic Usage

The storage interface is simple, and should be used for all storage operations.

from canvas_core import storage
import io

path = "tests/my_file.txt"

# You can work with any file-like object.
obj = io.StringIO()

# Persist a file to the object store at a given path.
storage.save_file(obj, path)

# Open a persisted object like any other file.
with storage.open_object(path, mode="rw") as obj:
    # do something

# Get the signed URL for a persisted object.
storage.get_object_url(path)
# https://s3.amazonaws.com/...

# Delete the persisted object at the given path.
storage.delete_object(path)

# Get a persisted object as a file (but don't forget to close it when you're done!).
file = storage.get_file(path)
# do something
file.close()

# Write bytes to the object at the given path.
storage.write_object(path, b"test string")

# Read the contents of the object at the given path (as bytes)
storage.read_object(path)
# b"test string"

API Reference

canvas_core.storage.delete_object(path: str | Path) None

Delete file from storage.

canvas_core.storage.generate_upload_path(instance: Model, filename: str) str

Generates an upload path for stored files.

canvas_core.storage.get_object(path: str | Path, mode: str = 'rb') File

Open file at given path.

canvas_core.storage.get_object_url(path: str | Path) str

Get object url.

canvas_core.storage.get_stored_object_model() type[StoredObject]

Return the StoredObject model.

canvas_core.storage.open_object(path: str | Path, mode: str = 'rb') Generator[File, None, None]

Retrieve stored file object.

canvas_core.storage.read_object(path: str | Path) bytes

Get file contents from file as bytes.

canvas_core.storage.save_object(path: str | Path, file: IO | File) str

Save any file object to storage.

Returns:

file name

canvas_core.storage.write_object(path: str | Path, contents: bytes) None

Write bytes to file.