Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

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

以下任务引导您使用 .NET 类来管理存储桶上的网站配置。有关 Amazon S3 网站功能的更多信息,请参阅在 Amazon S3 上托管静态网站

管理存储桶网站配置

1

创建 AmazonS3Client 类的实例。

2

要将网站配置添加到存储桶,请执行 PutBucketWebsite 方法。您需要提供存储桶名称和网站配置信息,包括索引文档和错误文档名称。您必须提供索引文档,但错误文档是可选的。通过创建 PutBucketWebsiteRequest 对象来提供此信息。

要检索网站配置,请通过提供存储桶名称来执行 GetBucketWebsite 方法。

要删除存储桶网站配置,请通过提供存储桶名称来执行 DeleteBucketWebsite 方法。在删除网站配置之后,无法再从网站终端节点使用存储桶。有关更多信息,请参阅 网站终端节点

以下 C# 代码示例演示了上述任务。

Copy
static IAmazonS3 client; client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2); // Add website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument } }; client.PutBucketWebsite(putRequest); // Get bucket website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName }; GetBucketWebsiteResponse getResponse = client.GetBucketWebsite(getRequest); // Print configuration data. Console.WriteLine("Index document: {0}", getResponse.WebsiteConfiguration.IndexDocumentSuffix); Console.WriteLine("Error document: {0}", getResponse.WebsiteConfiguration.ErrorDocument); // Delete website configuration. DeleteBucketWebsiteRequest deleteRequest = new DeleteBucketWebsiteRequest() { BucketName = bucketName }; client.DeleteBucketWebsite(deleteRequest);

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

Copy
using System; using System.Configuration; using System.Collections.Specialized; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class AddWebsiteConfig { static string bucketName = "*** Provide existing bucket name ***"; static string indexDocumentSuffix = "*** Provide index document name ***"; static string errorDocument = "*** Provide error document name ***"; static IAmazonS3 client; public static void Main(string[] args) { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2)) { Console.WriteLine("Adding website configuration"); AddWebsiteConfiguration(bucketName, indexDocumentSuffix, errorDocument); } // Get bucket website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName }; GetBucketWebsiteResponse getResponse = client.GetBucketWebsite(getRequest); // Print configuration data. Console.WriteLine("Index document: {0}", getResponse.WebsiteConfiguration.IndexDocumentSuffix); Console.WriteLine("Error document: {0}", getResponse.WebsiteConfiguration.ErrorDocument); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void AddWebsiteConfiguration(string bucketName, string indexDocumentSuffix, string errorDocument) { try { PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument } }; client.PutBucketWebsite(putRequest); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine("Sign up for service at http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error:{0}, occurred when adding website configuration. Message:'{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message); } } } } }