AWS: Java S3 Upload

This entry is part 3 of 5 in the series AWS & Java

If you want to push data to AWS S3 there are a few different ways of doing this. I will show you two ways I have used.

Option 1: putObject

  1. import com.amazonaws.AmazonClientException;
  2. import com.amazonaws.services.s3.model.ObjectMetadata;
  3. import com.amazonaws.ClientConfiguration;
  4. import com.amazonaws.auth.AWSCredentialsProvider;
  5. import com.amazonaws.regions.Regions;
  6. import com.amazonaws.services.s3.AmazonS3;
  7. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  8.  
  9. ClientConfiguration config = new ClientConfiguration();
  10. config.setSocketTimeout(SOCKET_TIMEOUT);
  11. config.setMaxErrorRetry(RETRY_COUNT);
  12. config.setClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT);
  13. config.setRequestTimeout(REQUEST_TIMEOUT);
  14. config.setConnectionTimeout(CONNECTION_TIMEOUT);
  15.  
  16. AWSCredentialsProvider credProvider = ...;
  17. String region = ...;
  18.  
  19. AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(credProvider).withRegion(region).withClientConfiguration(config).build();
  20.  
  21. InputStream stream = ...;
  22. String bucketName = .....;
  23. String keyName = ...;
  24. String mimeType = ...;
  25.  
  26. //You use metadata to describe the data.
  27. final ObjectMetadata metaData = new ObjectMetadata();
  28. metaData.setContentType(mimeType);
  29.  
  30. //There are overrides available. Find the one that suites what you need.
  31. try {
  32. s3Client.putObject(bucketName, keyName, stream, metaData);
  33. } catch (final AmazonClientException ex) {
  34. //Log the exception
  35. }

Option 2: MultiPart Upload

  1. import com.amazonaws.AmazonClientException;
  2. import com.amazonaws.event.ProgressEvent;
  3. import com.amazonaws.regions.Regions;
  4. import com.amazonaws.services.s3.AmazonS3;
  5. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  6. import com.amazonaws.ClientConfiguration;
  7. import com.amazonaws.auth.AWSCredentialsProvider;
  8. import com.amazonaws.event.ProgressEventType;
  9. import com.amazonaws.event.ProgressListener;
  10. import com.amazonaws.services.s3.AmazonS3;
  11. import com.amazonaws.services.s3.model.ObjectMetadata;
  12. import com.amazonaws.services.s3.transfer.TransferManager;
  13. import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
  14. import com.amazonaws.services.s3.transfer.Upload;
  15.  
  16. ClientConfiguration config = new ClientConfiguration();
  17. config.setSocketTimeout(SOCKET_TIMEOUT);
  18. config.setMaxErrorRetry(RETRY_COUNT);
  19. config.setClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT);
  20. config.setRequestTimeout(REQUEST_TIMEOUT);
  21. config.setConnectionTimeout(CONNECTION_TIMEOUT);
  22.  
  23. AWSCredentialsProvider credProvider = ...;
  24. String region = ...;
  25.  
  26. AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(credProvider).withRegion(region).withClientConfiguration(config).build();
  27.  
  28. InputStream stream = ...;
  29. String bucketName = .....;
  30. String keyName = ...;
  31. long contentLength = ...;
  32. String mimeType = ...;
  33.  
  34. //You use metadata to describe the data. You need the content length so the multi part upload knows how big it is
  35. final ObjectMetadata metaData = new ObjectMetadata();
  36. metaData.setContentLength(contentLength);
  37. metaData.setContentType(mimeType);
  38.  
  39. TransferManager tf = TransferManagerBuilder.standard().withS3Client(s3Client).build();
  40. tf.getConfiguration().setMinimumUploadPartSize(UPLOAD_PART_SIZE);
  41. tf.getConfiguration().setMultipartUploadThreshold(UPLOAD_THRESHOLD);
  42. Upload xfer = tf.upload(bucketName, keyName, stream, metaData);
  43.  
  44. ProgressListener progressListener = new ProgressListener() {
  45. public void progressChanged(ProgressEvent progressEvent) {
  46. if (xfer == null)
  47. return;
  48. if (progressEvent.getEventType() == ProgressEventType.TRANSFER_FAILED_EVENT || progressEvent.getEventType() == ProgressEventType.TRANSFER_PART_FAILED_EVENT) {
  49. //Log the message
  50. }
  51. }
  52. };
  53.  
  54. xfer.addProgressListener(progressListener);
  55. xfer.waitForCompletion();