| Did this page help you? Yes No Tell us about it... |
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 |
|
2 |
To add website configuration to a bucket, execute the
To retrieve website configuration, execute the
To delete your bucket website configuration, execute the
|
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;
}
}
}