diff --git a/connectors/connector-aws/connector_aws/auths/simpleAuth.py b/connectors/connector-aws/connector_aws/auths/simpleAuth.py index e22cbb3..7df85a3 100644 --- a/connectors/connector-aws/connector_aws/auths/simpleAuth.py +++ b/connectors/connector-aws/connector_aws/auths/simpleAuth.py @@ -6,20 +6,24 @@ from botocore.config import Config # type: ignore class SimpleAuth: """Established a simple Boto 3 Client based on an access key and a secret key.""" - def __init__(self, resource_type: str, access_key: str, secret_key: str): + def __init__(self, resource_type: str, config: dict): """ :param access_key: AWS Access Key :param secret_key: AWS Secret Key """ + aws_access_key_id = config["AWS_ACCESS_KEY_ID"] + aws_secret_access_key = config["AWS_SECRET_ACCESS_KEY"] + aws_region = config["AWS_REGION"] + my_config = Config( - region_name="us-east-1", retries={"max_attempts": 10, "mode": "standard"} + region_name=aws_region, retries={"max_attempts": 10, "mode": "standard"} ) # Get the service resource. self.resource = boto3.resource( resource_type, - aws_access_key_id=access_key, - aws_secret_access_key=secret_key, + aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key, config=my_config, ) diff --git a/connectors/connector-aws/connector_aws/commands/addDynamoItem.py b/connectors/connector-aws/connector_aws/commands/addDynamoItem.py index 0a0a193..d22711f 100644 --- a/connectors/connector-aws/connector_aws/commands/addDynamoItem.py +++ b/connectors/connector-aws/connector_aws/commands/addDynamoItem.py @@ -8,29 +8,25 @@ class AddDynamoItem: """Add a new record to a dynamo db table.""" def __init__( - self, access_key: str, secret_key: str, table_name: str, item_data: str + self, table_name: str, item_data: str ): """ - :param access_key: AWS Access Key - :param secret_key: AWS Secret Key :param table_name: The name of hte Dynamo DB table to add information to. - :param item_data: The data to add + :param item_data: The data to add, should be in json format. :return: Json Data structure containing a http status code (hopefully '200' for success..) and a response string. """ - # Get the service resource. - self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource() + self.table_name = table_name + self.item_data = item_data - # Instantiate a table resource object without actually - # creating a DynamoDB table. Note that the attributes of this table - # are lazy-loaded: a request is not made nor are the attribute - # values populated until the attributes - # on the table resource are accessed or its load() method is called. - self.table = self.dynamodb.Table(table_name) - self.item_data = json.loads(item_data) def execute(self, config, task_data): """Execute.""" + # Get the service resource. + self.dynamodb = SimpleAuth("dynamodb", config).get_resource() + self.table = self.dynamodb.Table(self.table_name) + self.item_data = json.loads(self.item_data) + result = self.table.put_item(Item=self.item_data) if "ResponseMetadata" in result: del result["ResponseMetadata"] diff --git a/connectors/connector-aws/connector_aws/commands/queryDynamoTable.py b/connectors/connector-aws/connector_aws/commands/queryDynamoTable.py index 84b03bf..5f27250 100644 --- a/connectors/connector-aws/connector_aws/commands/queryDynamoTable.py +++ b/connectors/connector-aws/connector_aws/commands/queryDynamoTable.py @@ -7,21 +7,20 @@ from connector_aws.auths.simpleAuth import SimpleAuth # type: ignore class QueryDynamoTable: """Return all records for a given partition key.""" - def __init__(self, access_key: str, secret_key: str, table_name: str, key: str): + def __init__(self, table_name: str, key: str): """ - :param access_key: AWS Access Key - :param secret_key: AWS Secret Key :param table_name: The name of hte Dynamo DB table to add information to. :param key: The partition key for what to return. :return: Json Data structure containing the requested data. """ - self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource() - self.table = self.dynamodb.Table(table_name) + self.table_name = table_name self.key = key def execute(self, config, task_data): """Execute.""" - result = self.table.get_item(Key={"primaryKeyName": self.key}) + dynamodb = SimpleAuth("dynamodb", config).get_resource() + table = dynamodb.Table(self.table_name) + result = table.get_item(Key={"primaryKeyName": self.key}) if "ResponseMetadata" in result: del result["ResponseMetadata"] result_str = json.dumps(result) diff --git a/connectors/connector-aws/connector_aws/commands/scanDynamoTable.py b/connectors/connector-aws/connector_aws/commands/scanDynamoTable.py index a7e25a5..492b406 100644 --- a/connectors/connector-aws/connector_aws/commands/scanDynamoTable.py +++ b/connectors/connector-aws/connector_aws/commands/scanDynamoTable.py @@ -7,19 +7,18 @@ from connector_aws.auths.simpleAuth import SimpleAuth # type: ignore class ScanDynamoTable: """Return all records in a given table. Potentially very expensive.""" - def __init__(self, access_key: str, secret_key: str, table_name: str): + def __init__(self, table_name: str): """ - :param access_key: AWS Access Key - :param secret_key: AWS Secret Key :param table_name: The name of hte Dynamo DB table to scan :return: Json Data structure containing the requested data. """ - self.dynamodb = SimpleAuth("dynamodb", access_key, secret_key).get_resource() - self.table = self.dynamodb.Table(table_name) + self.table_name = table_name def execute(self, config, task_data): """Execute.""" - result = self.table.scan() + dynamodb = SimpleAuth("dynamodb", config).get_resource() + table = dynamodb.Table(self.table_name) + result = table.scan() if "ResponseMetadata" in result: del result["ResponseMetadata"] result_str = json.dumps(result) diff --git a/connectors/connector-aws/connector_aws/commands/uploadFile.py b/connectors/connector-aws/connector_aws/commands/uploadFile.py index af23d35..cf85e33 100644 --- a/connectors/connector-aws/connector_aws/commands/uploadFile.py +++ b/connectors/connector-aws/connector_aws/commands/uploadFile.py @@ -8,22 +8,17 @@ class UploadFileData: def __init__( self, - access_key: str, - secret_key: str, file_data: bytes, bucket: str, object_name: str, ): """ - :param access_key: AWS Access Key - :param secret_key: AWS Secret Key :param file_data: Contents of file to be uploaded :param bucket: Bucket to upload to :param object_name: S3 object name. :return: Json Data structure containing a http status code (hopefully '200' for success..) and a response string. """ - self.client = SimpleAuth("s3", access_key, secret_key).get_resource() self.file_data = file_data self.bucket = bucket self.object_name = object_name @@ -31,8 +26,9 @@ class UploadFileData: def execute(self, config, task_data): """Execute.""" # Upload the file + client = SimpleAuth("s3", config).get_resource() try: - result = self.client.Object(self.bucket, self.object_name).put( + result = client.Object(self.bucket, self.object_name).put( Body=self.file_data ) status = str(result["ResponseMetadata"]["HTTPStatusCode"]) diff --git a/connectors/connector-pdf/connector_pdf/commands/create.py b/connectors/connector-pdf/connector_pdf/commands/create.py index 10cb942..7b456bb 100644 --- a/connectors/connector-pdf/connector_pdf/commands/create.py +++ b/connectors/connector-pdf/connector_pdf/commands/create.py @@ -65,8 +65,6 @@ class CreatePDFAndUploadToS3: } aws_result = UploadFileData( - aws_access_key_id, - aws_secret_access_key, pdf_result["response"], aws_bucket, self.aws_object_name,