How do I use the AWS SDK for Java (v2) with Wasabi?

The AWS SDK for Java (v2) is certified for use with Wasabi.  To use this SDK with Wasabi, please follow the example below.

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article.

Imports for all of these operations:

import java.net.URI;

import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.ListBucketsRequest;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Object;

 

Configuration required to set up Wasabi with AWS Java SDK 2.x

Region region = Region.US_EAST_1;

final String END_POINT = "https://s3.wasabisys.com";
AwsSessionCredentials awsCreds = AwsSessionCredentials.create(ACCESS_KEY, SECRET_ACCESS_KEY, "");

S3Client s3 = S3Client.builder().credentialsProvider(

             StaticCredentialsProvider.create(awsCreds))
.endpointOverride(URI.create(END_POINT)).region(region).build();

Use the above created S3Client 's3' for all the below bucket operations examples.  

 

Bucket Operations Examples:

  • Create a Bucket
  • List the Buckets
  • Delete a Bucket

  

Create a Bucket


/**
* Creates a bucket in the given region with the name specified.
* @param s3
* @param bucketName
* @param region
*/
public static void createBucket(S3Client s3, String bucketName, Region region) {
 CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
  .bucket(bucketName).createBucketConfiguration(
  CreateBucketConfiguration.builder().locationConstraint(region.id()).build())
  .build();
 System.out.println(s3.createBucket(createBucketRequest).toString());
}


List the Buckets

/**
* Lists the buckets in your account.
* @param s3
*/
public static void listBuckets(S3Client s3) {
ListBucketsRequest req = ListBucketsRequest.builder().build();
ListBucketsResponse res = s3.listBuckets(req);
res.buckets().stream().forEach(x->System.out.println(x.name()));
}

 

Delete a Bucket

/**
* Before you can delete an Amazon S3 bucket, you must ensure that the bucket
* is empty or the service will return an error. If you have a versioned
* bucket, you must also delete any versioned objects that are in the bucket.
*
* @param s3

* @param bucketName
* @param region
*/


public static void deleteBucketWithContents(final S3Client s3, final String bucketName, final Region region) {

 ListObjectsV2Request request = ListObjectsV2Request.builder()

                                  .bucket(bucketName).build();
 ListObjectsV2Response response;
 do {
response = s3.listObjectsV2(request);
for(S3Object s3Obj : response.contents()) {
s3.deleteObject(DeleteObjectRequest.builder()
.bucket(bucketName).key(s3Obj.key()).build());
}
request = ListObjectsV2Request.builder().bucket(bucketName) .continuationToken(response.continuationToken()).build();
}while(response.isTruncated());



//Now the bucket is empty, delete the bucket
deleteEmptyBucket(s3, bucketName);
}

/**
* Deletes the bucket specified, given the bucket is empty.
* @param s3
* @param bucketName
*/
public static void deleteEmptyBucket(final S3Client s3, final String bucketName) {

DeleteBucketRequest req = DeleteBucketRequest.builder().bucket(bucketName).build();
System.out.println(s3.deleteBucket(req).toString());
}

  

Have more questions? Submit a request