|
| 1 | +# AWS Utilities |
| 2 | +# Utilities for working with AWS services |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +import boto3 |
| 7 | +from botocore.errorfactory import ClientError |
| 8 | + |
| 9 | + |
| 10 | +class AwsUtilities: |
| 11 | + def __init__(self): |
| 12 | + |
| 13 | + self.logger = logging.getLogger(__name__) |
| 14 | + self.logger.setLevel("DEBUG") |
| 15 | + |
| 16 | + self.s3 = boto3.resource("s3") |
| 17 | + |
| 18 | + def determine_bucket_name(self, path): |
| 19 | + """ |
| 20 | + For the given path, return the bucket name, if path is a valid s3 URL. |
| 21 | + :param path: Valid s3 URL. |
| 22 | + :return: |
| 23 | + """ |
| 24 | + if self.determine_valid_s3_path(path=path): |
| 25 | + self.logger.debug("Parsing %s" % path) |
| 26 | + |
| 27 | + if "s3://" in path: |
| 28 | + path = path[path.startswith("s3://") and len("s3://") :] |
| 29 | + |
| 30 | + self.logger.debug("Path is now %s" % path) |
| 31 | + |
| 32 | + bucket_name = path.split("/")[0] |
| 33 | + |
| 34 | + self.logger.debug("Bucket name is %s" % bucket_name) |
| 35 | + |
| 36 | + elif "s3" in path and ".amazonaws.com" in path: |
| 37 | + if path.startswith("http://"): |
| 38 | + |
| 39 | + path = path[len("http://") :] |
| 40 | + |
| 41 | + self.logger.debug("Path is now %s" % path) |
| 42 | + |
| 43 | + elif path.startswith("https://"): |
| 44 | + |
| 45 | + path = path[len("https://") :] |
| 46 | + self.logger.debug("Path is now %s" % path) |
| 47 | + |
| 48 | + else: |
| 49 | + error_msg = "It appears the URL is not valid. %s" % path |
| 50 | + |
| 51 | + self.logger.error(error_msg) |
| 52 | + raise Exception(error_msg) |
| 53 | + |
| 54 | + bucket_name = path.split(".")[0] |
| 55 | + else: |
| 56 | + error_msg = "It appears the URL is not a valid s3 path. %s" % path |
| 57 | + |
| 58 | + self.logger.error(error_msg) |
| 59 | + raise Exception(error_msg) |
| 60 | + |
| 61 | + return bucket_name |
| 62 | + |
| 63 | + def determine_file_key(self, path): |
| 64 | + """ |
| 65 | + Determine the key of the s3 file based on the filepath provided. |
| 66 | + :param path: Full s3 filepath. Can be in s3:// or http(s):// format. |
| 67 | + :type path: str |
| 68 | + :return: |
| 69 | + """ |
| 70 | + |
| 71 | + if "s3://" in path: |
| 72 | + groups = path.split("/", 3) |
| 73 | + |
| 74 | + key = groups[3] |
| 75 | + |
| 76 | + elif "s3" in path and ".amazonaws.com" in path: |
| 77 | + groups = path.split(".amazonaws.com/") |
| 78 | + |
| 79 | + key = groups[1] |
| 80 | + |
| 81 | + else: |
| 82 | + error_msg = "It appears the URL is not valid. %s" % path |
| 83 | + |
| 84 | + self.logger.error(error_msg) |
| 85 | + raise Exception(error_msg) |
| 86 | + |
| 87 | + return key |
| 88 | + |
| 89 | + def determine_s3_file_exists(self, path): |
| 90 | + """ |
| 91 | + Determine if a file exists on s3 based on given path. |
| 92 | + :param path: Full s3 filepath. Can be in s3:// or http(s):// format. |
| 93 | + :type path: str |
| 94 | + :return: |
| 95 | + """ |
| 96 | + self.logger.info("Determining if %s exists." % path) |
| 97 | + |
| 98 | + bucket_name = self.determine_bucket_name(path=path) |
| 99 | + |
| 100 | + key = self.determine_file_key(path=path) |
| 101 | + |
| 102 | + try: |
| 103 | + self.s3.Object(bucket_name, key).load() |
| 104 | + |
| 105 | + return True |
| 106 | + |
| 107 | + except ClientError: |
| 108 | + error_msg = "File %s does not exist in s3." % path |
| 109 | + self.logger.error(error_msg) |
| 110 | + |
| 111 | + return False |
| 112 | + |
| 113 | + def determine_valid_s3_path(self, path): |
| 114 | + """ |
| 115 | + Take the provided path and determine if valid s3 URL. |
| 116 | + :param path: Full s3 filepath. Can be in s3:// or http(s):// format. |
| 117 | + :type path: str |
| 118 | + :return: |
| 119 | + """ |
| 120 | + self.logger.debug("Validating %s" % path) |
| 121 | + if "s3://" in path: |
| 122 | + self.logger.debug("s3:// in path.") |
| 123 | + return True |
| 124 | + elif "s3" in path and ".amazonaws.com" in path: |
| 125 | + self.logger.debug("s3 and .amazonaws.com in path") |
| 126 | + return True |
| 127 | + else: |
| 128 | + self.logger.error("Path is invalid.") |
| 129 | + return False |
| 130 | + |
| 131 | + def get_s3_bucket(self, bucket_name): |
| 132 | + |
| 133 | + return self.s3.Bucket(bucket_name) |
| 134 | + |
| 135 | + def read_from_s3(self, bucket_name, filename): |
| 136 | + """ |
| 137 | + With a given bucket and filename, read from s3. |
| 138 | + :param bucket: |
| 139 | + :param file: |
| 140 | + :return: |
| 141 | + """ |
| 142 | + |
| 143 | + bucket = self.get_s3_bucket(bucket_name=bucket_name) |
0 commit comments