Import AWS SDK for Java
Ensure that Java v1.8 or a higher version is installed,as well as OpenSSL v1.x
Add the following code in the dependencies
block in the build.gradle
file
dependencies {
implementation platform('software.amazon.awssdk:bom:2.20.151')
implementation 'software.amazon.awssdk:s3'
implementation 'software.amazon.awssdk:s3-transfer-manager'
implementation 'software.amazon.awssdk:sts'
}
Here's the complete example of the build.gradle
file (Groovy DSL version) with the com.sufy.s3.examples.App
package:
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation platform('software.amazon.awssdk:bom:2.20.151')
implementation 'software.amazon.awssdk:s3'
implementation 'software.amazon.awssdk:s3-transfer-manager'
implementation 'software.amazon.awssdk:sts'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'com.google.guava:guava:32.1.1-jre'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
application {
mainClass = 'com.sufy.s3.examples.App'
}
tasks.named('test') {
useJUnitPlatform()
}
For each code example that follows,create the code under the app/src/main/java/com/sufy/s3/examples/App.java
,then execute
gradle run
UploadObject
Client-side upload
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.net.URI;
import java.time.Duration;
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.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest;
public class App {
public static void main(String[] args) {
final S3Presigner presigner = S3Presigner.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final PresignedPutObjectRequest presignedPutObjectRequest = presigner
.presignPutObject(b -> b.putObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
.signatureDuration(Duration.ofHours(1)));
System.out.println(presignedPutObjectRequest.url());
}
}
This code will generate a pre-signed client-side upload URL, valid for 1 hours, which the client can use to send an PUT request and upload a file within the expiration time.
The following is an example of uploading a file using curl:
curl -X PUT --upload-file "<path/to/file>" "<presigned url>"
Server-side upload
PutObject(file)
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.File;
import java.net.URI;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
public class App {
public static void main(String[] args) {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final PutObjectResponse putObjectResponse = s3.putObject(b -> b.bucket("<Bucket>").key("<Key>"),
RequestBody.fromFile(new File("<path/to/upload>")));
System.out.printf("ETag: %s\n", putObjectResponse.eTag());
}
}
PutObject(stream)
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URI;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
try (final RandomAccessFile file = new RandomAccessFile("<path/to/upload>", "r")) {
final PutObjectResponse putObjectResponse = s3.putObject(b -> b.bucket("<Bucket>").key("<Key>"),
RequestBody.fromInputStream(new FileInputStream(file.getFD()), file.length())); // The AWS Java SDK requires that data streams must provide a size, or implement the ContentStreamProvider interface to enable resetting the data stream.
System.out.printf("ETag: %s\n", putObjectResponse.eTag());
}
}
}
MultipartUpload(file)
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.CompletedPart;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.UploadPartResponse;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final int PART_SIZE = 5 * 1024 * 1024; // part size is 5 MB
try (final InputStream inputStream = new FileInputStream("<path/to/upload>")) {
final CreateMultipartUploadResponse createMultipartUploadResponse = s3
.createMultipartUpload(builder -> builder.bucket("<Bucket>").key("<Key>"));
// The example given here is a serial multipart upload. You can modify it to perform a parallel multipart upload to further improve the upload speed.
final byte[] bytes = new byte[PART_SIZE];
final List<CompletedPart> parts = new Vector<>();
for (int partNumber = 1;; partNumber++) {
final int haveRead = inputStream.read(bytes);
if (haveRead <= 0) {
break;
}
final int pn = partNumber;
final UploadPartResponse uploadPartResponse = s3
.uploadPart(builder -> builder.bucket(createMultipartUploadResponse.bucket())
.key(createMultipartUploadResponse.key())
.uploadId(createMultipartUploadResponse.uploadId()).partNumber(pn),
RequestBody.fromBytes(Arrays.copyOf(bytes, haveRead)));
parts.add(CompletedPart.builder().eTag(uploadPartResponse.eTag()).partNumber(pn).build());
}
final CompleteMultipartUploadResponse completeMultipartUploadResponse = s3
.completeMultipartUpload(builder -> builder.bucket(createMultipartUploadResponse.bucket())
.key(createMultipartUploadResponse.key()).uploadId(createMultipartUploadResponse.uploadId())
.multipartUpload(b -> b.parts(parts)));
System.out.printf("ETag: %s\n", completeMultipartUploadResponse.eTag());
}
}
}
UploadObject
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.CompletedUpload;
public class App {
public static void main(String[] args) throws IOException {
final S3AsyncClient s3 = S3AsyncClient.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final S3TransferManager transferManager = S3TransferManager.builder().s3Client(s3).build();
final CompletedUpload fileUpload = transferManager
.upload(b -> b.putObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
.requestBody(AsyncRequestBody.fromFile(new File("<path/to/upload>"))))
.completionFuture().join();
System.out.printf("ETag: %s\n", fileUpload.response().eTag());
}
}
GetObject
Client-side get object
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.net.URI;
import java.time.Duration;
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.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
public class App {
public static void main(String[] args) {
final S3Presigner presigner = S3Presigner.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final PresignedGetObjectRequest presignedGetObjectRequest = presigner
.presignGetObject(b -> b.getObjectRequest(builder -> builder.bucket("<Bucket>").key("<Key>"))
.signatureDuration(Duration.ofHours(1)));
System.out.println(presignedGetObjectRequest.url());
}
}
This code will generate a pre-signed client-side download URL, valid for 1 hour, which the client can use to send an GET request and download the file within the expiration time. The following is an example of downloading a file using curl:
curl -o "<path/to/download>" "<presigned url>"
Server-side get object
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.File;
import java.net.URI;
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.services.s3.model.GetObjectResponse;
public class App {
public static void main(String[] args) {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final GetObjectResponse getObjectResponse = s3.getObject(b -> b.bucket("<Bucket>").key("<Key>"), new File("<path/to/upload>").toPath());
System.out.printf("ETag: %s\n", getObjectResponse.eTag());
}
}
ObjectOperations
HeadObject
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.net.URI;
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.services.s3.model.HeadObjectResponse;
public class App {
public static void main(String[] args) {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final HeadObjectResponse headObjectResponse = s3.headObject(b -> b.bucket("<Bucket>").key("<Key>"));
System.out.printf("ETag: %s\n", headObjectResponse.eTag());
}
}
ChangeStorageClass
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.net.URI;
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.services.s3.model.MetadataDirective;
import software.amazon.awssdk.services.s3.model.StorageClass;
public class App {
public static void main(String[] args) {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
s3.copyObject(b -> b.sourceBucket("<Bucket>").sourceKey("<Key>").destinationBucket("<Bucket>").destinationKey("<Key>")
.metadataDirective(MetadataDirective.REPLACE).storageClass(StorageClass.GLACIER));
System.out.println("Done");
}
}
CopyObject
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.net.URI;
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.services.s3.model.MetadataDirective;
public class App {
public static void main(String[] args) {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
s3.copyObject(b -> b.sourceBucket("<FromBucket>").sourceKey("<FromKey>").destinationBucket("<ToBucket>").destinationKey("<ToKey>")
.metadataDirective(MetadataDirective.COPY));
System.out.println("Done");
}
}
CopyObject(> 5GB)
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Vector;
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.services.s3.model.CompleteMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.CompletedPart;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket("<FromBucket>").key("<FromKey>")
.build();
final HeadObjectResponse headObjectResponse = s3.headObject(headObjectRequest);
final CreateMultipartUploadResponse createMultipartUploadResponse = s3
.createMultipartUpload(b -> b.bucket("<ToBucket>").key("<ToKey>"));
final List<CompletedPart> parts = new Vector<>();
final long PART_SIZE = 5 * 1024 * 1024; // part size is 5 MB
long copied = 0;
// The example given here is a serial multipart copy. You can modify it to perform a parallel multipart copy to further improve the copy speed.
for (int partNumber = 1; copied < headObjectResponse.contentLength(); partNumber++) {
final long partSize = Math.min(PART_SIZE, headObjectResponse.contentLength() - copied);
final int pn = partNumber;
final String copySourceRange = String.format("bytes=%d-%d", copied, copied + partSize - 1);
final UploadPartCopyResponse uploadPartCopyResponse = s3
.uploadPartCopy(b -> b.sourceBucket(headObjectRequest.bucket()).sourceKey(headObjectRequest.key())
.destinationBucket(createMultipartUploadResponse.bucket())
.destinationKey(createMultipartUploadResponse.key())
.uploadId(createMultipartUploadResponse.uploadId()).partNumber(pn)
.copySourceRange(copySourceRange));
parts.add(CompletedPart.builder().eTag(uploadPartCopyResponse.copyPartResult().eTag()).partNumber(pn)
.build());
copied += partSize;
}
final CompleteMultipartUploadResponse completeMultipartUploadResponse = s3.completeMultipartUpload(
b -> b.bucket(createMultipartUploadResponse.bucket()).key(createMultipartUploadResponse.key())
.uploadId(createMultipartUploadResponse.uploadId())
.multipartUpload(builder -> builder.parts(parts)));
System.out.printf("ETag: %s\n", completeMultipartUploadResponse.eTag());
}
}
DeleteObject
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.IOException;
import java.net.URI;
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;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
s3.deleteObject(b -> b.bucket("<Bucket>").key("<Key>"));
System.out.println("Done");
}
}
ListObjects
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.IOException;
import java.net.URI;
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.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final ListObjectsV2Response listObjectsV2Response = s3.listObjectsV2(b -> b.bucket("<Bucket>").prefix("<KeyPrefix>"));
for (final S3Object s3Object : listObjectsV2Response.contents()) {
System.out.printf("Key: %s\n", s3Object.key());
System.out.printf("ETag: %s\n", s3Object.eTag());
}
}
}
DeleteObjects
Create app/src/main/java/com/sufy/s3/examples/App.java
package com.sufy.s3.examples;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Vector;
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.services.s3.model.ObjectIdentifier;
public class App {
public static void main(String[] args) throws IOException {
final S3Client s3 = S3Client.builder().region(Region.of("ap-southeast-2")) // Asia Pacific (Hanoi) RegionID
.endpointOverride(URI.create("https://mos.ap-southeast-2.sufybkt.com")) // Asia Pacific (Hanoi) Endpoint
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create("<AccessKey>", "<SecretKey>")))
.build();
final List<ObjectIdentifier> objectIdentifiers = new Vector<>();
objectIdentifiers.add(ObjectIdentifier.builder().key("<Key1>").build());
objectIdentifiers.add(ObjectIdentifier.builder().key("<Key2>").build());
objectIdentifiers.add(ObjectIdentifier.builder().key("<Key3>").build());
s3.deleteObjects(b -> b.bucket("<Bucket>").delete(builder -> builder.objects(objectIdentifiers)));
System.out.println("Done");
}
}