In this article, we will review the basics for using VPSA Object Storage with AWS S3 Java SDK. aws-sdk-java is the official AWS SDK for Java.
Prerequisites
1. Zadara VPSA Object Storage deployed in a location of your choice.
2. AWS aws-java-sdk (you can either download and install it locally, or use Apache Maven to get all required dependencies automatically).
Connectivity information
For Object Storage connectivity, it is required to gather the following information from the VPSA Object Storage management UI:
- VPSA Object Storage Endpoint
- VPSA Object Storage region.
- S3 API Access Key/Secret Key
From the VPSA Object Storage GUI, navigate to the User Information section (top right corner, by clicking the logged in username).
Example
Import the following classes (you may need to add some additional classes later according to your application's objectives)
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
S3 Connection
As the VPSA Object Storage is an S3 compatible storage, it is required to setup the connectivity details for it.
// The VPSA Connectivity information
String endpoint = "https://vsa-0000009a-zadara-qa14.zadarazios.com";
String region = "us-east-1";
// The VPSA Authentication information
String access_key = 5f3d67a374ca45e98268123304b5bda0
String secret_key = 7ad6a0f21d1b425eaa2790e8a4aa19e2
AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
//Define Signature V4 Object Storage
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
// Build the Client
s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint , region))
.withPathStyleAccessEnabled(true)
.withClientConfiguration(clientConfiguration)
.build();
Operating the Object Storage
Upon successful connectivity, various S3 Operation can be done against the VPSA Object Storage, for example:
Create a new bucket
String bucket_name = "mybucket";
if (s3.doesBucketExistV2(bucket_name)) {
System.out.format("\nCannot create the bucket. \n" +
"A bucket named '%s' already exists.", bucket_name);
return;
} else {
try {
System.out.format("\nCreating a new bucket named '%s'...\n\n", bucket_name);
s3.createBucket(new CreateBucketRequest(bucket_name, region));
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
}
Delete a bucket:
try {
System.out.format("\nDeleting the bucket named '%s'...\n\n", bucket_name);
s3.deleteBucket(bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
List buckets:
private static void ListMyBuckets() {
List<Bucket> buckets = s3.listBuckets();
System.out.println("My buckets now are:");
for (Bucket b : buckets) {
System.out.println(b.getName());
}
}
These and additional AWS S3 Java SDK examples can be found in aws-java-sdk github examples repository.
Got a question? We love to help, send us a mail to support@zadarastorage.com