Articles → AWS SDK AND CSHARP → Enable Static Website Hosting On S3 Bucket Using C#

Enable Static Website Hosting On S3 Bucket Using C#






Code To Enable Static Website Hosting On S3 Bucket




using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System.Threading.Tasks;

namespace dotnetS3
{
    class Program
    {

        public static void Main(string[] args)
        {

            string bucketName = "gyansangrah-bucket";

            EnableStaticWebsiteHosting(bucketName).Wait();
        }

        static async Task EnableStaticWebsiteHosting(string bucketName)
        {
            var region = RegionEndpoint.GetBySystemName("ap-south-1");
            AWSCredentials credentails = new BasicAWSCredentials("<Access ID>>", "<<Secret Key>>");

            AmazonS3Client s3Client = new AmazonS3Client(credentails, region);
            PutBucketWebsiteResponse response = await s3Client.PutBucketWebsiteAsync(new PutBucketWebsiteRequest
            {
                BucketName = bucketName,
                WebsiteConfiguration = new WebsiteConfiguration
                {
                    ErrorDocument = "error.html",
                    IndexDocumentSuffix = "index.html"
                }
            });
        }

       

    }
}



Output


Picture showing the static website option enabled in S3 bucket
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 10, 2022

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250