Java Quickstart

Learn how to use Java with Rabata.io for managing your object storage using the AWS SDK for Java.

Installation

To use Rabata.io with Java, you’ll need to install the AWS SDK for Java.

Using Maven

Add the following dependency to your pom.xml file:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.20.56</version>
</dependency>

Using Gradle

Add the following to your build.gradle file:

dependencies {
    implementation 'software.amazon.awssdk:s3:2.20.56'
}

Configuration

There are several ways to configure the AWS SDK for Java to work with Rabata.io.

Method 1: Using AWS Credentials File

If you’ve already configured the AWS CLI as shown in the AWS CLI Quickstart, the SDK will automatically use those credentials.

Method 2: Explicit Configuration in Code

You can explicitly configure the S3 client in your code:

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import java.net.URI;

public class RabataS3Example {
    public static void main(String[] args) {
        // Create credentials with your Rabata.io access keys
        AwsBasicCredentials credentials = AwsBasicCredentials.create(
            "YOUR_ACCESS_KEY",
            "YOUR_SECRET_KEY"
        );

        // Create an S3 client with Rabata.io endpoint
        S3Client s3 = S3Client.builder()
            .region(Region.US_EAST_1)
            .endpointOverride(URI.create("https://s3.eu-west-1.rabata.io"))
            .credentialsProvider(StaticCredentialsProvider.create(credentials))
            .httpClientBuilder(UrlConnectionHttpClient.builder())
            .build();
            
        // Now you can use the s3 client to interact with Rabata.io
    }
}

Basic Operations

List Buckets

import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.model.Bucket;

// List all buckets
ListBucketsResponse listBucketsResponse = s3.listBuckets();
System.out.println("Your buckets are:");
for (Bucket bucket : listBucketsResponse.buckets()) {
    System.out.println(bucket.name());
}

Create a Bucket

import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.CreateBucketResponse;

// Create a bucket
String bucketName = "my-new-bucket";
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
    .bucket(bucketName)
    .build();
    
CreateBucketResponse createBucketResponse = s3.createBucket(createBucketRequest);
System.out.println("Bucket created: " + bucketName);

Upload an Object

import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import java.nio.file.Paths;
import java.nio.file.Files;

// Upload a file to a bucket
String bucketName = "my-bucket";
String key = "my-object-key";
String filePath = "/path/to/your/file.txt";

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
    .bucket(bucketName)
    .key(key)
    .build();

PutObjectResponse putObjectResponse = s3.putObject(
    putObjectRequest,
    RequestBody.fromFile(Paths.get(filePath))
);

System.out.println("File uploaded successfully");

Download an Object

import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.FileOutputStream;
import java.io.IOException;

// Download a file from a bucket
String bucketName = "my-bucket";
String key = "my-object-key";
String downloadPath = "/path/to/download/file.txt";

GetObjectRequest getObjectRequest = GetObjectRequest.builder()
    .bucket(bucketName)
    .key(key)
    .build();

ResponseInputStream<GetObjectResponse> response = s3.getObject(getObjectRequest);

// Save the downloaded content to a file
try (FileOutputStream fos = new FileOutputStream(downloadPath)) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = response.read(buffer)) != -1) {
        fos.write(buffer, 0, bytesRead);
    }
    System.out.println("File downloaded successfully");
} catch (IOException e) {
    System.err.println("Error downloading file: " + e.getMessage());
}

List Objects in a Bucket

import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;

// List objects in a bucket
String bucketName = "my-bucket";

ListObjectsV2Request listObjectsRequest = ListObjectsV2Request.builder()
    .bucket(bucketName)
    .build();

ListObjectsV2Response listObjectsResponse = s3.listObjectsV2(listObjectsRequest);

System.out.println("Objects in bucket " + bucketName + ":");
for (S3Object s3Object : listObjectsResponse.contents()) {
    System.out.println("Key: " + s3Object.key() + ", Size: " + s3Object.size() + " bytes");
}

Delete an Object

import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;

// Delete an object from a bucket
String bucketName = "my-bucket";
String key = "my-object-key";

DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
    .bucket(bucketName)
    .key(key)
    .build();

DeleteObjectResponse deleteObjectResponse = s3.deleteObject(deleteObjectRequest);
System.out.println("Object deleted successfully");

Delete a Bucket

import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteBucketResponse;

// Delete a bucket (must be empty)
String bucketName = "my-bucket";

DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder()
    .bucket(bucketName)
    .build();

DeleteBucketResponse deleteBucketResponse = s3.deleteBucket(deleteBucketRequest);
System.out.println("Bucket deleted successfully");

Advanced Usage

Setting Object Metadata

import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import java.util.HashMap;
import java.util.Map;

// Upload a file with custom metadata
String bucketName = "my-bucket";
String key = "my-object-key";
String filePath = "/path/to/your/file.txt";

// Define custom metadata
Map<String, String> metadata = new HashMap<>();
metadata.put("custom-key1", "custom-value1");
metadata.put("custom-key2", "custom-value2");

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
    .bucket(bucketName)
    .key(key)
    .metadata(metadata)
    .build();

s3.putObject(putObjectRequest, RequestBody.fromFile(Paths.get(filePath)));
System.out.println("File uploaded with custom metadata");

Configuring Object Access Control

import software.amazon.awssdk.services.s3.model.ObjectCannedACL;

// Upload a file with public read access
String bucketName = "my-bucket";
String key = "my-public-object";
String filePath = "/path/to/your/file.txt";

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
    .bucket(bucketName)
    .key(key)
    .acl(ObjectCannedACL.PUBLIC_READ)
    .build();

s3.putObject(putObjectRequest, RequestBody.fromFile(Paths.get(filePath)));
System.out.println("File uploaded with public read access");

Error Handling

import software.amazon.awssdk.services.s3.model.S3Exception;

try {
    // S3 operation that might fail
    s3.getObject(GetObjectRequest.builder()
        .bucket("non-existent-bucket")
        .key("non-existent-key")
        .build());
} catch (S3Exception e) {
    System.err.println("Error: " + e.getMessage());
    System.err.println("HTTP Status Code: " + e.statusCode());
    System.err.println("AWS Error Code: " + e.awsErrorDetails().errorCode());
}

Conclusion

This quickstart guide covered the basics of using the AWS SDK for Java with Rabata.io. For more detailed information, refer to the AWS SDK for Java Documentation.