Skip to content

Commit

Permalink
Adding a new example to show how to configure an S3 website.
Browse files Browse the repository at this point in the history
  • Loading branch information
irenepsmith committed Jun 22, 2021
1 parent 52af058 commit d90d918
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
34 changes: 34 additions & 0 deletions dotnetv3/S3/WebsiteConfigExample/WebsiteConfigExample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.6.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebsiteConfigExample", "WebsiteConfigExample\WebsiteConfigExample.csproj", "{846AA466-7695-4903-81A9-A5177EEBB96B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|x64.ActiveCfg = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|x64.Build.0 = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|x86.ActiveCfg = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Debug|x86.Build.0 = Debug|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|Any CPU.Build.0 = Release|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|x64.ActiveCfg = Release|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|x64.Build.0 = Release|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|x86.ActiveCfg = Release|Any CPU
{846AA466-7695-4903-81A9-A5177EEBB96B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved
// SPDX-License-Identifier: Apache-2.0

namespace WebsiteConfigExample
{
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

/// <summary>
/// This example uses Amazon Simple Storage Service (Amazon S3) to
/// configure a static website in an Amazon S3 bucket. The example was
/// created using AWS SDK for .NET version 3.7 and .NET Core 5.0.
/// </summary>
public class WebsiteConfig
{
/// <summary>
/// The Main method initializes the values for Amazon Simple Storage
/// Service (Amazon S3) and the document and error document suffix
/// values.
/// </summary>
public static async Task Main()
{
const string bucketName = "doc-example-bucket";
const string indexDocumentSuffix = "index.html";
const string errorDocument = "error.html";

// Specify the region for your S3 bucket if it is different from the region
// of the default user. RegionEndpoint.USWest2 for example.
IAmazonS3 client = new AmazonS3Client(RegionEndpoint.USEast1);

await AddWebsiteConfigurationAsync(client, bucketName, indexDocumentSuffix, errorDocument);
}

/// <summary>
/// This method first adds and then checks the configuration for a
/// static website in an S3 bucket.
/// </summary>
/// <param name="client">The S3 client used to add and then check the
/// website configuration.</param>
/// <param name="bucketName">The name of the bucket that will serve as
/// a static website.</param>
/// <param name="indexDocumentSuffix">The index document suffix for the
/// website.</param>
/// <param name="errorDocument">The name of the error document for the
/// static website.</param>
public static async Task AddWebsiteConfigurationAsync(
IAmazonS3 client,
string bucketName,
string indexDocumentSuffix,
string errorDocument)
{
try
{
// Put the website configuration.
PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest()
{
BucketName = bucketName,
WebsiteConfiguration = new WebsiteConfiguration()
{
IndexDocumentSuffix = indexDocumentSuffix,
ErrorDocument = errorDocument,
},
};
PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest);

// Get the website configuration.
GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest()
{
BucketName = bucketName,
};
GetBucketWebsiteResponse getResponse = await client.GetBucketWebsiteAsync(getRequest);
Console.WriteLine($"Index document: {getResponse.WebsiteConfiguration.IndexDocumentSuffix}");
Console.WriteLine($"Error document: {getResponse.WebsiteConfiguration.ErrorDocument}");
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' when writing an object.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.0.30" />
<PackageReference Include="AWSSDK.S3" Version="3.7.0.31" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit d90d918

Please sign in to comment.