Skip to content

Commit 6b4afdd

Browse files
[PRMP-990] Add S3 version id support
1 parent 8f5b61d commit 6b4afdd

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

lambdas/models/document_reference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class DocumentReference(BaseModel):
126126
raw_request: str | None = None
127127
s3_bucket_name: str = Field(exclude=True, default=None)
128128
s3_file_key: str = Field(default=None)
129-
s3_version_id: Optional[str] = Field(default=None, exclude=True)
129+
s3_version_id: Optional[str] = Field(default=None, alias="S3VersionID")
130130
s3_upload_key: str = Field(default=None, exclude=True)
131131
status: Literal["current", "superseded", "entered-in-error"] = Field(
132132
default="current"

lambdas/repositories/bulk_upload/bulk_upload_s3_repository.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def check_virus_result(
6868
f"Verified that all documents for patient {staging_metadata.nhs_number} are clean."
6969
)
7070

71-
def copy_to_lg_bucket(self, source_file_key: str, dest_file_key: str):
72-
self.s3_repository.copy_across_bucket(
71+
def copy_to_lg_bucket(self, source_file_key: str, dest_file_key: str):
72+
result = self.s3_repository.copy_across_bucket(
7373
source_bucket=self.staging_bucket_name,
7474
source_file_key=source_file_key,
7575
dest_bucket=self.lg_bucket_name,
@@ -78,6 +78,8 @@ def copy_to_lg_bucket(self, source_file_key: str, dest_file_key: str):
7878
self.source_bucket_files_in_transaction.append(source_file_key)
7979
self.dest_bucket_files_in_transaction.append(dest_file_key)
8080

81+
return result
82+
8183
def remove_ingested_file_from_source_bucket(self):
8284
for source_file_key in self.source_bucket_files_in_transaction:
8385
self.s3_repository.delete_object(

lambdas/services/bulk_upload_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,13 @@ def create_lg_records_and_copy_files(
363363
source_file_key = self.file_path_cache[file_metadata.file_path]
364364
dest_file_key = document_reference.s3_file_key
365365

366-
self.bulk_upload_s3_repository.copy_to_lg_bucket(
366+
copy_result = self.bulk_upload_s3_repository.copy_to_lg_bucket(
367367
source_file_key=source_file_key, dest_file_key=dest_file_key
368368
)
369369
s3_bucket_name = self.bulk_upload_s3_repository.lg_bucket_name
370370

371+
document_reference.s3_version_id = copy_result.get("VersionId")
372+
371373
document_reference.file_size = (
372374
self.bulk_upload_s3_repository.s3_repository.get_file_size(
373375
s3_bucket_name=s3_bucket_name, object_key=dest_file_key

lambdas/services/pdf_stitching_service.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def process_message(self, stitching_message: PdfStitchingSqsMessage):
101101
stitch_file_size=sys.getsizeof(stitching_data_stream),
102102
)
103103
self.upload_stitched_file(stitching_data_stream=stitching_data_stream)
104+
self.update_stitched_reference()
104105
self.migrate_multipart_references()
105106
self.write_stitching_reference()
106107
self.publish_nrl_message(
@@ -129,6 +130,12 @@ def create_stitched_reference(
129130
deep=True,
130131
)
131132

133+
def update_stitched_reference(self):
134+
self.stitched_reference.s3_version_id = self.s3_service.get_head_object(
135+
self.target_bucket,
136+
self.stitched_reference.s3_file_key
137+
).get("VersionId")
138+
132139
def process_stitching(self, s3_object_keys: list[str]) -> BytesIO:
133140
pdf_writer = PdfWriter()
134141

lambdas/services/upload_document_reference_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def _finalize_and_supersede_with_transaction(self, new_document: DocumentReferen
229229
"file_size",
230230
"uploaded",
231231
"uploading",
232+
"s3_version_id",
232233
},
233234
)
234235

0 commit comments

Comments
 (0)