创建和管理 Amazon S3 on Outposts 存储桶 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

创建和管理 Amazon S3 on Outposts 存储桶

您可以使用适用于 Java 的开发工具包创建和管理 S3 on Outposts 存储桶。以下示例说明了如何配合使用 Outposts 存储桶、接入点、生命周期配置、存储桶策略、接入点策略和终端节点。

为 S3 on Outposts 配置 S3 控制客户端

以下示例使用适用于 Java 的开发工具包为 S3 on Outposts 配置 S3 控制客户端。

import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3control.AWSS3Control; import com.amazonaws.services.s3control.AWSS3ControlClient; public AWSS3Control createS3ControlClient() { String accessKey = AWSAccessKey; String secretKey = SecretAccessKey; BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); return AWSS3ControlClient.builder().enableUseArnRegion() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .build(); }

创建 S3 on Outposts 存储桶

以下示例使用 SDK for Java 创建 S3 on Outposts 存储桶(s3-outposts:CreateBucket)。

import com.amazonaws.services.s3control.model.*; public String createBucket(String bucketName) { CreateBucketRequest reqCreateBucket = new CreateBucketRequest() .withBucket(bucketName) .withOutpostId(OutpostId) .withCreateBucketConfiguration(new CreateBucketConfiguration()); CreateBucketResult respCreateBucket = s3ControlClient.createBucket(reqCreateBucket); System.out.printf("CreateBucket Response: %s%n", respCreateBucket.toString()); return respCreateBucket.getBucketArn(); }

获取 S3 on Outposts 存储桶

以下 S3 on Outposts 示例使用适用于 Java 的开发工具包获取存储桶。

import com.amazonaws.services.s3control.model.*; public void getBucket(String bucketArn) { GetBucketRequest reqGetBucket = new GetBucketRequest() .withBucket(bucketArn) .withAccountId(AccountId); GetBucketResult respGetBucket = s3ControlClient.getBucket(reqGetBucket); System.out.printf("GetBucket Response: %s%n", respGetBucket.toString()); }

获取 Outpost 中的存储桶列表

以下适用于 Java 的开发工具包示例获取 Outpost 中的存储桶列表。

import com.amazonaws.services.s3control.model.*; public void listRegionalBuckets() { ListRegionalBucketsRequest reqListBuckets = new ListRegionalBucketsRequest() .withAccountId(AccountId) .withOutpostId(OutpostId); ListRegionalBucketsResult respListBuckets = s3ControlClient.listRegionalBuckets(reqListBuckets); System.out.printf("ListRegionalBuckets Response: %s%n", respListBuckets.toString()); }

为 S3 on Outposts 存储桶创建访问点

以下适用于 Java 的开发工具包示例为 Outpost 存储桶创建访问点。

import com.amazonaws.services.s3control.model.*; public String createAccessPoint(String bucketArn, String accessPointName) { CreateAccessPointRequest reqCreateAP = new CreateAccessPointRequest() .withAccountId(AccountId) .withBucket(bucketArn) .withName(accessPointName) .withVpcConfiguration(new VpcConfiguration().withVpcId("vpc-12345")); CreateAccessPointResult respCreateAP = s3ControlClient.createAccessPoint(reqCreateAP); System.out.printf("CreateAccessPoint Response: %s%n", respCreateAP.toString()); return respCreateAP.getAccessPointArn(); }

获取 S3 on Outposts 存储桶的访问点

以下适用于 Java 的开发工具包示例为 Outpost 存储桶获取访问点。

import com.amazonaws.services.s3control.model.*; public void getAccessPoint(String accessPointArn) { GetAccessPointRequest reqGetAP = new GetAccessPointRequest() .withAccountId(AccountId) .withName(accessPointArn); GetAccessPointResult respGetAP = s3ControlClient.getAccessPoint(reqGetAP); System.out.printf("GetAccessPoint Response: %s%n", respGetAP.toString()); }

列出 S3 on Outposts 存储桶的接入点

以下 SDK for Java 示例列出 Outposts 存储桶的接入点。

import com.amazonaws.services.s3control.model.*; public void listAccessPoints(String bucketArn) { ListAccessPointsRequest reqListAPs = new ListAccessPointsRequest() .withAccountId(AccountId) .withBucket(bucketArn); ListAccessPointsResult respListAPs = s3ControlClient.listAccessPoints(reqListAPs); System.out.printf("ListAccessPoints Response: %s%n", respListAPs.toString()); }

在 S3 on Outposts 存储桶上放置生命周期配置

以下 SDK for Java 示例在 Outposts 存储桶上放置生命周期配置。此生命周期配置指定具有标记前缀(myprefix)的所有对象,并且标签在 10 天后过期。

import com.amazonaws.services.s3control.model.*; public void putBucketLifecycleConfiguration(String bucketArn) { S3Tag tag1 = new S3Tag().withKey("mytagkey1").withValue("mytagvalue1"); S3Tag tag2 = new S3Tag().withKey("mytagkey2").withValue("mytagvalue2"); LifecycleRuleFilter lifecycleRuleFilter = new LifecycleRuleFilter() .withAnd(new LifecycleRuleAndOperator() .withPrefix("myprefix") .withTags(tag1, tag2)); LifecycleExpiration lifecycleExpiration = new LifecycleExpiration() .withExpiredObjectDeleteMarker(false) .withDays(10); LifecycleRule lifecycleRule = new LifecycleRule() .withStatus("Enabled") .withFilter(lifecycleRuleFilter) .withExpiration(lifecycleExpiration) .withID("id-1"); LifecycleConfiguration lifecycleConfiguration = new LifecycleConfiguration() .withRules(lifecycleRule); PutBucketLifecycleConfigurationRequest reqPutBucketLifecycle = new PutBucketLifecycleConfigurationRequest() .withAccountId(AccountId) .withBucket(bucketArn) .withLifecycleConfiguration(lifecycleConfiguration); PutBucketLifecycleConfigurationResult respPutBucketLifecycle = s3ControlClient.putBucketLifecycleConfiguration(reqPutBucketLifecycle); System.out.printf("PutBucketLifecycleConfiguration Response: %s%n", respPutBucketLifecycle.toString()); }

获取 S3 on Outposts 存储桶的生命周期配置

以下 SDK for Java 示例获取 Outposts 存储桶的生命周期配置。

import com.amazonaws.services.s3control.model.*; public void getBucketLifecycleConfiguration(String bucketArn) { GetBucketLifecycleConfigurationRequest reqGetBucketLifecycle = new GetBucketLifecycleConfigurationRequest() .withAccountId(AccountId) .withBucket(bucketArn); GetBucketLifecycleConfigurationResult respGetBucketLifecycle = s3ControlClient.getBucketLifecycleConfiguration(reqGetBucketLifecycle); System.out.printf("GetBucketLifecycleConfiguration Response: %s%n", respGetBucketLifecycle.toString()); }

在您的 S3 on Outposts 存储桶上放置策略

以下 SDK for Java 示例在 Outpost 存储桶上放置策略。

import com.amazonaws.services.s3control.model.*; public void putBucketPolicy(String bucketArn) { String policy = "{\"Version\":\"2012-10-17\",\"Id\":\"testBucketPolicy\",\"Statement\":[{\"Sid\":\"st1\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"" + AccountId+ "\"},\"Action\":\"s3-outposts:*\",\"Resource\":\"" + bucketArn + "\"}]}"; PutBucketPolicyRequest reqPutBucketPolicy = new PutBucketPolicyRequest() .withAccountId(AccountId) .withBucket(bucketArn) .withPolicy(policy); PutBucketPolicyResult respPutBucketPolicy = s3ControlClient.putBucketPolicy(reqPutBucketPolicy); System.out.printf("PutBucketPolicy Response: %s%n", respPutBucketPolicy.toString()); }

为 S3 on Outposts 存储桶获取策略

以下适用于 Java 的开发工具包示例为 Outpost 存储桶获取策略。

import com.amazonaws.services.s3control.model.*; public void getBucketPolicy(String bucketArn) { GetBucketPolicyRequest reqGetBucketPolicy = new GetBucketPolicyRequest() .withAccountId(AccountId) .withBucket(bucketArn); GetBucketPolicyResult respGetBucketPolicy = s3ControlClient.getBucketPolicy(reqGetBucketPolicy); System.out.printf("GetBucketPolicy Response: %s%n", respGetBucketPolicy.toString()); }

在 S3 on Outposts 接入点上放置策略

以下 SDK for Java 示例在 Outposts 接入点上放置策略。

import com.amazonaws.services.s3control.model.*; public void putAccessPointPolicy(String accessPointArn) { String policy = "{\"Version\":\"2012-10-17\",\"Id\":\"testAccessPointPolicy\",\"Statement\":[{\"Sid\":\"st1\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"" + AccountId + "\"},\"Action\":\"s3-outposts:*\",\"Resource\":\"" + accessPointArn + "\"}]}"; PutAccessPointPolicyRequest reqPutAccessPointPolicy = new PutAccessPointPolicyRequest() .withAccountId(AccountId) .withName(accessPointArn) .withPolicy(policy); PutAccessPointPolicyResult respPutAccessPointPolicy = s3ControlClient.putAccessPointPolicy(reqPutAccessPointPolicy); System.out.printf("PutAccessPointPolicy Response: %s%n", respPutAccessPointPolicy.toString()); printWriter.printf("PutAccessPointPolicy Response: %s%n", respPutAccessPointPolicy.toString()); }

为 S3 on Outposts 访问点获取策略

以下 SDK for Java 示例获取 Outposts 接入点策略。

import com.amazonaws.services.s3control.model.*; public void getAccessPointPolicy(String accessPointArn) { GetAccessPointPolicyRequest reqGetAccessPointPolicy = new GetAccessPointPolicyRequest() .withAccountId(AccountId) .withName(accessPointArn); GetAccessPointPolicyResult respGetAccessPointPolicy = s3ControlClient.getAccessPointPolicy(reqGetAccessPointPolicy); System.out.printf("GetAccessPointPolicy Response: %s%n", respGetAccessPointPolicy.toString()); printWriter.printf("GetAccessPointPolicy Response: %s%n", respGetAccessPointPolicy.toString()); }

为 Outpost 创建终端节点

以下适用于 Java 的开发工具包示例为 Outpost 创建终端节点。

import com.amazonaws.services.s3outposts.AmazonS3Outposts; import com.amazonaws.services.s3outposts.AmazonS3OutpostsClientBuilder; import com.amazonaws.services.s3outposts.model.CreateEndpointRequest; import com.amazonaws.services.s3outposts.model.CreateEndpointResult; public void createEndpoint() { AmazonS3Outposts s3OutpostsClient = AmazonS3OutpostsClientBuilder .standard().build(); CreateEndpointRequest createEndpointRequest = new CreateEndpointRequest() .withOutpostId("op-0d79779cef3c30a40") .withSubnetId("subnet-8c7a57c5") .withSecurityGroupId("sg-ab19e0d1") .withAccessType("CustomerOwnedIp") .withCustomerOwnedIpv4Pool("ipv4pool-coip-12345678901234567"); // Use .withAccessType and .withCustomerOwnedIpv4Pool only when the access type is // customer-owned IP address pool (CoIP pool) CreateEndpointResult createEndpointResult = s3OutpostsClient.createEndpoint(createEndpointRequest); System.out.println("Endpoint is created and its ARN is " + createEndpointResult.getEndpointArn()); }

删除 Outpost 的终端节点

以下适用于 Java 的开发工具包示例会删除 Outpost 的终端节点。

import com.amazonaws.arn.Arn; import com.amazonaws.services.s3outposts.AmazonS3Outposts; import com.amazonaws.services.s3outposts.AmazonS3OutpostsClientBuilder; import com.amazonaws.services.s3outposts.model.DeleteEndpointRequest; public void deleteEndpoint(String endpointArnInput) { String outpostId = "op-01ac5d28a6a232904"; AmazonS3Outposts s3OutpostsClient = AmazonS3OutpostsClientBuilder .standard().build(); Arn endpointArn = Arn.fromString(endpointArnInput); String[] resourceParts = endpointArn.getResource().getResource().split("/"); String endpointId = resourceParts[resourceParts.length - 1]; DeleteEndpointRequest deleteEndpointRequest = new DeleteEndpointRequest() .withEndpointId(endpointId) .withOutpostId(outpostId); s3OutpostsClient.deleteEndpoint(deleteEndpointRequest); System.out.println("Endpoint with id " + endpointId + " is deleted."); }

为 S3 on Outposts 列出终端节点

以下 SDK for Java 示例列出 Outpost 的终端节点。

import com.amazonaws.services.s3outposts.AmazonS3Outposts; import com.amazonaws.services.s3outposts.AmazonS3OutpostsClientBuilder; import com.amazonaws.services.s3outposts.model.ListEndpointsRequest; import com.amazonaws.services.s3outposts.model.ListEndpointsResult; public void listEndpoints() { AmazonS3Outposts s3OutpostsClient = AmazonS3OutpostsClientBuilder .standard().build(); ListEndpointsRequest listEndpointsRequest = new ListEndpointsRequest(); ListEndpointsResult listEndpointsResult = s3OutpostsClient.listEndpoints(listEndpointsRequest); System.out.println("List endpoints result is " + listEndpointsResult); }