""" AWS Simple Storage Service (S3) =============================== This module is a set of util functions that relate to AWS S3. """ def extract_bucket_path(url): """Extract the bucket name and path from the provided S3 url Args: url (str): S3 URL like 's3://bucket_name/folder1/folder2/' Return: tuple: bucket name and bucket path """ bucket_path = '' bucket_name = '' if url.startswith('s3://'): split_path = url.split('/', 3) bucket_name = split_path[2] if len(split_path) > 3: bucket_path = split_path[3] if not bucket_name: raise Exception('The S3 url \'{url}\' is not valid.'.format(url=url)) return (bucket_name, bucket_path) def get_destination_s3key_path(bucket, destination_s3_key): """Build s3 url from bucket and s3 key Args: bucket (str): s3 bucket destination_s3key (str): s3 key Return: str: fill s3 object path """ assert bucket and destination_s3_key, 'Invalid destination_s3_key' return 's3://{bucket}/{destination_s3_key}' \ .format( bucket=bucket, destination_s3_key=destination_s3_key)