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
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 | |
path(rel)
classmethod
¶
Convert a relative path to an S3 URL.
If an absolute S3 URL is provided (starts with s3://), it is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
Relative path or absolute S3 URL |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full S3 URL |
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
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: true)
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Configuration instance |
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 |
Source code in src/cs_copilot/storage/config.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
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 | |
path(rel)
classmethod
¶
Convert a relative path to an S3 URL.
If an absolute S3 URL is provided (starts with s3://), it is returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
Relative path or absolute S3 URL |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full S3 URL |
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.
S3Config
dataclass
¶
Configuration for S3/MinIO storage.
Source code in src/cs_copilot/storage/config.py
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: true)
Returns:
| Name | Type | Description |
|---|---|---|
S3Config |
S3Config
|
Configuration instance |
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 |