Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 outpost examples for v2 to replace v1 examples in S3 User Guide #6552

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions javav2/example_code/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3control</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3outposts</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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:<region>:<accountId>:outpost/op-<123456789abcdefgh>/bucket/<bucketName>")
.accountId("<account-id>")
.name("<access-point-name>")
.vpcConfiguration(b1 -> b1.vpcId("<vpc-12345>")));
logger.info("Access point created with Arn: [{}]", response.accessPointArn());
} catch (SdkException e) {
logger.error(e.getMessage(), e);
}
}
}
// snippet-end:[s3-outposts.java2.create_accesspoint]
}

Original file line number Diff line number Diff line change
@@ -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("<bucket-name>")
.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]
}

Original file line number Diff line number Diff line change
@@ -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("<subnet-id>")
.securityGroupId("<security-group-id")
.accessType(EndpointAccessType.CUSTOMER_OWNED_IP)
.customerOwnedIpv4Pool("<ipv4pool-coip-12345678901234567>"));
logger.info("Endpoint point created with Arn: [{}]", response.endpointArn());
} catch (SdkException e) {
logger.error(e.getMessage(), e);
}
}
}
// snippet-end:[s3-outposts.java2.create_endpoint]
}
Original file line number Diff line number Diff line change
@@ -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:<region>:<accountId>:outpost/op-<123456789abcdefgh>/bucket/<bucketName>";
String stringObjKeyName = "<test-object-key>";
String fileObjKeyName = "<file-object-key>";
String fileName = "<path-to-local-file>";

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]
}
Loading