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.

使用适用于 .NET 的 AWS 开发工具包实现跨源资源共享 (CORS)

您可以使用适用于 .NET 的 AWS 开发工具包管理存储桶的跨源资源共享 (CORS)。有关 CORS 的更多信息,请参阅 跨源资源共享 (CORS)

本节将为下表中的任务提供示例代码,后跟完整的示例程序列表。

管理跨源资源共享

1

创建 AmazonS3Client 类的实例。

2

创建新的 CORS 配置。

3

检索和修改现有 CORS 配置。

4

向存储桶添加配置。

跨源资源共享方法

AmazonS3Client()

使用 App.config 文件中定义的凭证构建 AmazonS3Client

PutCORSConfiguration()

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

GetCORSConfiguration()

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

DeleteCORSConfiguration()

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

有关适用于 .NET 的 AWS 开发工具包 API 的更多信息,请参阅使用 适用于 .NET 的 AWS 开发工具包

创建 AmazonS3 类的实例

以下示例创建 AmazonS3Client 类的实例。

Copy
static IAmazonS3 client; using (client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))

向存储桶添加 CORS 配置

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

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

  2. 创建可提供存储桶名称和 CORS 配置的 PutCORSConfigurationRequest 对象。

  3. 通过调用 client.PutCORSConfiguration,向存储桶添加 CORS 配置。

以下示例创建两个规则 CORSRule1CORSRule2,然后将每个规则添加到 rules 数组。接着,通过使用 rules 数组,将规则添加到存储桶 bucketName

Copy
// Add a sample configuration CORSConfiguration configuration = new CORSConfiguration { Rules = new System.Collections.Generic.List<CORSRule> { new CORSRule { Id = "CORSRule1", AllowedMethods = new List<string> {"PUT", "POST", "DELETE"}, AllowedOrigins = new List<string> {"http://*.example.com"} }, new CORSRule { Id = "CORSRule2", AllowedMethods = new List<string> {"GET"}, AllowedOrigins = new List<string> {"*"}, MaxAgeSeconds = 3000, ExposeHeaders = new List<string> {"x-amz-server-side-encryption"} } } }; // Save the configuration PutCORSConfiguration(configuration); static void PutCORSConfiguration(CORSConfiguration configuration) { PutCORSConfigurationRequest request = new PutCORSConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = client.PutCORSConfiguration(request); }

更新现有 CORS 配置

更新现有 CORS 配置的步骤

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

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

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

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

Copy
// Get configuration. configuration = GetCORSConfiguration(); // Add new rule. configuration.Rules.Add(new CORSRule { Id = "NewRule", AllowedMethods = new List<string> { "HEAD" }, AllowedOrigins = new List<string> { "http://www.example.com" } }); // Save configuration. PutCORSConfiguration(configuration);

例 程序列表

下面的 C# 程序集成了上述任务。

有关创建和测试有效示例的信息,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.Configuration; using System.Collections.Specialized; using System.Net; using Amazon.S3; using Amazon.S3.Model; using Amazon.S3.Util; using System.Diagnostics; using System.Collections.Generic; namespace s3.amazon.com.docsamples { class CORS { static string bucketName = "*** Provide bucket name ***"; static IAmazonS3 client; public static void Main(string[] args) { try { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2)) { // Create a new configuration request and add two rules CORSConfiguration configuration = new CORSConfiguration { Rules = new System.Collections.Generic.List<CORSRule> { new CORSRule { Id = "CORSRule1", AllowedMethods = new List<string> {"PUT", "POST", "DELETE"}, AllowedOrigins = new List<string> {"http://*.example.com"} }, new CORSRule { Id = "CORSRule2", AllowedMethods = new List<string> {"GET"}, AllowedOrigins = new List<string> {"*"}, MaxAgeSeconds = 3000, ExposeHeaders = new List<string> {"x-amz-server-side-encryption"} } } }; // Add the configuration to the bucket PutCORSConfiguration(configuration); // Retrieve an existing configuration configuration = GetCORSConfiguration(); // Add a new rule. configuration.Rules.Add(new CORSRule { Id = "CORSRule3", AllowedMethods = new List<string> { "HEAD" }, AllowedOrigins = new List<string> { "http://www.example.com" } }); // Add the configuration to the bucket PutCORSConfiguration(configuration); // Verify that there are now three rules configuration = GetCORSConfiguration(); Console.WriteLine(); Console.WriteLine("Expected # of rulest=3; found:{0}", configuration.Rules.Count); Console.WriteLine(); Console.WriteLine("Pause before configuration delete. To continue, click Enter..."); Console.ReadKey(); // Delete the configuration DeleteCORSConfiguration(); // Retrieve a nonexistent configuration configuration = GetCORSConfiguration(); Debug.Assert(configuration == null); } Console.WriteLine("Example complete."); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString()); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); Console.ReadKey(); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void PutCORSConfiguration(CORSConfiguration configuration) { PutCORSConfigurationRequest request = new PutCORSConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = client.PutCORSConfiguration(request); } static CORSConfiguration GetCORSConfiguration() { GetCORSConfigurationRequest request = new GetCORSConfigurationRequest { BucketName = bucketName }; var response = client.GetCORSConfiguration(request); var configuration = response.Configuration; PrintCORSRules(configuration); return configuration; } static void DeleteCORSConfiguration() { DeleteCORSConfigurationRequest request = new DeleteCORSConfigurationRequest { BucketName = bucketName }; client.DeleteCORSConfiguration(request); } static void PrintCORSRules(CORSConfiguration configuration) { Console.WriteLine(); if (configuration == null) { Console.WriteLine("\nConfiguration is null"); return; } Console.WriteLine("Configuration has {0} rules:", configuration.Rules.Count); foreach (CORSRule rule in configuration.Rules) { Console.WriteLine("Rule ID: {0}", rule.Id); Console.WriteLine("MaxAgeSeconds: {0}", rule.MaxAgeSeconds); Console.WriteLine("AllowedMethod: {0}", string.Join(", ", rule.AllowedMethods.ToArray())); Console.WriteLine("AllowedOrigins: {0}", string.Join(", ", rule.AllowedOrigins.ToArray())); Console.WriteLine("AllowedHeaders: {0}", string.Join(", ", rule.AllowedHeaders.ToArray())); Console.WriteLine("ExposeHeader: {0}", string.Join(", ", rule.ExposeHeaders.ToArray())); } } } }