from urllib.parse import quote

import boto3

from src.aws.aws_api_client import AWSAPIClient


class S3Client(AWSAPIClient):
    __s3_client = boto3.client('s3')

    def upload_file(self, local_file_path: str, bucket_name: str, file_key: str):
        self.__s3_client.upload_file(
            local_file_path,
            Bucket=bucket_name,
            Key=file_key
        )

    def generate_presigned_url(self, bucket_name: str, file_key: str, download_filename: str = ''):
        # presigned_urlを生成
        presigned_url = self.__s3_client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': bucket_name,
                'Key': file_key,
                # 別ファイル名に変更するための仕掛け。Unicode文字はquoteでエスケープが必要
                'ResponseContentDisposition': f'attachment; filename="{quote(download_filename)}"'
            },
            # 有効期限20分
            ExpiresIn=1200
        )

        return presigned_url
