No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors. 에러 해결법

꺄악 운석이다·2022년 10월 9일

메인 프로젝트

목록 보기
5/10

Aws S3를 이용해 이미지 업로드 기능을 구현하던 중 아래와 같은 로그가 발생했다.

[WARN] c.a.services.s3.AmazonS3Client:1714 - No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors.

찾아보니 setContentLength 를 지정하지 않거나 IOUtils.toByteArray(inputStream) 사용 시 컨텐츠 길이에 대한 메타 데이터가 없는 경우 컨텐츠 길이 계산을 위해 입력 스트림의 전체 콘텐츠가 메모리로 보내져서 메모리 낭비를 불러온다고 한다.

테스트를 진행할 때는 별 문제 없었으나 성능 저하를 불러 올 수 있겠다는 생각이 들어 수정하기로 하였다.

해결방법

byte[] bytes = IOUtils.toByteArray(inputStream);
objectMetadata.setContentLength(bytes.length);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, byteArrayInputStream, objectMetadata);
client.putObject(putObjectRequest);

위와같은 코드를 추가해주니 더이상 오류 로그가 보이지 않았다.


Reference : https://docs.aws.amazon.com/codeguru/detector-library/java/s3-object-metadata-content-length-check/

profile
멸종은 면하자

0개의 댓글