Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Managing Websites with the AWS SDK for .NET

The following example shows how to use the AWS SDK for .NET to manage website configuration for a bucket. To add a website configuration to a bucket, you provide a bucket name and a website configuration. The website configuration must include an index document and can contain an optional error document. These documents must be stored in the bucket. For more information, see PUT Bucket website. For more information about the Amazon S3 website feature, see Hosting a Static Website on Amazon S3.

The following C# code example adds a website configuration to the specified bucket. The configuration specifies both the index document and the error document names. For instructions on how to create and test a working sample, see Running the Amazon S3 .NET Code Examples.

// Copyright 2018, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see 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); } } } }