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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3