Skip to content

Commit

Permalink
chore: fix splat from reposting empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
uptickmetachu committed Oct 10, 2024
1 parent 522bac2 commit 038e1e3
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,14 @@ def deliver_pdf_to_presigned_url(payload: Payload, output_filepath: str) -> Resp
message="Invalid presigned URL",
) from e
print("output_filepath=", output_filepath)
with open(output_filepath, "rb") as f:
# 5xx responses are normal for s3, recommendation is to try 10 times
# https://aws.amazon.com/premiumsupport/knowledge-center/http-5xx-errors-s3/
attempts = 0
files = {"file": (output_filepath, f)}
print(f'splat|posting_to_s3|{presigned_url["url"]}|{presigned_url["fields"].get("key")}')
while attempts < S3_RETRY_COUNT:

attempts = 0
while attempts < S3_RETRY_COUNT:
with open(output_filepath, "rb") as f:
# 5xx responses are normal for s3, recommendation is to try 10 times
# https://aws.amazon.com/premiumsupport/knowledge-center/http-5xx-errors-s3/
files = {"file": (output_filepath, f)}
print(f'splat|posting_to_s3|{presigned_url["url"]}|{presigned_url["fields"].get("key")}')
response = requests.post(
presigned_url["url"],
data=presigned_url["fields"],
Expand All @@ -325,16 +326,16 @@ def deliver_pdf_to_presigned_url(payload: Payload, output_filepath: str) -> Resp
print(f"splat|s3_response|{response.status_code}")
if response.status_code in [500, 503]:
attempts += 1
print("splat|s3_retry")
print(f"splat|s3_retry|reason={response.status_code}")
else:
break
else:
print("splat|s3_max_retry_reached")
return Response(
status_code=response.status_code,
headers=response.headers,
body=response.content,
)
else:
print("splat|s3_max_retry_reached")
return Response(
status_code=response.status_code,
headers=response.headers,
body=response.content,
)
if response.status_code != 204:
print(f"splat|presigned_url_save|unknown_error|{response.status_code}|{response.content}")
return Response(
Expand Down

0 comments on commit 038e1e3

Please sign in to comment.