Successfully implemented configurable artifact storage for AstroML with support for local filesystem, AWS S3, and Google Cloud Storage (GCS). The system uses fsspec for robust cloud storage handling and integrates seamlessly with MLflow tracking.
File: astroml/storage/artifact_store.py
ArtifactStore- Abstract base class defining the storage interfaceLocalArtifactStore- Local filesystem implementationS3ArtifactStore- AWS S3 implementationGCSArtifactStore- Google Cloud Storage implementationcreate_artifact_store()- Factory function for creating stores from URIs
Key Features:
- Unified API across all backends
- fsspec-based implementation for reliability
- Support for save, load, exists, delete, list operations
- Full URI support (file://, s3://, gs://)
File: astroml/storage/config.py
ArtifactStorageConfig- Main configuration classLocalStorageConfig- Local storage settingsS3StorageConfig- S3 settings with credential supportGCSStorageConfig- GCS settings with credential support
Features:
- Pydantic-based validation
- Environment variable support for credentials
- URI generation from config
- Dict serialization/deserialization
File: astroml/tracking/mlflow_tracker.py (Enhanced)
New Parameters:
artifact_uri- URI for artifact storageartifact_store- Pre-configured ArtifactStore instance
New Methods:
log_model_artifact()- Returns artifact URIsave_artifact()- Save arbitrary artifactsload_artifact()- Load artifacts from store
Backward Compatibility:
- All existing code continues to work
- Artifact store is optional
- MLflow logging unchanged
File: astroml/training/config.py (Enhanced)
- Added
artifact_storage: ArtifactStorageConfigfield - Integrates with Hydra configuration system
- Allows per-experiment artifact storage configuration
Created example configurations:
configs/artifact_storage/local.yaml- Local storageconfigs/artifact_storage/s3.yaml- S3 storageconfigs/artifact_storage/gcs.yaml- GCS storage
Updated requirements files:
requirements.txt- Added fsspec, s3fs, gcsfsrequirements-cpu.txt- Added fsspec, s3fs, gcsfs
ARTIFACT_STORAGE.md- Comprehensive configuration and usage guideARTIFACT_STORE_INTEGRATION.md- Integration guide with migration pathexamples/train_with_artifact_store.py- Example training script
File: tests/test_artifact_store.py
Comprehensive test coverage:
- Local storage tests (save, load, exists, delete, list)
- S3 storage tests (mocked)
- GCS storage tests (mocked)
- Factory function tests
- Configuration tests
┌─────────────────────────────────────────────────────────────┐
│ Training Script │
└────────────────────┬────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ MLflowTracker (Enhanced) │
│ - log_model_artifact() │
│ - save_artifact() │
│ - load_artifact() │
└────────────────────┬────────────────────────────────────────┘
│
┌────────────┴────────────┐
│ │
▼ ▼
┌──────────────────┐ ┌──────────────────────┐
│ MLflow Tracking │ │ ArtifactStore │
│ (mlruns/) │ │ (Configurable) │
└──────────────────┘ └──────────┬───────────┘
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────┐ ┌──────────────┐
│ Local FS │ │ S3 │ │ GCS │
│ (file://) │ │(s3://) │ │ (gs://) │
└──────────────┘ └──────────┘ └──────────────┘
from astroml.storage import create_artifact_store
from astroml.tracking import MLflowTracker
# Create artifact store
store = create_artifact_store("s3://my-bucket/models")
# Initialize tracker
tracker = MLflowTracker(artifact_store=store)
# Save model
uri = tracker.log_model_artifact(model, checkpoint_path="best.pth")
print(f"Model saved to: {uri}")from hydra import compose, initialize_config_dir
from astroml.storage import create_artifact_store
cfg = compose(config_name="config")
artifact_uri = cfg.training.artifact_storage.get_artifact_uri()
store = create_artifact_store(artifact_uri)from astroml.storage import S3ArtifactStore
store = S3ArtifactStore("my-bucket", "models")
# Save
uri = store.save("local_model.pth", "exp1/model.pth")
# Load
store.load("exp1/model.pth", "downloaded.pth")
# List
artifacts = store.list_artifacts("exp1")
# Delete
store.delete("exp1/model.pth")-
Multiple Backends
- Local filesystem (development)
- AWS S3 (production)
- Google Cloud Storage (multi-cloud)
-
Unified Interface
- Same API regardless of backend
- Easy to switch backends via configuration
-
fsspec Integration
- Robust cloud storage handling
- Automatic multipart uploads for large files
- Consistent error handling
-
Configuration-Driven
- Define backend via YAML
- Environment variable support
- Credential management
-
MLflow Integration
- Seamless logging to both MLflow and artifact store
- Optional - doesn't break existing code
- Returns artifact URIs for tracking
-
Backward Compatible
- All existing code continues to work
- Artifact store is optional enhancement
- No breaking changes
astroml/
├── storage/
│ ├── __init__.py
│ ├── artifact_store.py # Core implementations
│ └── config.py # Configuration classes
├── tracking/
│ └── mlflow_tracker.py # Enhanced with artifact store
└── training/
└── config.py # Enhanced with artifact storage config
configs/
└── artifact_storage/
├── local.yaml
├── s3.yaml
└── gcs.yaml
tests/
└── test_artifact_store.py # Comprehensive tests
examples/
└── train_with_artifact_store.py # Example training script
Documentation:
├── ARTIFACT_STORAGE.md # Configuration guide
├── ARTIFACT_STORE_INTEGRATION.md # Integration guide
└── ARTIFACT_STORE_SUMMARY.md # This file
fsspec>=2024.2.0 # Filesystem abstraction
s3fs>=2024.2.0 # S3 support
gcsfs>=2024.2.0 # GCS support
Run tests with:
pytest tests/test_artifact_store.py -vTest coverage includes:
- Local storage operations
- S3 operations (mocked)
- GCS operations (mocked)
- Factory function
- Configuration validation
- Install dependencies:
pip install -r requirements.txt - Update training scripts: Add artifact store initialization
- Configure backend: Create artifact_storage config
- Test locally: Use local storage first
- Deploy to cloud: Switch to S3/GCS in production
- Local Storage: Fastest, no network overhead
- S3: Good for AWS environments, supports multipart uploads
- GCS: Good for GCP environments, similar performance to S3
For large models (>1GB):
- Use multipart uploads (automatic)
- Compress models before upload
- Use regional buckets
-
Credentials Management
- Use environment variables for credentials
- Never commit credentials to version control
- Use IAM roles in production
-
Access Control
- Restrict bucket access via IAM policies
- Use service accounts for CI/CD
- Enable bucket versioning for recovery
-
Encryption
- S3: Enable server-side encryption
- GCS: Enable default encryption
- Consider client-side encryption for sensitive models
Potential improvements:
- Model registry integration
- Artifact versioning and tagging
- Automatic cleanup policies
- Artifact compression
- Parallel uploads for large files
- Artifact signing and verification
- Cost tracking and optimization
- Additional cloud providers (Azure, MinIO)
-
ModuleNotFoundError: fsspec
- Solution:
pip install -r requirements.txt
- Solution:
-
NoCredentialsError (S3)
- Solution: Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
-
DefaultCredentialsError (GCS)
- Solution: Set GOOGLE_APPLICATION_CREDENTIALS
-
PermissionError
- Solution: Verify IAM permissions for credentials
See ARTIFACT_STORAGE.md for detailed troubleshooting.
The artifact storage system provides a flexible, extensible solution for managing model artifacts across different storage backends. It integrates seamlessly with existing MLflow tracking while maintaining full backward compatibility.
The implementation follows best practices:
- Abstract base class for extensibility
- Factory pattern for object creation
- Configuration-driven design
- Comprehensive error handling
- Full test coverage
- Clear documentation
This enables teams to:
- Develop locally with filesystem storage
- Deploy to production with S3/GCS
- Switch backends without code changes
- Track artifacts across experiments
- Manage model lifecycle efficiently