Skip to content

[ocr-lambda] S3 다운로드 전 파일 크기 제한 적용 - #507

Merged
s2yeons merged 3 commits into
developfrom
feat/502-ocr-s3-file-size-limit
Sep 30, 2026
Merged

s2yeons merged 3 commits into
developfrom
feat/502-ocr-s3-file-size-limit

Conversation

@s2yeons

@s2yeons s2yeons commented Sep 30, 2026

Copy link
Copy Markdown
Member

개요 💡

이 PR의 목적과 배경을 설명해주세요.

기존 handler.ts는 S3 객체 전체를 메모리에 읽어들인 뒤에야 파일 크기를 검사하였습니다. 이 경우 다운로드가 끝날 때까지 임의로 큰 파일의 전체 바이트를 그대로 메모리에 적재하게 되어, 크기 제한(30MB)이 실질적인 방어로 동작하지 않는 문제가 있었습니다. 다운로드 이전과 다운로드 도중 모두에서 크기를 제한하도록 개선하였습니다.

작업내용 ⌨️

이 PR에서 작업한 구체적인 내용을 작성해주세요.

  • 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 요청이 하나 더 추가되는데, 레이턴시 관점에서 이 정도 오버헤드가 허용 가능한지 확인 부탁드립니다.

@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated
hellogsm-client-26 Skipped Skipped Sep 30, 2026 1:26am UTC
hellogsm-client-26-admin Skipped Skipped Sep 30, 2026 1:26am UTC

Request Review

@pr-agent-demo pr-agent-demo Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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 junjuny0227 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#502 기준으로 수정 후 재검토가 필요합니다. 정확성/위험: 청크 검사만으로 피크 메모리가 30MB 이하임을 보장하지 못합니다. 요구사항: 실제 정상 PDF 사례의 테스트가 없습니다. 로컬에서 PR HEAD 기준 OCR Lambda 테스트 10개, 타입 검사, lint, build는 통과했습니다. 실제 S3의 피크 메모리와 HEAD 추가 지연은 측정하지 않았습니다.

Comment thread apps/ocr-lambda/src/s3Download.ts Resolved
Comment thread apps/ocr-lambda/src/s3Download.test.ts Resolved
@vercel
vercel Bot temporarily deployed to Preview – hellogsm-client-26-admin September 30, 2026 01:26 Inactive
@vercel
vercel Bot temporarily deployed to Preview – hellogsm-client-26 September 30, 2026 01:26 Inactive

@junjuny0227 junjuny0227 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다

@s2yeons
s2yeons merged commit 447f4c4 into develop Sep 30, 2026
5 checks passed
@s2yeons
s2yeons deleted the feat/502-ocr-s3-file-size-limit branch September 30, 2026 01:31

This branch was previously deployed

2 inactive deployments
Preview – hellogsm-client-26-admin — 097808fd Deployed Sep 30, 2026 by vercel[bot]
Preview – hellogsm-client-26 — 097808fd Deployed Sep 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants