Storage API Reference¶
cs_copilot.storage
¶
Cs_copilot Storage Module
Provides unified storage abstraction for local files and S3/MinIO. Handles session management and file operations transparently.
Main Components:¶
- S3: Storage client class for file operations
- SESSION_ID: Current session identifier
- S3Config: Configuration dataclass for S3 settings
Usage:¶
from cs_copilot.storage import S3
# Read a file
with S3.open("data/compounds.csv", "r") as f:
df = pd.read_csv(f)
# Write a file
with S3.open("results/output.csv", "w") as f:
df.to_csv(f)
# Get S3 path
path = S3.path("results/model.pkl.gz")
S3
¶
Unified storage client for S3/MinIO and local file operations.
All file operations are scoped to the current session by default, unless an absolute S3 URL or local path is provided.
Class Attributes:¶
prefix : str Session-scoped prefix for all relative paths
Methods:¶
path(rel: str) -> str Convert a relative path to an S3 URL
open(rel: str, mode: str = "rb") Open a file for reading or writing
Examples:¶
Write to session-scoped S3 path¶
with S3.open("results.csv", "w") as f: ... f.write("data")
Read from absolute S3 URL¶
with S3.open("s3://bucket/key.csv", "r") as f: ... data = f.read()
Read from local file¶
with S3.open("/tmp/local.csv", "r") as f: ... data = f.read()
Source code in src/cs_copilot/storage/client.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
set_session_prefix(prefix)
classmethod
¶
Set the storage prefix for the current execution context.
Chainlit serves multiple chat sessions in one Python process. A plain
class-level prefix is shared across concurrent chats, so relative
storage paths can leak into another user's session. The context-local
prefix isolates async tasks while keeping S3.prefix as a fallback
for CLI/tests/backwards compatibility.
Source code in src/cs_copilot/storage/client.py
current_prefix()
classmethod
¶
Return the active context-local prefix, falling back to S3.prefix.
path(rel)
classmethod
¶
Convert a relative path to an S3 URL or local session path.
Explicit S3 and local paths are returned unchanged. Relative paths are resolved against the active backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
Relative path or absolute S3 URL |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full S3 URL or local path |
Examples:¶
S3.path("data.csv") 's3://chatbot-assets/sessions/20250121-123456-abc123/data.csv'
S3.path("s3://mybucket/data.csv") 's3://mybucket/data.csv'
Source code in src/cs_copilot/storage/client.py
open(rel, mode='rb')
classmethod
¶
Open a file for reading or writing.
Supports three types of paths: 1. Absolute S3 URLs (s3://...) 2. Local absolute paths (/ or file://) 3. Relative paths (scoped to current session)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
File path (relative, absolute, or S3 URL) |
required |
mode
|
str
|
File mode ('r', 'w', 'rb', 'wb', etc.) |
'rb'
|
Returns:
| Type | Description |
|---|---|
|
File-like object opened with the specified mode |
Examples:¶
Read from session-scoped S3 path¶
with S3.open("data.csv", "r") as f: ... df = pd.read_csv(f)
Write to session-scoped S3 path¶
with S3.open("output.csv", "w") as f: ... df.to_csv(f)
Read from absolute S3 URL¶
with S3.open("s3://bucket/data.csv", "r") as f: ... df = pd.read_csv(f)
Read from local file¶
with S3.open("/tmp/data.csv", "r") as f: ... df = pd.read_csv(f)
Source code in src/cs_copilot/storage/client.py
S3Config
dataclass
¶
Configuration for S3/MinIO storage.
Source code in src/cs_copilot/storage/config.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
from_env()
classmethod
¶
Create S3Config from environment variables.
Environment Variables:¶
Endpoint (one of): - MINIO_ENDPOINT - MINIO_ENDPOINT_URL - S3_ENDPOINT_URL
Access Key (one of): - MINIO_ACCESS_KEY - AWS_ACCESS_KEY_ID
Secret Key (one of): - MINIO_SECRET_KEY - AWS_SECRET_ACCESS_KEY
Bucket Name (one of): - ASSETS_BUCKET - S3_BUCKET_NAME
Other
- AWS_REGION (default: us-east-1)
- USE_S3 (default: false)
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Configuration instance |
Source code in src/cs_copilot/storage/config.py
storage_backend()
¶
Resolve the active storage backend.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
"local", "aws", or "s3-compatible" |
Raises:
| Type | Description |
|---|---|
StorageConfigError
|
If S3 is explicitly enabled but missing required settings. |
Source code in src/cs_copilot/storage/config.py
to_storage_options()
¶
Convert config to fsspec/s3fs storage options.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Storage options for fsspec.open() |
Source code in src/cs_copilot/storage/config.py
StorageConfigError
¶
OutputLayout
dataclass
¶
Structured output path builder for one workflow.
Source code in src/cs_copilot/storage/layout.py
OutputOperation
¶
Bases: str, Enum
Top-level operation folders inside a workflow.
Source code in src/cs_copilot/storage/layout.py
get_s3_config()
¶
Get S3 configuration from environment variables.
This is evaluated at call time (not import time) so that notebooks can call load_dotenv() after importing modules and still have up-to-date credentials and endpoint settings.
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Current S3 configuration |
Source code in src/cs_copilot/storage/config.py
is_s3_enabled()
¶
Check if S3 is enabled based on configuration.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if S3 should be used |
ensure_output_context(session_state=None, *, workflow_slug=None)
¶
Ensure a layout context exists and return it.
One storage session gets one workflow root. The workflow id is derived from the active S3/local session prefix, so tools that do not share the same in-memory session_state still write under the same root.
Source code in src/cs_copilot/storage/layout.py
scoped_artifact_path(path, operation, *folders, session_state=None, workflow_slug=None)
¶
Scope an ordinary relative artifact path into the workflow layout.
Source code in src/cs_copilot/storage/layout.py
client
¶
S3 storage client for unified file operations.
Provides transparent access to files stored in S3/MinIO or locally, with automatic session-based path management.
S3
¶
Unified storage client for S3/MinIO and local file operations.
All file operations are scoped to the current session by default, unless an absolute S3 URL or local path is provided.
Class Attributes:¶
prefix : str Session-scoped prefix for all relative paths
Methods:¶
path(rel: str) -> str Convert a relative path to an S3 URL
open(rel: str, mode: str = "rb") Open a file for reading or writing
Examples:¶
Write to session-scoped S3 path¶
with S3.open("results.csv", "w") as f: ... f.write("data")
Read from absolute S3 URL¶
with S3.open("s3://bucket/key.csv", "r") as f: ... data = f.read()
Read from local file¶
with S3.open("/tmp/local.csv", "r") as f: ... data = f.read()
Source code in src/cs_copilot/storage/client.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
set_session_prefix(prefix)
classmethod
¶
Set the storage prefix for the current execution context.
Chainlit serves multiple chat sessions in one Python process. A plain
class-level prefix is shared across concurrent chats, so relative
storage paths can leak into another user's session. The context-local
prefix isolates async tasks while keeping S3.prefix as a fallback
for CLI/tests/backwards compatibility.
Source code in src/cs_copilot/storage/client.py
current_prefix()
classmethod
¶
Return the active context-local prefix, falling back to S3.prefix.
path(rel)
classmethod
¶
Convert a relative path to an S3 URL or local session path.
Explicit S3 and local paths are returned unchanged. Relative paths are resolved against the active backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
Relative path or absolute S3 URL |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full S3 URL or local path |
Examples:¶
S3.path("data.csv") 's3://chatbot-assets/sessions/20250121-123456-abc123/data.csv'
S3.path("s3://mybucket/data.csv") 's3://mybucket/data.csv'
Source code in src/cs_copilot/storage/client.py
open(rel, mode='rb')
classmethod
¶
Open a file for reading or writing.
Supports three types of paths: 1. Absolute S3 URLs (s3://...) 2. Local absolute paths (/ or file://) 3. Relative paths (scoped to current session)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
File path (relative, absolute, or S3 URL) |
required |
mode
|
str
|
File mode ('r', 'w', 'rb', 'wb', etc.) |
'rb'
|
Returns:
| Type | Description |
|---|---|
|
File-like object opened with the specified mode |
Examples:¶
Read from session-scoped S3 path¶
with S3.open("data.csv", "r") as f: ... df = pd.read_csv(f)
Write to session-scoped S3 path¶
with S3.open("output.csv", "w") as f: ... df.to_csv(f)
Read from absolute S3 URL¶
with S3.open("s3://bucket/data.csv", "r") as f: ... df = pd.read_csv(f)
Read from local file¶
with S3.open("/tmp/data.csv", "r") as f: ... df = pd.read_csv(f)
Source code in src/cs_copilot/storage/client.py
config
¶
Storage configuration module.
Manages S3/MinIO connection settings and environment variable fallbacks.
StorageConfigError
¶
S3Config
dataclass
¶
Configuration for S3/MinIO storage.
Source code in src/cs_copilot/storage/config.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
from_env()
classmethod
¶
Create S3Config from environment variables.
Environment Variables:¶
Endpoint (one of): - MINIO_ENDPOINT - MINIO_ENDPOINT_URL - S3_ENDPOINT_URL
Access Key (one of): - MINIO_ACCESS_KEY - AWS_ACCESS_KEY_ID
Secret Key (one of): - MINIO_SECRET_KEY - AWS_SECRET_ACCESS_KEY
Bucket Name (one of): - ASSETS_BUCKET - S3_BUCKET_NAME
Other
- AWS_REGION (default: us-east-1)
- USE_S3 (default: false)
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Configuration instance |
Source code in src/cs_copilot/storage/config.py
storage_backend()
¶
Resolve the active storage backend.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
"local", "aws", or "s3-compatible" |
Raises:
| Type | Description |
|---|---|
StorageConfigError
|
If S3 is explicitly enabled but missing required settings. |
Source code in src/cs_copilot/storage/config.py
to_storage_options()
¶
Convert config to fsspec/s3fs storage options.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Storage options for fsspec.open() |
Source code in src/cs_copilot/storage/config.py
get_s3_config()
¶
Get S3 configuration from environment variables.
This is evaluated at call time (not import time) so that notebooks can call load_dotenv() after importing modules and still have up-to-date credentials and endpoint settings.
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Current S3 configuration |
Source code in src/cs_copilot/storage/config.py
is_s3_enabled()
¶
Check if S3 is enabled based on configuration.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if S3 should be used |
layout
¶
Session workflow output layout helpers.
OutputOperation
¶
Bases: str, Enum
Top-level operation folders inside a workflow.
Source code in src/cs_copilot/storage/layout.py
OutputLayout
dataclass
¶
Structured output path builder for one workflow.
Source code in src/cs_copilot/storage/layout.py
sanitize_path_part(value, *, default='artifact')
¶
Return a safe single path component while preserving useful suffix dots.
Source code in src/cs_copilot/storage/layout.py
ensure_output_context(session_state=None, *, workflow_slug=None)
¶
Ensure a layout context exists and return it.
One storage session gets one workflow root. The workflow id is derived from the active S3/local session prefix, so tools that do not share the same in-memory session_state still write under the same root.
Source code in src/cs_copilot/storage/layout.py
scoped_artifact_path(path, operation, *folders, session_state=None, workflow_slug=None)
¶
Scope an ordinary relative artifact path into the workflow layout.