Last active
May 31, 2024 02:00
-
-
Save hrchu/1dc625525f8382d5e59a930d898aa6d3 to your computer and use it in GitHub Desktop.
Example code of operating S3 on Outposts with AWS Java SDK v2. Checkout AWS offical document for using v1: https://docs.aws.amazon.com/AmazonS3/latest/userguide/S3OutpostsCreateAccessPoint.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>aws-sdk-example</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>AWS SDK Example</name> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>bom</artifactId> | |
<version>2.20.28</version> | |
<!-- <version>2.25.63</version>--> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>s3</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>s3control</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>apache-client</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>software.amazon.awssdk</groupId> | |
<artifactId>netty-nio-client</artifactId> | |
</dependency> | |
<!-- SLF4J Logging API --> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>1.7.30</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-simple</artifactId> | |
<version>1.7.30</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.8.1</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; | |
import software.amazon.awssdk.regions.Region; | |
import software.amazon.awssdk.services.s3control.S3ControlClient; | |
import software.amazon.awssdk.services.s3control.model.*; | |
import java.util.stream.Collectors; | |
public class S3OutpostsBucket { | |
public static void main(String[] args) { | |
// Enable debug-level logging for the AWS Java SDK v2 | |
// System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug"); | |
// System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.out"); | |
// System.setProperty("org.slf4j.simpleLogger.log.software.amazon.awssdk", "debug"); | |
String accountId = "xxx"; | |
String vpcId = "vpc-xxx"; | |
String outpostId = "op-xxx"; | |
String bucketName = "test-java-create"; | |
String accessPointName = "poc-java-sdk-ap2"; | |
// Create an S3ControlClient | |
S3ControlClient s3ControlClient = S3ControlClient.builder() | |
.credentialsProvider(ProfileCredentialsProvider.create()) | |
// .region(Region.AP_NORTHEAST_1) | |
.build(); | |
// Create the bucket request | |
String bucketArn; | |
try { | |
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder() | |
.bucket(bucketName) | |
.outpostId(outpostId) | |
.build(); | |
CreateBucketResponse createBucketResponse = s3ControlClient.createBucket(createBucketRequest); | |
bucketArn = createBucketResponse.bucketArn(); | |
} catch (BucketAlreadyOwnedByYouException e) { | |
System.out.println("Bucket already exist"); | |
ListRegionalBucketsResponse buckets = s3ControlClient.listRegionalBuckets(ListRegionalBucketsRequest.builder().accountId(accountId).outpostId(outpostId).build()); | |
bucketArn = buckets.regionalBucketList().stream().filter(x -> x.bucket().equals(bucketName)).map(RegionalBucket::bucketArn).collect(Collectors.toList()).get(0); | |
} | |
System.out.println("Bucket Arn: " + bucketArn); | |
// create AP | |
CreateAccessPointResponse accessPoint = s3ControlClient.createAccessPoint(CreateAccessPointRequest.builder() | |
.name(accessPointName) | |
.accountId(accountId) | |
.vpcConfiguration(VpcConfiguration.builder() | |
.vpcId(vpcId).build() | |
) | |
.bucket(bucketArn).build() | |
); | |
System.out.println("Access point Arn: " + accessPoint.accessPointArn()); | |
System.out.println("Wait a few minutes for the new access point to become usable."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; | |
import software.amazon.awssdk.core.exception.SdkClientException; | |
import software.amazon.awssdk.regions.Region; | |
import software.amazon.awssdk.services.s3.S3Client; | |
import software.amazon.awssdk.services.s3.model.*; | |
import java.nio.file.Paths; | |
public class S3OutpostsObject { | |
public static void main(String[] args) { | |
String accessPointArn = "TBD; | |
String filePath = "/tmp/100M"; | |
String keyName = "test-v2-sdk-obj"; | |
// Create an S3 client using the default credentials profile | |
S3Client s3 = S3Client.builder() | |
.credentialsProvider(ProfileCredentialsProvider.create()) | |
.build(); | |
//////// | |
System.out.println("Create object"); | |
try { | |
// Create a PutObjectRequest | |
PutObjectRequest putObjectRequest = PutObjectRequest.builder() | |
.bucket(accessPointArn) | |
.key(keyName) | |
.build(); | |
// Upload the file | |
PutObjectResponse putObjectResponse = s3.putObject(putObjectRequest, Paths.get(filePath)); | |
System.out.println("File uploaded successfully. ETag: " + putObjectResponse.eTag()); | |
} catch (S3Exception e) { | |
// The call was transmitted successfully, but Amazon S3 couldn't process it, so it returned an error response. | |
e.printStackTrace(); | |
} catch (SdkClientException e) { | |
// Amazon S3 couldn't be contacted for a response, or the client couldn't parse the response from Amazon S3. | |
e.printStackTrace(); | |
} | |
//////// | |
System.out.println("Listing objects"); | |
// maxKeys is set to 2 to demonstrate the use of | |
// ListObjectsV2Response.nextContinuationToken() | |
ListObjectsV2Request req = ListObjectsV2Request.builder() | |
.bucket(accessPointArn) | |
.maxKeys(2) | |
.build(); | |
ListObjectsV2Response result; | |
do { | |
result = s3.listObjectsV2(req); | |
for (S3Object objectSummary : result.contents()) { | |
System.out.printf(" - %s (size: %d)\n", objectSummary.key(), objectSummary.size()); | |
} | |
// If there are more than maxKeys keys in the bucket, get a continuation token | |
// and list the next objects. | |
String token = result.nextContinuationToken(); | |
System.out.println("Next Continuation Token: " + token); | |
req = req.toBuilder().continuationToken(token).build(); | |
} while (result.isTruncated()); | |
//////// | |
System.out.println("Delete object"); | |
s3.deleteObject(DeleteObjectRequest.builder().bucket(accessPointArn).key(keyName).build()); | |
// Close the S3 client | |
s3.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment