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

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

您可以使用 适用于 Java 的开发工具包 创建和管理 S3 on Outposts 存储桶。从这些示例中,您可以创建和获取 Outposts 存储桶,列出 Outpost 的存储桶,创建和管理 Outpost 存储桶的访问点、生命周期配置和策略。

为 Amazon 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(); }

创建 Amazon S3 on Outposts 存储桶

以下示例使用 适用于 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(); }

获取 Amazon 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()); }

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

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

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(); }

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

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

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()); }

列出 AWS Outpost 的访问点

以下 适用于 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()); }

为 Outposts 存储桶添加生命周期配置

以下 适用于 Java 的开发工具包 示例为 Outposts 存储桶提供生命周期配置,其中带有标记前缀和标签的所有对象在 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(1); 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()); }

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

以下 适用于 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()); }

在 Outposts 存储桶上放置策略

以下 适用于 Java 的开发工具包 示例为 Outposts 存储桶放置策略。

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()); }

获取 Amazon S3 on Outposts 存储桶的策略

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

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()); }

将策略放在 Outposts 访问点上

以下 适用于 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()); }

获取 Amazon S3 on Outposts 访问点的策略

以下 适用于 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 上创建 endpoint

以下 适用于 Java 的开发工具包 示例为 Outpost 创建 endpoint。

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"); CreateEndpointResult createEndpointResult = s3OutpostsClient.createEndpoint(createEndpointRequest); System.out.println("Endpoint is created and its arn is " + createEndpointResult.getEndpointArn()); }

在 Outpost 上删除 endpoint

以下 适用于 Java 的开发工具包 示例删除 Outpost 的 endpoint。

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-0d79779cef3c30a40"; 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."); }

列出 Amazon S3 on Outposts Outpost 的 endpoint

以下 适用于 Java 的开发工具包 示例列出 Outpost 的 endpoint。

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); }