Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用适用于 .NET 的 AWS 开发工具包管理网站

以下示例说明如何使用适用于 .NET 的 AWS 开发工具包管理存储桶的网站配置。要将网站配置添加到存储桶,请提供存储桶名称和网站配置。网站配置必须包含索引文档,并且可包含可选的错误文档。这些文档必须存储在存储桶中。有关更多信息,请参阅 PUT 存储桶网站。有关 Amazon S3 网站功能的更多信息,请参阅在 Amazon S3 上托管静态网站

以下 C# 代码示例将网站配置添加到指定的存储桶。该配置指定索引文档和错误文档名称。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

using Amazon.S3; using Amazon.S3.Model; using System; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class WebsiteConfigTest { private const string bucketName = "*** bucket name ***"; private const string indexDocumentSuffix = "*** index object key ***"; // For example, index.html. private const string errorDocument = "*** error object key ***"; // For example, error.html. // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); AddWebsiteConfigurationAsync(bucketName, indexDocumentSuffix, errorDocument).Wait(); } static async Task AddWebsiteConfigurationAsync(string bucketName, string indexDocumentSuffix, string errorDocument) { try { // 1. Put the website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument } }; PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest); // 2. Get the website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName }; GetBucketWebsiteResponse getResponse = await client.GetBucketWebsiteAsync(getRequest); Console.WriteLine("Index document: {0}", getResponse.WebsiteConfiguration.IndexDocumentSuffix); Console.WriteLine("Error document: {0}", getResponse.WebsiteConfiguration.ErrorDocument); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } } }