Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用AWS SDK for Java实现跨源资源共享 (CORS)

您可以使用AWS SDK for Java管理存储桶的跨源资源共享 (CORS)。有关 CORS 的更多信息,请参阅 跨源资源共享 (CORS)

本节将为下面的任务提供示例代码段,后跟演示所有任务的完整示例程序。

  • 创建一个 Amazon S3 客户端类实例

  • 创建 CORS 配置并将其添加到存储桶

  • 更新现有 CORS 配置

跨源资源共享方法

AmazonS3Client()

构建 AmazonS3Client 对象。

setBucketCrossOriginConfiguration()

设置要应用于存储桶的 CORS 配置。如果指定的存储桶已存在一个配置,新的配置将替换现有配置。

getBucketCrossOriginConfiguration()

检索指定存储桶的 CORS 配置。如果没有为存储桶设置任何配置,则响应中的 Configuration 标头将为空。

deleteBucketCrossOriginConfiguration()

删除指定存储桶的 CORS 配置。

有关AWS SDK for Java API 的更多信息,请参阅AWS SDK for Java API Reference

创建一个 Amazon S3 客户端类实例

下面的代码段将为名为 CORS_JavaSDK 的类创建新的 AmazonS3Client 实例。本示例将从 AwsCredentials.properties 文件检索 accessKeysecretKey 的值。

Copy
AmazonS3Client client; client = new AmazonS3Client(new ProfileCredentialsProvider());

创建 CORS 配置并将其添加到存储桶

将 CORS 配置添加到存储桶的步骤:

  1. 创建用于说明规则的 CORSRule 对象。

  2. 创建 BucketCrossOriginConfiguration 对象,然后将规则添加到配置对象。

  3. 通过调用 client.setBucketCrossOriginConfiguration 方法,将 CORS 配置添加到存储桶。

下面的代码段将创建两个规则,CORSRule1CORSRule2,然后将每个规则添加到 rules 数组。接着,通过使用 rules 数组,将规则添加到存储桶 bucketName

Copy
// Add a sample configuration BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration(); List<CORSRule> rules = new ArrayList<CORSRule>(); CORSRule rule1 = new CORSRule() .withId("CORSRule1") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.POST, CORSRule.AllowedMethods.DELETE})) .withAllowedOrigins(Arrays.asList(new String[] {"http://*.example.com"})); CORSRule rule2 = new CORSRule() .withId("CORSRule2") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET})) .withAllowedOrigins(Arrays.asList(new String[] {"*"})) .withMaxAgeSeconds(3000) .withExposedHeaders(Arrays.asList(new String[] {"x-amz-server-side-encryption"})); configuration.setRules(Arrays.asList(new CORSRule[] {rule1, rule2})); // Save the configuration client.setBucketCrossOriginConfiguration(bucketName, configuration);

更新现有 CORS 配置

更新现有 CORS 配置的步骤

  1. 通过调用 client.getBucketCrossOriginConfiguration 方法获取 CORS 配置。

  2. 通过在规则列表中添加或删除规则,更新配置信息。

  3. 通过调用 client.getBucketCrossOriginConfiguration 方法,向存储桶添加配置。

下面的代码段将获取现有的配置,然后添加一个 ID 为 NewRule 的新规则。

Copy
// Get configuration. BucketCrossOriginConfiguration configuration = client.getBucketCrossOriginConfiguration(bucketName); // Add new rule. CORSRule rule3 = new CORSRule() .withId("CORSRule3") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.HEAD})) .withAllowedOrigins(Arrays.asList(new String[] {"http://www.example.com"})); List<CORSRule> rules = configuration.getRules(); rules.add(rule3); configuration.setRules(rules); // Save configuration. client.setBucketCrossOriginConfiguration(bucketName, configuration);

例 程序列表

以下 Java 程序集成了上述任务。

有关创建和测试有效示例的信息,请参阅 测试 Java 代码示例

Copy
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration; import com.amazonaws.services.s3.model.CORSRule; public class Cors { /** * @param args * @throws IOException */ public static AmazonS3Client client; public static String bucketName = "***provide bucket name***"; public static void main(String[] args) throws IOException { client = new AmazonS3Client(new ProfileCredentialsProvider()); // Create a new configuration request and add two rules BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration(); List<CORSRule> rules = new ArrayList<CORSRule>(); CORSRule rule1 = new CORSRule() .withId("CORSRule1") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.POST, CORSRule.AllowedMethods.DELETE})) .withAllowedOrigins(Arrays.asList(new String[] {"http://*.example.com"})); CORSRule rule2 = new CORSRule() .withId("CORSRule2") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET})) .withAllowedOrigins(Arrays.asList(new String[] {"*"})) .withMaxAgeSeconds(3000) .withExposedHeaders(Arrays.asList(new String[] {"x-amz-server-side-encryption"})); configuration.setRules(Arrays.asList(new CORSRule[] {rule1, rule2})); // Add the configuration to the bucket. client.setBucketCrossOriginConfiguration(bucketName, configuration); // Retrieve an existing configuration. configuration = client.getBucketCrossOriginConfiguration(bucketName); printCORSConfiguration(configuration); // Add a new rule. CORSRule rule3 = new CORSRule() .withId("CORSRule3") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.HEAD})) .withAllowedOrigins(Arrays.asList(new String[] {"http://www.example.com"})); rules = configuration.getRules(); rules.add(rule3); configuration.setRules(rules); client.setBucketCrossOriginConfiguration(bucketName, configuration); System.out.format("Added another rule: %s\n", rule3.getId()); // Verify that the new rule was added. configuration = client.getBucketCrossOriginConfiguration(bucketName); System.out.format("Expected # of rules = 3, found %s", configuration.getRules().size()); // Delete the configuration. client.deleteBucketCrossOriginConfiguration(bucketName); // Try to retrieve configuration. configuration = client.getBucketCrossOriginConfiguration(bucketName); System.out.println("\nRemoved CORS configuration."); printCORSConfiguration(configuration); } static void printCORSConfiguration(BucketCrossOriginConfiguration configuration) { if (configuration == null) { System.out.println("\nConfiguration is null."); return; } System.out.format("\nConfiguration has %s rules:\n", configuration.getRules().size()); for (CORSRule rule : configuration.getRules()) { System.out.format("Rule ID: %s\n", rule.getId()); System.out.format("MaxAgeSeconds: %s\n", rule.getMaxAgeSeconds()); System.out.format("AllowedMethod: %s\n", rule.getAllowedMethods().toArray()); System.out.format("AllowedOrigins: %s\n", rule.getAllowedOrigins()); System.out.format("AllowedHeaders: %s\n", rule.getAllowedHeaders()); System.out.format("ExposeHeader: %s\n", rule.getExposedHeaders()); } } }