Java Heap Space is insufficient to upload files on AWS S3

前端 未结 2 1437
-上瘾入骨i
-上瘾入骨i 2020-12-11 22:59

I\'m trying to upload a file on AWS S3 by using Java-AWS API. The problem is my application is unable to upload large sized files because the heap is reaching its limit. Err

相关标签:
2条回答
  • 2020-12-11 23:49

    I strongly recommend to setContentLength() on ObjectMetadata, since:

    ..If not provided, the library will have to buffer the contents of the input stream in order to calculate it.

    (..which predictably will lead to OutOfMemory on "sufficient large" files.)

    source: PutObjectRequest javadoc

    Applied to your code:

     // ...
     ObjectMetadata omd = new ObjectMetadata();
     // a tiny code line, but with a "huge" information gain and memory saving!;)
     omd.setContentLength(file.length());
    
     s3Client.putObject(new PutObjectRequest(ABuck, AFkey, file.getInputStream(), omd).withCannedAcl(CannedAccessControlList.PublicRead));
     // ...
    
    0 讨论(0)
  • 2020-12-11 23:59

    You need to add example code to get a proper answer. If you are dealing with a large object, use TransferManager to upload rather than doing putObject.

    0 讨论(0)
提交回复
热议问题