From 1ff729e75f5ea7050dd88f67113015f7585cbda6 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 8 Aug 2018 17:21:16 +0100 Subject: [PATCH] read first file returned from s3 rather than looping through, just use `next`. We only expect to return one file, so this won't actually affect the code flow. If, for whatever reason, the file isn't on s3, whereas previously it would error when trying to return an uninstantiated variable, now it will raise a StopIteration --- app/letters/utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/letters/utils.py b/app/letters/utils.py index c14cc80d1..a293663c1 100644 --- a/app/letters/utils.py +++ b/app/letters/utils.py @@ -135,14 +135,15 @@ def get_letter_pdf(notification): s3 = boto3.resource('s3') bucket = s3.Bucket(bucket_name) - for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification, is_test_letter)): - obj = s3.Object( - bucket_name=bucket_name, - key=item.key - ) - file_content = obj.get()["Body"].read() + item = next(x for x in bucket.objects.filter( + Prefix=get_bucket_prefix_for_notification(notification, is_test_letter) + )) - return file_content + obj = s3.Object( + bucket_name=bucket_name, + key=item.key + ) + return obj.get()["Body"].read() def _move_s3_object(source_bucket, source_filename, target_bucket, target_filename):