diff --git a/javav2/example_code/s3/pom.xml b/javav2/example_code/s3/pom.xml index 87db3497718..2284c95f640 100644 --- a/javav2/example_code/s3/pom.xml +++ b/javav2/example_code/s3/pom.xml @@ -152,6 +152,10 @@ software.amazon.awssdk s3control + + software.amazon.awssdk + s3outposts + software.amazon.awssdk netty-nio-client diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsAccessPoint.java b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsAccessPoint.java new file mode 100644 index 00000000000..90c751d5942 --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsAccessPoint.java @@ -0,0 +1,38 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.s3.outposts; + +// snippet-start:[s3-outposts.java2.create_accesspoint.import] +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.exception.SdkException; +import software.amazon.awssdk.services.s3control.S3ControlClient; +import software.amazon.awssdk.services.s3control.model.CreateAccessPointResponse; +// snippet-end:[s3-outposts.java2.create_accesspoint.import] + +public class CreateOutpostsAccessPoint { + private static final Logger logger = LoggerFactory.getLogger(CreateOutpostsAccessPoint.class); + + public static void main(String[] args) { + createAccessPoint(); + } + + // snippet-start:[s3-outposts.java2.create_accesspoint] + public static void createAccessPoint() { + try (S3ControlClient s3ControlClient = S3ControlClient.create()) { + try { + CreateAccessPointResponse response = s3ControlClient.createAccessPoint(b -> b + .bucket("arn:aws:s3-outposts:::outpost/op-<123456789abcdefgh>/bucket/") + .accountId("") + .name("") + .vpcConfiguration(b1 -> b1.vpcId(""))); + logger.info("Access point created with Arn: [{}]", response.accessPointArn()); + } catch (SdkException e) { + logger.error(e.getMessage(), e); + } + } + } + // snippet-end:[s3-outposts.java2.create_accesspoint] +} + diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java new file mode 100644 index 00000000000..40ce7230262 --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java @@ -0,0 +1,38 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.s3.outposts; + +// snippet-start:[s3-outposts.java2.create_bucket.import] +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.exception.SdkException; +import software.amazon.awssdk.services.s3control.S3ControlClient; +import software.amazon.awssdk.services.s3control.model.CreateBucketConfiguration; +import software.amazon.awssdk.services.s3control.model.CreateBucketResponse; +// snippet-end:[s3-outposts.java2.create_bucket.import] + +public class CreateOutpostsBucket { + private static final Logger logger = LoggerFactory.getLogger(CreateOutpostsBucket.class); + + public static void main(String[] args) { + createOutpostsBucket(); + } + + // snippet-start:[s3-outposts.java2.create_bucket] + public static void createOutpostsBucket() { + try (S3ControlClient s3ControlClient = S3ControlClient.create()) { + try { + CreateBucketResponse response = s3ControlClient.createBucket(b -> b + .bucket("") + .outpostId("op-<123456789abcdefgh>") + .createBucketConfiguration(CreateBucketConfiguration.builder().build())); + logger.info("Bucket created with Arn: [{}]", response.bucketArn()); + } catch (SdkException e) { + logger.error(e.getMessage(), e); + } + } + } + // snippet-end:[s3-outposts.java2.create_bucket] +} + diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsEndPoint.java b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsEndPoint.java new file mode 100644 index 00000000000..c7ad2cc0b50 --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsEndPoint.java @@ -0,0 +1,49 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.s3.outposts; + +// snippet-start:[s3-outposts.java2.create_endpoint.import] +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.exception.SdkException; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3control.S3ControlClient; +import software.amazon.awssdk.services.s3control.model.CreateAccessPointResponse; +import software.amazon.awssdk.services.s3control.model.CreateBucketConfiguration; +import software.amazon.awssdk.services.s3control.model.CreateBucketResponse; +import software.amazon.awssdk.services.s3outposts.S3OutpostsClient; +import software.amazon.awssdk.services.s3outposts.model.CreateEndpointResponse; +import software.amazon.awssdk.services.s3outposts.model.EndpointAccessType; + +import java.io.File; +import java.util.Map; +// snippet-end:[s3-outposts.java2.create_endpoint.import] + +public class CreateOutpostsEndPoint { + private static final Logger logger = LoggerFactory.getLogger(CreateOutpostsEndPoint.class); + + public static void main(String[] args) { + createOutpostsEndPoint(); + } + + // snippet-start:[s3-outposts.java2.create_endpoint] + public static void createOutpostsEndPoint() { + // Use an SDK S3OutpostsClient to create an S3 on Outposts endpoint. + try (S3OutpostsClient s3OutpostsClient = S3OutpostsClient.create()) { + try { + CreateEndpointResponse response = s3OutpostsClient.createEndpoint(b -> b + .outpostId("op-<123456789abcdefgh>") + .subnetId("") + .securityGroupId("")); + logger.info("Endpoint point created with Arn: [{}]", response.endpointArn()); + } catch (SdkException e) { + logger.error(e.getMessage(), e); + } + } + } + // snippet-end:[s3-outposts.java2.create_endpoint] +} diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/PutObjectOnOutpost.java b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/PutObjectOnOutpost.java new file mode 100644 index 00000000000..a65373c5604 --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/PutObjectOnOutpost.java @@ -0,0 +1,57 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.s3.outposts; + +// snippet-start:[s3-outposts.java2.put_object.imports] +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.core.exception.SdkException; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.PutObjectResponse; + +import java.io.File; +import java.util.Map; +// snippet-end:[s3-outposts.java2.put_object.imports] + +public class PutObjectOnOutpost { + private static final Logger logger = LoggerFactory.getLogger(PutObjectOnOutpost.class); + + public static void main(String[] args) { + putObjectOnOutpost(); + } + + // snippet-start:[s3-outposts.java2.put_object] + public static void putObjectOnOutpost() { + String accessPointArn = "arn:aws:s3-outposts:::outpost/op-<123456789abcdefgh>/bucket/"; + String stringObjKeyName = ""; + String fileObjKeyName = ""; + String fileName = ""; + + try (S3Client s3Client = S3Client.builder().useArnRegion(true).build()) { + try { + // Upload a text string as a new object. + PutObjectResponse response = s3Client.putObject(b -> b + .bucket(accessPointArn) + .key(stringObjKeyName), + RequestBody.fromString("Uploaded String Object")); + + logger.info("Uploaded a text string as new object, ETag is: [{}]", response.eTag()); + + // Upload a file as a new object with ContentType and title specified. + response = s3Client.putObject(b -> b + .bucket(accessPointArn) + .key(fileObjKeyName) + .contentType("plain/text") + .metadata(Map.of("title", "someTitle")), + new File(fileName).toPath()); + + logger.info("Uploaded a file as a new object with ContentType and title specified, ETag is: [{}]", response.eTag()); + } catch (SdkException e) { + logger.error(e.getMessage(), e); + } + } + } + // snippet-end:[s3-outposts.java2.put_object] +}