title: How do I directly stream a multipart file as inputstream to AWS S3 in Java?
body: <p>Right now my code takes the MultipartFile and converts into a java.util.File and uploads to S3.</p>
  
  <pre><code>try {
      final ObjectMetadata objectMetadata = new ObjectMetadata();
      objectMetadata.setContentType(file.getContentType());
      byte[] contentBytes = null;
      try {
          final InputStream is = file.getInputStream();
          contentBytes = IOUtils.toByteArray(is);
      } catch (final IOException e) {
          log.error("Failed while reading bytes from %s" + e.getMessage());
      }
  
      final Long contentLength = Long.valueOf(contentBytes.length);
      objectMetadata.setContentLength(contentLength);
      objectMetadata.setHeader("filename", fileNameWithExtn);
  
      /*
       * Reobtain the tmp uploaded file as input stream
       */
      final InputStream inputStream = file.getInputStream();
  
      final File convFile = new File(fileNameWithExtn);
      file.transferTo(convFile);
  
      FileUtils.copyInputStreamToFile(inputStream, convFile);
      try {
          final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
          versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, convFile))
                  .getVersionId();
      } catch (final AmazonServiceException ase) {
          System.out.println("Caught an AmazonServiceException, which"
                  + " means your request made it "
                  + "to Amazon S3, but was rejected with an error response" + " for some reason.");
          System.out.println("Error Message:    " + ase.getMessage());
          System.out.println("HTTP Status Code: " + ase.getStatusCode());
          System.out.println("AWS Error Code:   " + ase.getErrorCode());
          System.out.println("Error Type:       " + ase.getErrorType());
          System.out.println("Request ID:       " + ase.getRequestId());
      } catch (final AmazonClientException ace) {
          System.out.println("Caught an AmazonClientException, which means"
                  + " the client encountered " + "an internal error while trying to "
                  + "communicate with S3, " + "such as not being able to access the network.");
          System.out.println("Error Message: " + ace.getMessage());
      }
  } catch (final Exception e) {
      log.error("You failed to upload " + name + " =&gt; " + e.getMessage());
  }
  </code></pre>
  
  <p>The reason why I don't want to do this way is because, in my desktop the image I upload in browser stays temporarily in tomcat server and then it get uploaded to S3. </p>
  
  <p>But when my webapp runs in Ec2, the image uploaded from browser doesn't seem to stay in tomcat server because of permission issues(or some other reason). Hence the entire image upload fails. </p>
  
  <p>I tried the solution provided in this article <a href="https://stackoverflow.com/questions/21089106/converting-multipartfile-to-java-io-file-without-copying-to-local-machine">Converting MultipartFile to java.io.File without copying to local machine</a> and missing luck. </p>
  
  <p>Could someone direct me with code to directly stream a multipart file to S3?</p>