"""Boto3 s3 file operations.""" from botocore.exceptions import ClientError from mypy_boto3_s3.literals import StorageClassType from vectororder.connectors import s3 def check_s3_file_exists(bucket_name: str, file_key: str) -> bool: """Check if the file exists in the s3 bucket. Args: bucket_name (str): name of bucket where file is stored. file_key (str): path to file for which existence to check. Returns: bool """ try: s3_client = s3.get_s3_client() file_response = s3_client.head_object(Bucket=bucket_name, Key=file_key) status = file_response["ResponseMetadata"]["HTTPStatusCode"] return status == 200 except ClientError: return False def get_s3_file_storage_class(bucket_name: str, file_key: str) -> StorageClassType: """Get s3 files storage class. Args: bucket_name (str): name of bucket where file is stored. file_key (str): path to file for which StorageClass should be returned. Returns: StorageClassType """ s3_client = s3.get_s3_client() file_response = s3_client.head_object(Bucket=bucket_name, Key=file_key) storage_class = file_response.get("StorageClass", "STANDARD") return storage_class def create_presigned_url( bucket: str, key: str, expiration: int = 3600, content_disposition: str | None = None, ) -> str: """Generate a presigned URL to share an S3 object. Args: bucket (str): The bucket where the S3 object is located in. key (str): The key where the S3 object is located at. expiration (int, optional): The expiration time in seconds. Defaults to 3600. content_disposition (str, optional): Content disposition for the URL. Defaults to None Returns: str: The presigned URL. """ params = {"Bucket": bucket, "Key": key} if content_disposition is not None: params["ResponseContentDisposition"] = content_disposition return s3.get_s3_client().generate_presigned_url( ClientMethod="get_object", Params=params, ExpiresIn=expiration )