Skip to content

Commit 7eec48b

Browse files
committed
Updates to add hello example for v4
1 parent 64a9989 commit 7eec48b

10 files changed

Lines changed: 169 additions & 25 deletions

File tree

.doc_gen/metadata/s3_metadata.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ s3_Hello:
44
synopsis: get started using &S3;.
55
category: Hello
66
languages:
7+
.NET:
8+
versions:
9+
- sdk_version: 4
10+
github: dotnetv4/S3
11+
sdkguide:
12+
excerpts:
13+
- description:
14+
snippet_tags:
15+
- S3.dotnetv4.S3_Hello
716
Java:
817
versions:
918
- sdk_version: 2

dotnetv4/DotNetV4Examples.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{2B3E1EA1
175175
EndProject
176176
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3PresignedPostTests", "S3\Tests\S3_PresignedPostTests\S3PresignedPostTests.csproj", "{FA06BC34-332D-5CA4-09EE-B34A65EF0FFD}"
177177
EndProject
178+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{F03ACBAF-50CE-4E7E-BEBE-4F42673E73E1}"
179+
EndProject
180+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3_Actions", "S3\Actions\S3_Actions.csproj", "{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}"
181+
EndProject
178182
Global
179183
GlobalSection(SolutionConfigurationPlatforms) = preSolution
180184
Debug|Any CPU = Debug|Any CPU
@@ -893,6 +897,18 @@ Global
893897
{FA06BC34-332D-5CA4-09EE-B34A65EF0FFD}.Release|x64.Build.0 = Release|Any CPU
894898
{FA06BC34-332D-5CA4-09EE-B34A65EF0FFD}.Release|x86.ActiveCfg = Release|Any CPU
895899
{FA06BC34-332D-5CA4-09EE-B34A65EF0FFD}.Release|x86.Build.0 = Release|Any CPU
900+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
901+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
902+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|x64.ActiveCfg = Debug|Any CPU
903+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|x64.Build.0 = Debug|Any CPU
904+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|x86.ActiveCfg = Debug|Any CPU
905+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Debug|x86.Build.0 = Debug|Any CPU
906+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
907+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|Any CPU.Build.0 = Release|Any CPU
908+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|x64.ActiveCfg = Release|Any CPU
909+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|x64.Build.0 = Release|Any CPU
910+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|x86.ActiveCfg = Release|Any CPU
911+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE}.Release|x86.Build.0 = Release|Any CPU
896912
EndGlobalSection
897913
GlobalSection(SolutionProperties) = preSolution
898914
HideSolutionNode = FALSE
@@ -970,6 +986,8 @@ Global
970986
{8E8DBC13-E421-9394-21CE-766ED2A9A237} = {2B3E1EA1-8665-4B2C-8960-82008AF47A78}
971987
{2B3E1EA1-8665-4B2C-8960-82008AF47A78} = {F929DB74-DD0E-B0EF-AA66-D8703D547BBD}
972988
{FA06BC34-332D-5CA4-09EE-B34A65EF0FFD} = {2B3E1EA1-8665-4B2C-8960-82008AF47A78}
989+
{F03ACBAF-50CE-4E7E-BEBE-4F42673E73E1} = {F929DB74-DD0E-B0EF-AA66-D8703D547BBD}
990+
{F71D8C59-C3B0-E86C-FBE0-D0450DBE00EE} = {F03ACBAF-50CE-4E7E-BEBE-4F42673E73E1}
973991
EndGlobalSection
974992
GlobalSection(ExtensibilityGlobals) = postSolution
975993
SolutionGuid = {08502818-E8E1-4A91-A51C-4C8C8D4FF9CA}

dotnetv4/S3/Actions/HelloS3.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Amazon.S3;
5+
using Amazon.S3.Model;
6+
7+
namespace S3_Actions;
8+
9+
// snippet-start:[S3.dotnetv4.S3_Hello]
10+
/// <summary>
11+
/// Hello AWS S3 example.
12+
/// </summary>
13+
public class HelloS3
14+
{
15+
/// <summary>
16+
/// Main method to run the Hello S3 example.
17+
/// </summary>
18+
/// <param name="args">Command line arguments.</param>
19+
/// <returns>A Task object.</returns>
20+
public static async Task Main(string[] args)
21+
{
22+
var s3Client = new AmazonS3Client();
23+
24+
try
25+
{
26+
Console.WriteLine("Hello AWS S3! Let's list your buckets:");
27+
Console.WriteLine(new string('-', 80));
28+
29+
// Use the built-in paginator to list buckets
30+
var request = new ListBucketsRequest();
31+
var paginator = s3Client.Paginators.ListBuckets(request);
32+
33+
var buckets = new List<S3Bucket>();
34+
35+
await foreach (var response in paginator.Responses)
36+
{
37+
buckets.AddRange(response.Buckets);
38+
}
39+
40+
if (buckets.Any())
41+
{
42+
Console.WriteLine($"Found {buckets.Count} S3 buckets:");
43+
Console.WriteLine();
44+
45+
foreach (var bucket in buckets)
46+
{
47+
Console.WriteLine($"- Bucket Name: {bucket.BucketName}");
48+
Console.WriteLine($" Creation Date: {bucket.CreationDate:yyyy-MM-dd HH:mm:ss UTC}");
49+
Console.WriteLine();
50+
}
51+
}
52+
else
53+
{
54+
Console.WriteLine("No S3 buckets found in your account.");
55+
}
56+
57+
Console.WriteLine("Hello S3 completed successfully.");
58+
}
59+
catch (AmazonS3Exception ex)
60+
{
61+
Console.WriteLine($"S3 service error occurred: {ex.Message}");
62+
}
63+
catch (Exception ex)
64+
{
65+
Console.WriteLine($"Couldn't list S3 buckets. Here's why: {ex.Message}");
66+
}
67+
}
68+
}
69+
// snippet-end:[S3.dotnetv4.S3_Hello]

dotnetv4/S3/Scenarios/S3_Basics/S3Bucket.cs renamed to dotnetv4/S3/Actions/S3Wrapper.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
namespace S3_BasicsScenario;
54
// snippet-start:[S3.dotnetv4.S3_BasicsBucket]
5+
using Amazon.S3;
6+
using Amazon.S3.Model;
7+
8+
namespace S3_Actions;
69

710
/// <summary>
811
/// This class contains all of the methods for working with Amazon Simple
912
/// Storage Service (Amazon S3) buckets.
1013
/// </summary>
11-
public class S3Bucket
14+
public class S3Wrapper
1215
{
1316
private readonly IAmazonS3 _amazonS3;
1417

1518
/// <summary>
16-
/// Initializes a new instance of the <see cref="S3Bucket"/> class.
19+
/// Initializes a new instance of the <see cref="S3Wrapper"/> class.
1720
/// </summary>
1821
/// <param name="amazonS3">An initialized Amazon S3 client object.</param>
19-
public S3Bucket(IAmazonS3 amazonS3)
22+
public S3Wrapper(IAmazonS3 amazonS3)
2023
{
2124
_amazonS3 = amazonS3;
2225
}
@@ -176,9 +179,9 @@ public async Task<bool> CopyObjectInBucketAsync(
176179
/// Shows how to list the objects in an Amazon S3 bucket.
177180
/// </summary>
178181
/// <param name="bucketName">The name of the bucket for which to list.
179-
/// the contents.</param>
182+
/// <param name="printList">True to print out the list.
180183
/// <returns>The collection of objects.</returns>
181-
public async Task<List<S3Object>> ListBucketContentsAsync(string bucketName)
184+
public async Task<List<S3Object>?> ListBucketContentsAsync(string bucketName, bool printList = true)
182185
{
183186
try
184187
{
@@ -188,9 +191,12 @@ public async Task<List<S3Object>> ListBucketContentsAsync(string bucketName)
188191
MaxKeys = 5,
189192
};
190193

191-
Console.WriteLine("--------------------------------------");
192-
Console.WriteLine($"Listing the contents of {bucketName}:");
193-
Console.WriteLine("--------------------------------------");
194+
if (printList)
195+
{
196+
Console.WriteLine("--------------------------------------");
197+
Console.WriteLine($"Listing the contents of {bucketName}:");
198+
Console.WriteLine("--------------------------------------");
199+
}
194200

195201
var listObjectsV2Paginator = _amazonS3.Paginators.ListObjectsV2(new ListObjectsV2Request
196202
{
@@ -205,10 +211,13 @@ public async Task<List<S3Object>> ListBucketContentsAsync(string bucketName)
205211
}
206212
}
207213

208-
Console.WriteLine($"Number of Objects: {s3Objects.Count}");
209-
foreach (var entry in s3Objects)
214+
if (printList)
210215
{
211-
Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}");
216+
Console.WriteLine($"Number of Objects: {s3Objects.Count}");
217+
foreach (var entry in s3Objects)
218+
{
219+
Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}");
220+
}
212221
}
213222

214223
return s3Objects;
@@ -237,7 +246,7 @@ public async Task<bool> DeleteBucketContentsAsync(string bucketName)
237246
try
238247
{
239248
// Delete all objects in the bucket.
240-
var deleteList = await ListBucketContentsAsync(bucketName);
249+
var deleteList = await ListBucketContentsAsync(bucketName, false);
241250
if (deleteList != null && deleteList.Any())
242251
{
243252
await _amazonS3.DeleteObjectsAsync(new DeleteObjectsRequest()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AWSSDK.S3" Version="4.0.18.2" />
12+
</ItemGroup>
13+
14+
</Project>

dotnetv4/S3/README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv4
2929
<!--custom.prerequisites.start-->
3030
<!--custom.prerequisites.end-->
3131

32+
### Get started
33+
34+
- [Hello Amazon S3](Actions/HelloS3.cs#L9) (`ListBuckets`)
35+
36+
3237
### Basics
3338

3439
Code examples that show you how to perform the essential operations within a service.
@@ -40,14 +45,14 @@ Code examples that show you how to perform the essential operations within a ser
4045

4146
Code excerpts that show you how to call individual service functions.
4247

43-
- [CopyObject](Scenarios/S3_Basics/S3Bucket.cs#L134)
44-
- [CreateBucket](Scenarios/S3_Basics/S3Bucket.cs#L24)
48+
- [CopyObject](Actions/S3Wrapper.cs#L137)
49+
- [CreateBucket](Actions/S3Wrapper.cs#L27)
4550
- [CreatePresignedPost](Scenarios/S3_CreatePresignedPost/S3Wrapper.cs#L35)
46-
- [DeleteBucket](Scenarios/S3_Basics/S3Bucket.cs#L261)
47-
- [DeleteObjects](Scenarios/S3_Basics/S3Bucket.cs#L225)
48-
- [GetObject](Scenarios/S3_Basics/S3Bucket.cs#L93)
49-
- [ListObjectsV2](Scenarios/S3_Basics/S3Bucket.cs#L173)
50-
- [PutObject](Scenarios/S3_Basics/S3Bucket.cs#L54)
51+
- [DeleteBucket](Actions/S3Wrapper.cs#L270)
52+
- [DeleteObjects](Actions/S3Wrapper.cs#L234)
53+
- [GetObject](Actions/S3Wrapper.cs#L96)
54+
- [ListObjectsV2](Actions/S3Wrapper.cs#L176)
55+
- [PutObject](Actions/S3Wrapper.cs#L57)
5156

5257
### Scenarios
5358

@@ -68,6 +73,10 @@ functions within the same service.
6873
<!--custom.instructions.start-->
6974
<!--custom.instructions.end-->
7075

76+
#### Hello Amazon S3
77+
78+
This example shows you how to get started using Amazon S3.
79+
7180

7281
#### Learn the basics
7382

dotnetv4/S3/S3Examples.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3_BasicsScenarioTests", "T
1414
EndProject
1515
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3PresignedPostTests", "Tests\S3_PresignedPostTests\S3PresignedPostTests.csproj", "{E4309612-564B-57EA-B85D-A794AA690BFC}"
1616
EndProject
17+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{82700254-B9D3-4BB5-9DB4-1ECB64E44A8A}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3_Actions", "Actions\S3_Actions.csproj", "{E7B624EA-CB01-B7D5-262D-E232F24C23F3}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
@@ -36,6 +40,10 @@ Global
3640
{E4309612-564B-57EA-B85D-A794AA690BFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
3741
{E4309612-564B-57EA-B85D-A794AA690BFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
3842
{E4309612-564B-57EA-B85D-A794AA690BFC}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{E7B624EA-CB01-B7D5-262D-E232F24C23F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{E7B624EA-CB01-B7D5-262D-E232F24C23F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{E7B624EA-CB01-B7D5-262D-E232F24C23F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{E7B624EA-CB01-B7D5-262D-E232F24C23F3}.Release|Any CPU.Build.0 = Release|Any CPU
3947
EndGlobalSection
4048
GlobalSection(SolutionProperties) = preSolution
4149
HideSolutionNode = FALSE
@@ -45,6 +53,7 @@ Global
4553
{3D292DB7-AE7E-008B-82F1-AA0AD525620C} = {C13DDD1A-438D-4E52-90FB-A496A54516C7}
4654
{56441456-07D5-B31C-6288-D6EBA5E79E5B} = {C51625C8-3B42-4810-BF1B-0E3C6C716FA6}
4755
{E4309612-564B-57EA-B85D-A794AA690BFC} = {C51625C8-3B42-4810-BF1B-0E3C6C716FA6}
56+
{E7B624EA-CB01-B7D5-262D-E232F24C23F3} = {82700254-B9D3-4BB5-9DB4-1ECB64E44A8A}
4857
EndGlobalSection
4958
GlobalSection(ExtensibilityGlobals) = postSolution
5059
SolutionGuid = {5ACB5B08-E8F8-453C-B63B-6C0C9DE67780}

dotnetv4/S3/Scenarios/S3_Basics/S3_Basics.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// computer. It also shows how to copy an object within an Amazon S3 bucket,
88
// list the bucket's contents, and finally how to delete the objects in the
99
// bucket before deleting the bucket itself.
10+
using S3_Actions;
11+
1012
namespace S3_BasicsScenario;
1113

1214
// snippet-start:[S3.dotnetv4.S3_BasicsScenario]
@@ -15,7 +17,7 @@ public class S3_Basics
1517
public static bool IsInteractive = true;
1618
public static string BucketName = null!;
1719
public static string TempFilePath = null!;
18-
public static S3Bucket _s3Wrapper = null!;
20+
public static S3Wrapper _s3Wrapper = null!;
1921
public static ILogger<S3_Basics> _logger = null!;
2022

2123
public static async Task Main(string[] args)
@@ -24,14 +26,14 @@ public static async Task Main(string[] args)
2426
using var host = Host.CreateDefaultBuilder(args)
2527
.ConfigureServices((_, services) =>
2628
services.AddAWSService<IAmazonS3>()
27-
.AddTransient<S3Bucket>()
29+
.AddTransient<S3Wrapper>()
2830
.AddLogging(builder => builder.AddConsole()))
2931
.Build();
3032

3133
_logger = LoggerFactory.Create(builder => builder.AddConsole())
3234
.CreateLogger<S3_Basics>();
3335

34-
_s3Wrapper = host.Services.GetRequiredService<S3Bucket>();
36+
_s3Wrapper = host.Services.GetRequiredService<S3Wrapper>();
3537

3638
var sepBar = new string('-', 45);
3739

@@ -59,7 +61,7 @@ public static async Task Main(string[] args)
5961
/// <param name="s3Wrapper">The S3 wrapper instance.</param>
6062
/// <param name="scenarioLogger">The logger instance.</param>
6163
/// <returns>A Task object.</returns>
62-
public static async Task RunScenario(S3Bucket s3Wrapper, ILogger<S3_Basics> scenarioLogger)
64+
public static async Task RunScenario(S3Wrapper s3Wrapper, ILogger<S3_Basics> scenarioLogger)
6365
{
6466
string bucketName = BucketName;
6567
string filePath = TempFilePath;

dotnetv4/S3/Scenarios/S3_Basics/S3_BasicsScenario.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
</PackageReference>
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<ProjectReference Include="..\..\Actions\S3_Actions.csproj" />
25+
</ItemGroup>
26+
2327
</Project>

dotnetv4/S3/Tests/S3_BasicsScenarioTests/S3IntegrationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using Microsoft.Extensions.Logging;
5+
using S3_Actions;
56
using S3_BasicsScenario;
67

78
namespace S3_BasicsScenarioTests;
@@ -50,7 +51,7 @@ public async Task TestScenarioIntegration()
5051
S3_Basics._logger = loggerScenarioMock.Object;
5152

5253
// Set up the wrapper with real AWS S3 client
53-
S3_Basics._s3Wrapper = new S3_BasicsScenario.S3Bucket(new AmazonS3Client());
54+
S3_Basics._s3Wrapper = new S3Wrapper(new AmazonS3Client());
5455

5556
await S3_Basics.RunScenario(S3_Basics._s3Wrapper, S3_Basics._logger);
5657

0 commit comments

Comments
 (0)