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

AWS SDK for Java has been certified for use with Wasabi. 

 

To use the Java SDK execute the following steps:

1) Install Apache maven

2) Follow the guide Using the SDK with Apache Maven and add the required dependencies in the pom.xml file:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.1000</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!--S3 Dependency-->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>
        <!--IAM Dependency-->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-iam</artifactId>
        </dependency>
    </dependencies>

3) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload an Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket

Other examples can be referred from the AWS documentation.   

❗️

Important: 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.

Please send all IAM requests to iam.wasabisys.com

 

1. How to set the credentials

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;

/**
* This class provides an example of connecting to Wasabi S3 and IAM services.
*/
public class GeneralConnection {

    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // IAM endpoint parameters.
        final String iamEndpoint = "https://iam.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        AWSStaticCredentialsProvider credentials =
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        // AWSStaticCredentialsProvider credentials = new ProfileCredentialsProvider("wasabi");
    }
}

2.Connect to IAM and S3 endpoints

IAM:

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder;

/**
* This class provides an example of connecting to Wasabi S3 and IAM services.
*/
public class IamConnection {

    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // IAM endpoint parameters.
        final String iamEndpoint = "https://iam.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        AWSStaticCredentialsProvider credentials =
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        // AWSStaticCredentialsProvider credentials = new ProfileCredentialsProvider("wasabi");

        // connect to Wasabi iam
        AmazonIdentityManagement iam = 

        AmazonIdentityManagementClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(iamEndpoint, region))
                .withCredentials(credentials)
                .build();
    }
}

S3:

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

public class S3Connection {
    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // S3 endpoint parameters.
        final String s3Endpoint = "https://s3.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        AWSStaticCredentialsProvider credentials =
                new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        // AWSStaticCredentialsProvider credentials = new ProfileCredentialsProvider("wasabi");

        // connect to Wasabi s3
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
                .withCredentials(credentials)
                .build();
    }
}

3. Create a user using IAM

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder;
import com.amazonaws.services.identitymanagement.model.CreateUserRequest;

/**
* This class provides an example of creating an IAM user on Wasabi.
*/
public class IamCreateUser {

    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // IAM endpoint parameters.
        final String iamEndpoint = "https://iam.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        // AWSCredentialsProvider credentials =
        //         new AWSStaticCredentialsProvider(
        //                 new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        AWSCredentialsProvider credentials = new 

        ProfileCredentialsProvider("wasabi");

        // connect to Wasabi iam
        AmazonIdentityManagement iam = 

        AmazonIdentityManagementClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(iamEndpoint, region))
                .withCredentials(credentials)
                .build();

        iam.createUser(new CreateUserRequest().withUserName("<user-name>"));
    }
}

4. Create a Bucket

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

public class S3CreateBucket {
    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // S3 endpoint parameters.
        final String s3Endpoint = "https://s3.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        // AWSCredentialsProvider credentials =
        //         new AWSStaticCredentialsProvider(
        //                 new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        AWSCredentialsProvider credentials = new 

        ProfileCredentialsProvider("wasabi");

        // connect to Wasabi s3
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
                .withCredentials(credentials)
                .build();

        s3.createBucket("java-bucket-rv");
    }
}

5. Upload an object to the Bucket

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import java.io.File;

public class S3CreateBucket {
    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // S3 endpoint parameters.
        final String s3Endpoint = "https://s3.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        // AWSCredentialsProvider credentials =
        //         new AWSStaticCredentialsProvider(
        //                 new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        AWSCredentialsProvider credentials = new            

        ProfileCredentialsProvider("wasabi");

        // connect to Wasabi s3
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
                .withCredentials(credentials)
                .build();

        // General bucket information
        final String bucketName = "java-bucket-rv";
        final String keyName = "text.txt";
        final String filePath = "Data/text.txt";

        // Upload object to bucket
        s3.putObject(bucketName,keyName,new File(filePath));
    }
}

6. Read an object from the Bucket

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

public class S3CreateBucket {
    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // S3 endpoint parameters.
        final String s3Endpoint = "https://s3.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        // AWSCredentialsProvider credentials =
        //         new AWSStaticCredentialsProvider(
        //                 new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        AWSCredentialsProvider credentials = new 

        ProfileCredentialsProvider("wasabi");

        // connect to Wasabi s3
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new           

        AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
                .withCredentials(credentials)
                .build();

        // General bucket information
        final String bucketName = "java-bucket-rv";
        final String keyName = "text.txt";

        // GET object from bucket
        s3.getObject(bucketName, keyName);
    }
}

7. Delete the object from the Bucket

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

public class S3CreateBucket {
    /**
    * Main Driver method.
    *
    * @param args basic input arguments. This example does not have any inputs.
    */
    public static void main(String[] args) {
        // S3 endpoint parameters.
        final String s3Endpoint = "https://s3.wasabisys.com";
        final String region = "us-east-1";

        // Use the following code if you would like to add your access and secret keys in the code.
        // AWSCredentialsProvider credentials =
        //         new AWSStaticCredentialsProvider(
        //                 new BasicAWSCredentials("<access-key>", "<secret-key>"));

        // Use the following code to fetch the aws profile credentials.
        AWSCredentialsProvider credentials = new 

        ProfileCredentialsProvider("wasabi");

        // connect to Wasabi s3
        AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new 

        AwsClientBuilder.EndpointConfiguration(s3Endpoint, region))
                .withCredentials(credentials)
                .build();

        // General bucket information
        final String bucketName = "java-bucket-rv";
        final String keyName = "text.txt";

        // DELETE object from bucket
        s3.deleteObject(bucketName, keyName);
    }
}

 

Have more questions? Submit a request