Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Managing Websites with the AWS SDK for .NET

The following tasks guide you through using the .NET classes to manage website configuration on your bucket. For more information about the Amazon S3 website feature, see Hosting Websites on Amazon S3.

Managing Bucket Website Configuration

1

Create an instance of the AmazonS3 class by providing your AWS credentials.

2

To add website configuration to a bucket, execute the AmazonS3.PutBucketWebsite method. You need to provide the bucket name and the website configuration information including the index document and the error document names. You must provide the index document, however, the error document is optional. You provide this information by creating a PutBucketWebsiteRequest object.

To retrieve website configuration, execute the AmazonS3.GetBucketWebsite method by providing the bucket name.

To delete your bucket website configuration, execute the AmazonS3.DeleteBucketWebsite method by providing the bucket name. After you remove the website configuration, the bucket is no longer available via the website endpoint. For more information, see see Website Endpoints.


The following C# code sample demonstrates the preceding tasks.

AmazonS3 client;
client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID);
// Add website configuration.
PutBucketWebsiteRequest putRequest =
   new PutBucketWebsiteRequest()
        .WithBucketName(bucketName)
        .WithWebsiteConfiguration(new WebsiteConfiguration()
        .WithIndexDocumentSuffix(indexDocumentSuffix)
        .WithErrorDocument(errorDocument));

client.PutBucketWebsite(putRequest);

// Get bucket website configuration.
GetBucketWebsiteRequest getRequest =
   new GetBucketWebsiteRequest()
     .WithBucketName(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()
        .WithBucketName(bucketName);

client.DeleteBucketWebsite(deleteRequest);

Example

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 Testing the .NET Code Examples.

using System;
using System.Configuration;
using System.Collections.Specialized;
using Amazon.S3;
using Amazon.S3.Model;

namespace s3.amazon.com.docsamples.addwebsiteconfiguration
{
    class S3Sample
    {
        static string bucketName          = "*** Provide existing bucket name ***";
        static string indexDocumentSuffix = "*** Provide index document name ***";
        static string errorDocument       = "*** Provide error document name ***";
        static AmazonS3 client;

        public static void Main(string[] args)
        {
            if (checkRequiredFields())
            {
                NameValueCollection appConfig =
                    ConfigurationManager.AppSettings;

                string accessKeyID = appConfig["AWSAccessKey"];
                string secretAccessKeyID = appConfig["AWSSecretKey"];

                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID))
                {
                    AddWebsiteConfig(bucketName, indexDocumentSuffix, errorDocument); 
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static void AddWebsiteConfig(string bucketName,
                                     string indexDocumentSuffix,
                                     string errorDocument)
        {
            try
            {
                PutBucketWebsiteRequest putRequest =
                    new PutBucketWebsiteRequest()
                    .WithBucketName(bucketName)
                    .WithWebsiteConfiguration(new WebsiteConfiguration()
                        .WithIndexDocumentSuffix(indexDocumentSuffix)
                        .WithErrorDocument(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);
                }
            }
        }

        static bool checkRequiredFields()
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            if (string.IsNullOrEmpty(appConfig["AWSAccessKey"]))
            {
                Console.WriteLine(
                    "AWSAccessKey was not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(appConfig["AWSSecretKey"]))
            {
                Console.WriteLine(
                    "AWSSecretKey was not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(bucketName))
            {
                Console.WriteLine("The variable bucketName is not set.");
                return false;
            }


            return true;
        }

    }
}