import io
import sys

import boto3
import yaml
import os


class CDNCredentials:
    def __init__(self, host, downloader_access_key, downloader_access_secret, uploader_access_key, uploader_access_secret):
        self.host = host
        self.downloader_access_key = downloader_access_key
        self.downloader_access_secret = downloader_access_secret
        self.uploader_access_key = uploader_access_key
        self.uploader_access_secret = uploader_access_secret


class CDNManager:
    def __init__(self, credentials: CDNCredentials):
        self.creds = credentials
        self.download_client = boto3.client('s3', endpoint_url=self.creds.host,
                                            aws_access_key_id=self.creds.downloader_access_key,
                                            aws_secret_access_key=self.creds.downloader_access_secret)
        self.upload_client = boto3.client('s3', endpoint_url=self.creds.host,
                                          aws_access_key_id=self.creds.uploader_access_key,
                                          aws_secret_access_key=self.creds.uploader_access_secret)

    def upload(self, bucket: str, filename: str, file_bytes: bytearray):
        try:
            self.upload_client.upload_fileobj(io.BytesIO(file_bytes), bucket, filename)
            print(f'uploaded {filename} ({len(file_bytes)} bytes) to the {bucket}')
            return True
        except Exception as e:
            print(e)
            return False

    def download(self, bucket: str, filename: str):
        try:
            self.download_client.download_file(bucket, filename, filename)
            print(f'downloaded {filename} from the {bucket} to current folder')
            return True
        except Exception as e:
            print(e)
            return False
