AWS SDK - Java
Learn how to use the AWS SDK for Java with Filebase.

What is AWS SDK - Java?

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - Java.
This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.

Prerequisites:

  • Download and install Java in your environment.
  • Download and install the AWS SDK for Java.
  • This SDK uses your Filebase credentials stored in the default .aws file. To learn how to configure this file, see our guide on configuring and using AWS CLI.

Create a Bucket

The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:
  • new-filebase-bucket: The Intended New Bucket Name
Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes. Buckets created through this method will be automatically created on the Storj network.
1
import com.amazonaws.AmazonServiceException;
2
import com.amazonaws.SdkClientException;
3
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
4
import com.amazonaws.regions.Regions;
5
import com.amazonaws.services.s3.AmazonS3;
6
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
7
import com.amazonaws.services.s3.model.CreateBucketRequest;
8
import com.amazonaws.services.s3.model.GetBucketLocationRequest;
9
10
import java.io.IOException;
11
12
public class CreateBucket2 {
13
14
public static void main(String[] args) throws IOException {
15
String bucketName = "filebase-bucket-name";
16
17
try {
18
AmazonS3ClientBuilder.standard();
19
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
20
.withCredentials(new ProfileCredentialsProvider())
21
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.filebase.com", "us-east-1"))
22
.build();
23
24
if (!s3Client.doesBucketExistV2(bucketName)) {
25
s3Client.createBucket(new CreateBucketRequest(bucketName));
26
27
String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
28
System.out.println("Bucket location: " + bucketLocation);
29
}
30
} catch (AmazonServiceException e) {
31
e.printStackTrace();
32
} catch (SdkClientException e) {
33
e.printStackTrace();
34
}
35
}
36
}
Copied!

Upload an Object

The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:
  • filebase-bucket-name: The Filebase Bucket Name
  • /path/to/object/to/upload: The file path to the object that you want to be uploaded
  • object-name: The desired object name once uploaded
1
import com.amazonaws.AmazonServiceException;
2
import com.amazonaws.SdkClientException;
3
import com.amazonaws.regions.Regions;
4
import com.amazonaws.services.s3.AmazonS3;
5
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
6
import com.amazonaws.services.s3.model.ObjectMetadata;
7
import com.amazonaws.services.s3.model.PutObjectRequest;
8
9
import java.io.File;
10
import java.io.IOException;
11
12
public class UploadObject {
13
14
public static void main(String[] args) throws IOException {
15
String bucketName = "filebase-bucket-name";
16
String stringObjKeyName = "object-name";
17
String fileObjKeyName = "object-name";
18
String fileName = "/path/to/object/to/upload";
19
20
try {
21
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
22
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.filebase.com", "us-east-1"))
23
.build();
24
25
s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");
26
27
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
28
ObjectMetadata metadata = new ObjectMetadata();
29
metadata.setContentType("plain/text");
30
metadata.addUserMetadata("title", "someTitle");
31
request.setMetadata(metadata);
32
s3Client.putObject(request);
33
} catch (AmazonServiceException e) {
34
35
e.printStackTrace();
36
} catch (SdkClientException e) {
37
e.printStackTrace();
38
}
39
}
40
}
Copied!

Download an Object

The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:
  • filebase-bucket-name: The Filebase Bucket Name
  • object-name: The object to be downloaded
1
import com.amazonaws.AmazonServiceException;
2
import com.amazonaws.SdkClientException;
3
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
4
import com.amazonaws.regions.Regions;
5
import com.amazonaws.services.s3.AmazonS3;
6
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
7
import com.amazonaws.services.s3.model.GetObjectRequest;
8
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
9
import com.amazonaws.services.s3.model.S3Object;
10
11
import java.io.BufferedReader;
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.io.InputStreamReader;
15
16
public class GetObject2 {
17
18
public static void main(String[] args) throws IOException {
19
String bucketName = "filebase-bucket-name";
20
String key = "object-name";
21
22
S3Object fullObject = null, objectPortion = null, headerOverrideObject = null;
23
try {
24
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
25
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.filebase.com", "us-east-1"))
26
.withCredentials(new ProfileCredentialsProvider())
27
.build();
28
29
System.out.println("Downloading an object");
30
fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
31
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
32
System.out.println("Content: ");
33
displayTextInputStream(fullObject.getObjectContent());
34
35
GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key)
36
.withRange(0, 9);
37
objectPortion = s3Client.getObject(rangeObjectRequest);
38
System.out.println("Printing bytes retrieved.");
39
displayTextInputStream(objectPortion.getObjectContent());
40
41
ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
42
.withCacheControl("No-cache")
43
.withContentDisposition("attachment; filename=example.txt");
44
GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName, key)
45
.withResponseHeaders(headerOverrides);
46
headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
47
displayTextInputStream(headerOverrideObject.getObjectContent());
48
} catch (AmazonServiceException e) {
49
e.printStackTrace();
50
} catch (SdkClientException e) {
51
e.printStackTrace();
52
} finally {
53
if (fullObject != null) {
54
fullObject.close();
55
}
56
if (objectPortion != null) {
57
objectPortion.close();
58
}
59
if (headerOverrideObject != null) {
60
headerOverrideObject.close();
61
}
62
}
63
}
64
65
private static void displayTextInputStream(InputStream input) throws IOException {
66
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
67
String line = null;
68
while ((line = reader.readLine()) != null) {
69
System.out.println(line);
70
}
71
System.out.println();
72
}
73
}
Copied!

Delete an Object

The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:
  • filebase-bucket-name: The Filebase Bucket Name
  • object-name: The object to be downloaded
1
import com.amazonaws.AmazonServiceException;
2
import com.amazonaws.SdkClientException;
3
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
4
import com.amazonaws.regions.Regions;
5
import com.amazonaws.services.s3.AmazonS3;
6
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
7
import com.amazonaws.services.s3.model.DeleteObjectRequest;
8
9
import java.io.IOException;
10
11
public class DeleteObjectNonVersionedBucket {
12
13
public static void main(String[] args) throws IOException {
14
String bucketName = "filebase-bucket-name";
15
String keyName = "object-name";
16
17
try {
18
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
19
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.filebase.com", "us-east-1"))
20
.withCredentials(new ProfileCredentialsProvider())
21
.build();
22
23
s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
24
} catch (AmazonServiceException e) {
25
e.printStackTrace();
26
} catch (SdkClientException e) {
27
e.printStackTrace();
28
}
29
}
30
}
Copied!
If you have any questions, please join our Discord server, or send us an email at [email protected]