AWS SDK for Java 101

Build Java application with AWS services

Jargons

  • V1 vs V2: Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O

Example with S3 Client

  • prerequisite: you have configured your aws credentials.
  • it throws S3Exception
  • don’t forget the close the client
  • recommend enable the AbortIncompleteMultipartUpload lifecycle rule on your Amazon S3 buckets.
  1. build your client

     Region region = Region.US_WEST_2;
     S3Client s3Client = S3Client.builder().region(region).build();
    
  2. build up your bucket
    • create bucket request
    • wait the bucket to be created
     CreateBucketConfiguration bucketConfiguration = CreateBucketConfiguration.builder()
             .locationConstraint(region.id())
             .build();
    
     CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
             .bucket(bucketName)
             .createBucketConfiguration(bucketConfiguration)
             .build();
    
     s3Client.createBucket(createBucketRequest);
    
     System.out.println("Creating bucket " + bucketName);
    
     HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
             .bucket(bucketName)
             .build();
     s3Client.waiter()
             .waitUntilBucketExists(headBucketRequest);
    
     System.out.println(bucketName + " is ready.");
    
  3. put object

     PutObjectRequest putObjectRequest = PutObjectRequest.builder()
             .bucket(bucket)
             .key(key)
             .build();
    
     RequestBody requestBody = RequestBody.fromString("Testing with the {sdk-java}");
    
     s3Client.putObject(putObjectRequest, requestBody);
    
  4. remove object and bucket

     System.out.println("deleting object: " + keyName);
     DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
             .bucket(bucketName)
             .key(keyName)
             .build();
     s3Client.deleteObject(deleteObjectRequest);
    
     System.out.println(keyName + " has been deleted");
     System.out.println("deleting bucket: " + bucketName);
    
     DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder()
             .bucket(bucketName)
             .build();
     s3Client.deleteBucket(deleteBucketRequest);
     System.out.println(bucketName + " bucket has been deleted");
    
  5. use abortMultipartUploadRequest abort multipart uploads that don’t complete within a specified number of days

     ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder()
             .bucket(bucketName)
             .build();
    
     ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
     List<MultipartUpload> uploads = response.uploads();
    
     AbortMultipartUploadRequest abortMultipartUploadRequest = null;
    
     for (MultipartUpload upload: uploads) {
         abortMultipartUploadRequest = AbortMultipartUploadRequest.builder()
                 .bucket(bucketName)
                 .key(upload.key())
                 .expectedBucketOwner(accountId)
                 .uploadId(upload.uploadId())
                 .build();
    
         s3.abortMultipartUpload(abortMultipartUploadRequest);
     }
    
  6. Use S3Presigner

         @Inject
         S3Presigner s3Presigner;
    
         // get getObjectRequest
    
         GetObjectPresignRequest getObjectPresignRequest =
         GetObjectPresignRequest.builder()
         .signatureDuration(Duration.ofHours(48))
         .getObjectRequest(getObjectRequest)
         .build();
    
         PresignedGetObjectRequest presignedGetObjectRequest =
         s3Presigner.presignGetObject(getObjectPresignRequest);
    
         return Response
         .status(Response.Status.FOUND)
         .location(presignedGetObjectRequest.url().toURI())
         .build();
    
  7. close the s3 client

     System.out.println("Closing the connection to {S3}");
     s3Client.close();
     System.out.println("Connection closed");
    

References

Tags: ,

Posted: