title: How to upload multipart to Amazon S3 asynchronously using the java SDK
body: <p>In my java application I need to write data to S3, which I don't know the size in advance and sizes are usually big so as recommend in the AWS S3 documentation I am using the <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpu-upload-object.html#mpu-upload-low-level" rel="nofollow noreferrer">Using the Java AWS SDKs (low-level-level API)</a> to write data to the s3 bucket.</p>
  <p>In my application I provide <code>S3BufferedOutputStream</code> which is an implementation <code>OutputStream</code> where other classes in the app can use this stream to write to the s3 bucket.</p>
  <p>I store the data in a buffer and loop and once the data is bigger than bucket size I upload data in the buffer as a a single <code>UploadPartRequest</code>
  Here is the implementation of the write method of S3BufferedOutputStream</p>
  <pre><code>@Override
  public void write(byte[] b, int off, int len) throws IOException {
      this.assertOpen();
      int o = off, l = len;
      int size;
      while (l &gt; (size = this.buf.length - position)) {
          System.arraycopy(b, o, this.buf, this.position, size);
          this.position += size;
          flushBufferAndRewind();
          o += size;
          l -= size;
      }
      System.arraycopy(b, o, this.buf, this.position, l);
      this.position += l;
  }
  </code></pre>
  <p>The whole implementation is similar to this: <a href="https://gist.github.com/blagerweij/ad1dbb7ee2fff8bcffd372815ad310eb#file-s3outputstream-java" rel="nofollow noreferrer">code repo</a></p>
  <p>My problem here is that each UploadPartRequest is done synchronously, so we have to wait for one part to be uploaded to be able to upload the next part. And because I am using the AWS S3 low level API I can not benefit from the parallel uploading provided by the <a href="https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/transfer/TransferManager.java" rel="nofollow noreferrer">TransferManager</a></p>
  <p>Is there a way to achieve the parallel upload using low level SDK?
  Or some code changes that can be done to operate Asynchronously without corrupting the uploaded data and maintain order of the data?</p>