""" S3 Utils ======== Utils for any S3 operations for ows-accounting. """ import boto3 from botocore.exceptions import ClientError def extract_bucket_path(url): """Extract bucket name and object key from url. Args: url (str): S3 URL like 's3://bucket_name/folder1/folder2/'. Return: tuple: bucket name and bucket path. """ if url.startswith('s3://'): url_parts = url.split('/', 3) return url_parts[2], url_parts[3] raise Exception("The S3 url '{url}' is not valid.".format(url=url)) def get_presigned_url(bucket, key, expiration_time=3600): """Get presigned url. Args: bucket (str): s3 bucket name. key (str): s3 key. expiration_time (int): seconds after which link will be expired. Returns: str: presigned url. """ s3 = boto3.client('s3') return s3.generate_presigned_url( ClientMethod='get_object', Params={ 'Bucket': bucket, 'Key': key }, ExpiresIn=expiration_time) def object_exists(bucket, key): """Check if s3 object exists. Args: bucket (str): Bucket name. For example, myBucket key (str): full path to s3 object. For example, archives/file_part1.gz Returns: bool: True if exists, False otherwise. """ try: client = boto3.client('s3') client.head_object(Bucket=bucket, Key=key) return True except ClientError: return False