Java SDK

This guide details the steps needed to install and use the AWS SDK For Java.

The AWS SDK for Java simplifies use of AWS Services by providing a set of libraries that are consistent and familiar for Java developers. It provides support for API lifecycle considerations such as credential management, retries, data marshaling, and serialization. The AWS SDK for Java also supports higher-level abstractions for simplified development.

Using Java SDK

To use Java SDK, you must first import all necessary packages. After that, you need to set up an S3 client to connect to your bucket. Provide your access key, secret key, and path to your files.

 1import com.amazonaws.auth.AWSStaticCredentialsProvider;
 2import com.amazonaws.auth.BasicAWSCredentials;
 3import com.amazonaws.client.builder.AwsClientBuilder;
 4import com.amazonaws.services.s3.AmazonS3;
 5import com.amazonaws.services.s3.AmazonS3ClientBuilder;
 6import com.amazonaws.services.s3.model.S3Object;
 7import com.amazonaws.services.s3.model.PutObjectRequest;
 8import com.amazonaws.services.s3.model.CannedAccessControlList;
 9import com.amazonaws.services.s3.model.S3ObjectSummary;
10import com.amazonaws.services.s3.model.S3ObjectInputStream;
11
12import java.io.File;
13import java.util.List;
14import java.io.IOException;
15import java.io.FileOutputStream;
16
17//Let's create S3 client
18String accessKey = "Your access key";
19String secretKey = "Your secret key";
20String endpointUrl = "http://s3.tebi.io";
21String region = "US";
22String bucketName = "Your bucket name";
23String bucketFilePath = "This is the path in your bucket";
24String filePath = "This is the your local path";
25
26BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
27
28AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
29        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpointUrl, region))
30        .withCredentials(new AWSStaticCredentialsProvider(credentials))
31        .enablePathStyleAccess()
32        .build();

Now, after we have an S3 client, we can start sending files to our bucket:

1PutObjectRequest request = new PutObjectRequest(bucketName, bucketFilePath, new File(filePath));
2request.setCannedAcl(CannedAccessControlList.PublicRead);
3
4s3Client.putObject(request);
5
6System.out.println("File uploaded successfully!");

Or you can download a file from your bucket:

 1S3Object s3Object = s3Client.getObject(bucketName, bucketFilePath);
 2
 3S3ObjectInputStream objectInputStream = s3Object.getObjectContent();
 4
 5// Save the object content to a local file
 6try (FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath))) {
 7    byte[] buffer = new byte[1024];
 8    int bytesRead;
 9    while ((bytesRead = objectInputStream.read(buffer)) != -1) {
10        fileOutputStream.write(buffer, 0, bytesRead);
11    }
12    System.out.println("File retrieved and saved successfully!");
13} catch (IOException e) {
14    e.printStackTrace();
15}

Also, you can get a list of objects inside your bucket:

1List<S3ObjectSummary> objectSummaries = s3Client.listObjects(bucketName).getObjectSummaries();
2
3// Print the names of the objects
4System.out.println("Objects in bucket " + bucketName + ":");
5for (S3ObjectSummary objectSummary : objectSummaries) {
6    System.out.println("- " + objectSummary.getKey());
7}

For more examples, please refer to the official SDK documentation examples. Full AWS Java SDK reference is available here.