title: java - upload image file into my aws s3 bucket through html file field
body: <p>I tried to find a solution on google since 2 days but didn't have any solution.</p>
  
  <pre><code>DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
  TransferManager tx = new TransferManager(credentialProviderChain.getCredentials());
  Upload myUpload = tx.upload(existingBucketName, keyName, new File("C:/Users/Desktop/Management_System.jpg"));
      // While the transfer is processing, you can work with the transfer object
  while (myUpload.isDone() == false) {
      System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
  }
  myUpload.waitForCompletion();
  tx.shutdownNow();
  </code></pre>
  
  <p>This code is working fine and uploading an image <code>Management_System.jpg</code>, which is placed into my desktop to aws s3 bucket successfully. I have an html form with a file field. I want to browse any image in mycomputer and upload it into my s3 bucket. I also tried this below code and it works fine on my localhost when i test it on amazon ec2 then it throw error something like this "ImageIO write Invalid native argument". I've file field into my form name [photo]. So my code is as below:</p>
  
  <pre><code>BufferedImage src = ImageIO.read(photo.getInputStream());
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  ImageIO.write(src, "JPG", os);
  byte[] buffer = os.toByteArray();
  InputStream is = new ByteArrayInputStream(buffer);
  
  AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
  
  if(isValidFile(s3client, existingBucketName, info.getPhoto())){
      s3client.deleteObject(new DeleteObjectRequest(existingBucketName, info.getPhoto()));
  }
  
  ObjectMetadata meta = new ObjectMetadata();
  meta.setContentLength(buffer.length);
  meta.setContentType(photo.getContentType());
  s3client.putObject(new PutObjectRequest(existingBucketName, keyName, is, meta));
  s3client.setObjectAcl(existingBucketName, keyName, CannedAccessControlList.PublicRead);
  </code></pre>
  
  <p>Please guide me where i'm doing wrong? I'm using tomcat 7 on ec2 instance and my localhost version is also tomcat 7. I don't understand that is it version compatibility issue or something else... </p>
  
  <p><strong>Updated Answer:</strong></p>
  
  <p>Finally i make it work. Here is mine full file upload code through html form field. I'm posting it here to help others please vote me up if you find it helpfull. thanks</p>
  
  <pre><code>AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
              TransferManager tx = new TransferManager(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
              //AmazonS3 s3 = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
              if(isValidFile(s3, BucketName, info.getPhoto())){
                  s3.deleteObject(new DeleteObjectRequest(BucketName, info.getPhoto()));
              }
              S3Object s3Object = new S3Object();
              ObjectMetadata meta = new ObjectMetadata();
              meta.setContentType(photo.getContentType());
              meta.setContentLength(photo.getSize());
              meta.setHeader("filename", NewkeyName);
              ByteArrayInputStream bis = new ByteArrayInputStream(photo.getBytes());
              s3Object.setObjectContent(bis);
  
              //s3.putObject(new PutObjectRequest(BucketName, NewkeyName, bis, omd));
              Upload myUpload = tx.upload(BucketName, NewkeyName, bis, meta);
              while (myUpload.isDone() == false) {
                  System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
              }
              if(myUpload.isDone() == true){
                  System.out.println("100%");
              }
              s3.setObjectAcl(BucketName, NewkeyName, CannedAccessControlList.PublicRead);
              myUpload.waitForCompletion();
              tx.shutdownNow();
  
              s3Object.close();
  </code></pre>