Create a Cloud Storage Helper Class in Python
Build a simple local file-based helper class that mimics cloud storage operations like save, load, and list JSON objects.
Python code
48 linesimport datetime
import json
from pathlib import Path
class CloudDataHelper:
"""Simple helper for reading/writing JSON files in a cloud-style folder."""
def __init__(self, base_dir: str = "cloud_storage"):
self.base_dir = Path(base_dir)
self.base_dir.mkdir(exist_ok=True)
def save_json(self, key: str, data: dict) -> str:
"""Save dict as JSON file; returns stored file path."""
file_path = self.base_dir / f"{key}.json"
file_path.write_text(json.dumps(data, indent=2))
return str(file_path)
def load_json(self, key: str) -> dict:
"""Load JSON file; returns error dict if missing."""
file_path = self.base_dir / f"{key}.json"
if not file_path.exists():
return {"error": f"{key} not found"}
return json.loads(file_path.read_text())
def list_objects(self) -> list[str]:
"""Return list of stored object names."""
return [p.stem for p in self.base_dir.glob("*.json")]
if __name__ == "__main__":
helper = CloudDataHelper()
sample_data = {
"user": "beginner",
"created_at": datetime.date.today().isoformat(),
"skills": ["python", "cloud"],
}
saved_path = helper.save_json("user_profile", sample_data)
loaded = helper.load_json("user_profile")
missing = helper.load_json("does_not_exist")
objects = helper.list_objects()
print(f"Saved: {saved_path}")
print(f"Loaded: {loaded}")
print(f"Missing test: {missing}")
print(f"Objects in cloud: {objects}")
Output
Saved: cloud_storage/user_profile.json
Loaded: {'user': 'beginner', 'created_at': '2025-03-25', 'skills': ['python', 'cloud']}
Missing test: {'error': 'does_not_exist not found'}
Objects in cloud: ['user_profile']
How it works
The class creates a base directory on initialization and uses pathlib.Path for cross-platform path handling. The save_json method writes a dict as formatted JSON with write_text and optional json.dumps(indent=2). Loading reads the file and parses it with json.loads, returning an error dict if the file is missing. list_objects uses glob to find all .json files and extracts the stem as the object name. This mirrors the basic CRUD operations of cloud object storage like S3 but with a local folder.
Common mistakes
- Forgetting to create the base directory before writing files
- Using `json.load` instead of `json.loads` when reading a string from `read_text`
- Not handling missing files gracefully, causing a FileNotFoundError
- Mixing up key and file path when the key contains subdirectories
Variations
- Use `Path.read_bytes` and `json.loads` for binary-safe storage
- Add an upload method that uses boto3 for real AWS S3 integration
Real-world use cases
- Prototyping cloud storage logic locally before deploying to AWS S3 or Google Cloud Storage.
- Building a small file-based cache or settings store for a script that needs persistent JSON data.
- Teaching beginners how object storage concepts like get, put, and list map to file system operations.
Sponsored
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
- Generate a Mock Presigned URL in Python with HMAC medium
Keep learning
Related tutorials and quizzes for this topic.