Repository navigation
[ocr-lambda] S3 다운로드 전 파일 크기 제한 적용 - #507
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
There was a problem hiding this comment.
-
Overview:
OCR Lambda가 S3에서 파일을 받을 때 기존 “전체 다운로드 후 크기 확인” 방식을 개선하여, 다운로드 전HeadObjectCommand로 크기를 먼저 검증하고, 다운로드 중에도 스트림을 청크 단위로 검사하여 30MB 초과 시 즉시 중단하도록 변경합니다. -
Intent:
큰 파일이 메모리에 무제한으로 적재되는 문제를 막아, S3 객체 다운로드 단계에서도 파일 크기 제한이 실제 방어 논리로 동작하게 하는 것이 목적입니다. -
Risk:
전반적으로 낮음~중간.HeadObjectCommand로 S3 요청이 1회 추가되는 레이턴시/비용 증가 여부- Head 실패 시 404로 통일된 에러 응답이 실제 원인(권한, 네트워크, 객체 미존재 등)을 가릴 가능성
- 스트림
destroy()처리가 AWS SDK/Node 환경에서 예상대로 동작하는지, 특히 예외 전파와 리소스 정리 부분 readBodyWithLimit의 청크 누적 로직이 경계값(정확히 limit, limit+1)과 특수한 스트림 케이스에서 안정적으로 동작하는지
0 inline comment(s)
junjuny0227
requested changes
Sep 30, 2026
junjuny0227
left a comment
Member
There was a problem hiding this comment.
#502 기준으로 수정 후 재검토가 필요합니다. 정확성/위험: 청크 검사만으로 피크 메모리가 30MB 이하임을 보장하지 못합니다. 요구사항: 실제 정상 PDF 사례의 테스트가 없습니다. 로컬에서 PR HEAD 기준 OCR Lambda 테스트 10개, 타입 검사, lint, build는 통과했습니다. 실제 S3의 피크 메모리와 HEAD 추가 지연은 측정하지 않았습니다.
vercel
Bot
temporarily deployed
to
Preview – hellogsm-client-26-admin
September 30, 2026 01:26
Inactive
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
개요 💡
기존
handler.ts는 S3 객체 전체를 메모리에 읽어들인 뒤에야 파일 크기를 검사하였습니다. 이 경우 다운로드가 끝날 때까지 임의로 큰 파일의 전체 바이트를 그대로 메모리에 적재하게 되어, 크기 제한(30MB)이 실질적인 방어로 동작하지 않는 문제가 있었습니다. 다운로드 이전과 다운로드 도중 모두에서 크기를 제한하도록 개선하였습니다.작업내용 ⌨️
HeadObjectCommand로 S3 객체를 다운로드하기 전에Content-Length를 먼저 확인하여,30MB를 초과하는 파일은 다운로드 자체를 시작하지 않고 거부하도록 하였습니다.GetObjectCommand로 받은Body스트림을 한 번에transformToByteArray()하던 방식 대신, 청크 단위로 순회하며 누적 크기를 검사하도록 변경하였습니다. 누적 크기가 상한을 넘는 즉시 스트림을destroy()하여,Content-Length가 없거나 실제 전송량과 다르더라도 메모리 사용량이 제한되도록 하였습니다.s3Download.ts의readBodyWithLimit함수와FileTooLargeError로 분리하여,S3Client를 목킹하지 않고도 순수 함수 단위로 테스트할 수 있도록 하였습니다.s3Download.test.ts에 다음 케이스에 대한 테스트를 추가하였습니다.FileTooLargeError가 발생하는지Content-Length정보 없이 무한히 이어지는 스트림도 상한 도달 즉시 소비가 중단되는지관련 이슈 🚨
closes #502
리뷰 요청사항 👀
HeadObjectCommand로 별도의 S3 요청이 하나 더 추가되는데, 레이턴시 관점에서 이 정도 오버헤드가 허용 가능한지 확인 부탁드립니다.