Recently, i started working on a personal project where i want to build an application which is cloud agnostic - i.e. it can be deployed on any cloud provider with minimal/no code changes. Primary requirement is to separate business logic with cloud provider specific logic.
In this post, i want to share the approach which was followed.
Below code uses python
from abc import ABC, abstractmethod class IObjectStorage(ABC): @abstractmethod def upload_object_to_bucket(self, file_name, file_content): _raise an error that method is not implemented_
class AWSObjectStorageConnector(IObjectStorage): def __init__(self, bucket_name): _Initialize a s3 client using boto3 and initialize a variable using bucket name_ def upload_object_to_bucket(self, file_name, file_content): _Implement the logic to upload the file to AWS S3 bucket_
This method takes a cloud provider variable which will be passed from the calling method
def get_object_storage(cloud_provider, bucket_name) -> IObjectStorage: if cloud_provider == 'aws': return AWSObjectStorageConnector(bucket_name=bucket_name) else: raise ValueError(f'Unsupported cloud provider: {cloud_provider}')
cloud_provider variable will be read from an environment variable passed as input. This ensures that the same logic works fine with different cloud providers.
object_storage_connector = get_object_storage(cloud_provider=provider, bucket_name=bucket_name)
Please feel free to comment with any suggestions or feedback.
The above is the detailed content of Developing cloud agnostic application. For more information, please follow other related articles on the PHP Chinese website!