Retrieve metadata JSON from S3. Parameters ---------- s3_file_path : str The S3 file path for the digest (metadata will use .json extension). Returns ------- S3Metadata | None The metadata struct if found, None otherwise.
(s3_file_path: str)
| 343 | |
| 344 | |
| 345 | def get_metadata_from_s3(s3_file_path: str) -> S3Metadata | None: |
| 346 | """Retrieve metadata JSON from S3. |
| 347 | |
| 348 | Parameters |
| 349 | ---------- |
| 350 | s3_file_path : str |
| 351 | The S3 file path for the digest (metadata will use .json extension). |
| 352 | |
| 353 | Returns |
| 354 | ------- |
| 355 | S3Metadata | None |
| 356 | The metadata struct if found, None otherwise. |
| 357 | |
| 358 | """ |
| 359 | if not is_s3_enabled(): |
| 360 | return None |
| 361 | |
| 362 | # Generate metadata file path by replacing .txt with .json |
| 363 | metadata_file_path = s3_file_path.replace(".txt", ".json") |
| 364 | |
| 365 | try: |
| 366 | s3_client = create_s3_client() |
| 367 | bucket_name = get_s3_bucket_name() |
| 368 | |
| 369 | # Get the metadata object |
| 370 | response = s3_client.get_object(Bucket=bucket_name, Key=metadata_file_path) |
| 371 | metadata_content = response["Body"].read().decode("utf-8") |
| 372 | |
| 373 | return S3Metadata.model_validate_json(metadata_content) |
| 374 | except ClientError as err: |
| 375 | # Object doesn't exist if we get a 404 error |
| 376 | error_code = err.response.get("Error", {}).get("Code") |
| 377 | if error_code == "404": |
| 378 | logger.info("Metadata file not found", extra={"metadata_file_path": metadata_file_path}) |
| 379 | return None |
| 380 | # Log other errors but don't fail |
| 381 | logger.warning("Failed to retrieve metadata from S3", extra={"error": str(err)}) |
| 382 | return None |
| 383 | except Exception as exc: |
| 384 | # For any other exception, log and return None |
| 385 | logger.warning("Unexpected error retrieving metadata from S3", extra={"error": str(exc)}) |
| 386 | return None |
| 387 | |
| 388 | |
| 389 | def _build_s3_url(key: str) -> str: |
no test coverage detected