Merge pull request #9 from sartography/feature/dynamo_2

using locally configured access keys for AWS
This commit is contained in:
Dan Funk 2022-10-12 14:51:03 -04:00 committed by GitHub
commit 1b9ecca6aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 37 deletions

View File

@ -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,
)

View File

@ -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"]

View File

@ -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)

View File

@ -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)

View File

@ -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"])

View File

@ -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,