AWS SDK for Java (v2) With Wasabi
    • 12 Aug 2024
    • 10 Minutes to read
    • PDF

    AWS SDK for Java (v2) With Wasabi

    • PDF

    Article summary

    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: 

    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

    Setting 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");
        }
    }

    Connecting 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();
        }
    }

    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("", ""));         // 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();     } }

    Creating 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>"));
        }
    }

    Creating 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");
        }
    }

    Uploading 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));
        }
    }

    Reading 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);
        }
    }

    Deleting 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);
        }
    }