title: JAVA AWS S3 multipart upload with inputstream and length of inputstream is unknown
body: <p><br/>
  If content length is unknown then AmazonS3Client and TransferManager buffers the content in the memory,it results in out of memory exception.
  <br/>So i wanted to go with multipart upload(Low Level of API).
  But i didn't find ways to upload with inputstream(Examples are there with File).<br/>
  I tried by passing inputstream but no result.<br/><br/>
  My Code :: (Inside Vaadin upload receiver)</p>
  
  <pre><code>@Override
  public OutputStream receiveUpload(final String filename, String mimeType) {
      this.fileName=filename;
      PipedOutputStream pos=null;
      try {
          pos= new PipedOutputStream();
          final InputStream is = new PipedInputStream(pos);
          new Thread(){
              @Override
              public void run(){
                  try{
                      List&lt;PartETag&gt; partETags = new ArrayList&lt;PartETag&gt;();
                      InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(
                                                                          bucketName, filename);
                      InitiateMultipartUploadResult initResponse = 
                                                    s3.initiateMultipartUpload(initRequest);
                      long partSize = 5 * 1024 * 1024; // Set part size to 5 MB.
                      int size = 1024;
                      byte[] buffer = new byte[size];
                      int len=0;
                      int partNum = 1;
                       ByteArrayOutputStream bos = new ByteArrayOutputStream();
                      for(int i=1;(len=is.read(buffer,0,size))!=-1;i++){
                          bos.write(buffer,0,len); 
                          if(bos.size()&gt;=partSize){
                              UploadPartRequest uploadRequest = new UploadPartRequest()
                              .withBucketName(bucketName).withKey(filename)
                              .withUploadId(initResponse.getUploadId()).withPartNumber(partNum++)
                              .withInputStream(is)
                              .withPartSize(partSize);
                               partETags.add(
                                          s3.uploadPart(uploadRequest).getPartETag());
                              bos.flush();
                              bos.close();
                              ins.close();
                              bos = new ByteArrayOutputStream();
                          }
                      }
                      //rest of data, written to s3
                      System.out.println("After remaining bos "+bos.size());
                      UploadPartRequest uploadRequest = new UploadPartRequest()
                      .withBucketName(bucketName).withKey(filename)
                      .withUploadId(initResponse.getUploadId()).withPartNumber(partNum++)
                      .withInputStream(is)
                      .withLastPart(true);
                      partETags.add(
                              s3.uploadPart(uploadRequest).getPartETag());
  
                      CompleteMultipartUploadRequest compRequest = new 
                                   CompleteMultipartUploadRequest(
                                              bucketName, 
                                              filename, 
                                              initResponse.getUploadId(), 
                                              partETags);
  
                      s3.completeMultipartUpload(compRequest);
              }
          }.start();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return pos;
  }
  </code></pre>
  
  <blockquote>
    <p>.withInputStream(is)<br>
    here I'm passing original stream or do i need to create a temp stream
    with read content.</p>
  </blockquote>
  
  <p>Thanks in advance.</p>