Reduce extra S3 ops when working with letter PDFs

Previously we did some unnecessary work:

- Collate task. This had one S3 request to get a summary of the object,
which was then used in another request to get the full object. We only
need the size of the object, which is included in the summary [1].

- Archive task. This had one S3 request to get a summary of the object,
which was then used to make another request to delete it. We still need
both requests, but we can remove the S3.Object in the middle.

[1]: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#objectsummary
This commit is contained in:
Ben Thorner
2021-03-16 11:57:33 +00:00
parent ff7eebc90a
commit c76e789f1e
5 changed files with 22 additions and 38 deletions

View File

@@ -10,7 +10,7 @@ from moto import mock_s3
from app.letters.utils import (
LetterPDFNotFound,
ScanErrorType,
find_letter_pdf_filename,
find_letter_pdf_in_s3,
generate_letter_pdf_filename,
get_bucket_name_and_prefix_for_notification,
get_folder_name,
@@ -49,7 +49,7 @@ def _sample_precompiled_letter_notification_using_test_key(sample_precompiled_le
@mock_s3
def test_find_letter_pdf_filename_returns_filename(sample_notification):
def test_find_letter_pdf_in_s3_returns_object(sample_notification):
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
s3 = boto3.client('s3', region_name='eu-west-1')
s3.create_bucket(
@@ -60,11 +60,11 @@ def test_find_letter_pdf_filename_returns_filename(sample_notification):
_, prefix = get_bucket_name_and_prefix_for_notification(sample_notification)
s3.put_object(Bucket=bucket_name, Key=f'{prefix}-and-then-some', Body=b'f')
assert find_letter_pdf_filename(sample_notification) == f'{prefix}-and-then-some'
assert find_letter_pdf_in_s3(sample_notification).key == f'{prefix}-and-then-some'
@mock_s3
def test_find_letter_pdf_filename_raises_if_not_found(sample_notification):
def test_find_letter_pdf_in_s3_raises_if_not_found(sample_notification):
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
s3 = boto3.client('s3', region_name='eu-west-1')
s3.create_bucket(
@@ -73,7 +73,7 @@ def test_find_letter_pdf_filename_raises_if_not_found(sample_notification):
)
with pytest.raises(LetterPDFNotFound):
find_letter_pdf_filename(sample_notification)
find_letter_pdf_in_s3(sample_notification)
@pytest.mark.parametrize('created_at,folder', [