diff --git a/src/lambda_function.py b/src/lambda_function.py new file mode 100644 index 0000000..84c8590 --- /dev/null +++ b/src/lambda_function.py @@ -0,0 +1,35 @@ +import os +import json +import boto3 +from transferrer.transfer import transfer_shoot +import logging +logging.basicConfig(level=logging.INFO) + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +ACCESSION_NUMBER = os.getenv("ACCESSION_NUMBER", "2754") + + +def lambda_handler(event, context): + shoots = [single_handler(shoot_id) for shoot_id in get_shoot_ids(event)] + return { + 'statusCode': 200, + 'body': shoots + } + + +def single_handler(shoot_id): + logger.info(f"transferring {shoot_id}") + transfer_shoot( + from_session=boto3.Session(), + to_session=boto3.Session(), + shoot_number=shoot_id, + accession_number=ACCESSION_NUMBER + ) + return shoot_id + + +def get_shoot_ids(event): + for record in event['Records']: + yield json.loads(record['body'])["Message"] \ No newline at end of file diff --git a/src/transferrer/transfer.py b/src/transferrer/transfer.py index fce5948..7f888a3 100644 --- a/src/transferrer/transfer.py +++ b/src/transferrer/transfer.py @@ -1,4 +1,3 @@ -import logging import os import tempfile from transferrer.download import download_shoot diff --git a/src/transferrer/upload.py b/src/transferrer/upload.py index 7a8a71a..b8049dc 100644 --- a/src/transferrer/upload.py +++ b/src/transferrer/upload.py @@ -7,8 +7,10 @@ logger = logging.getLogger(__name__) +TARGET_BUCKET = os.getenv("TARGET_BUCKET", "wellcomecollection-archivematica-staging-transfer-source") -def upload(session, zipfile_path, target_bucket_name="wellcomecollection-archivematica-staging-transfer-source"): + +def upload(session, zipfile_path, target_bucket_name=TARGET_BUCKET): logger.info(f"uploading {zipfile_path} to {target_bucket_name}") get_target_bucket(session, target_bucket_name).upload_file(zipfile_path, f"born-digital-accessions/{os.path.basename(zipfile_path)}") @@ -18,4 +20,4 @@ def get_target_bucket(session, target_bucket): if __name__ == "__main__": - upload(boto3.Session(profile_name=os.environ["AWS_TARGET_PROFILE"]).resource('s3'), sys.argv[1]) + upload(boto3.Session(profile_name=os.environ["AWS_TARGET_PROFILE"]), sys.argv[1])