AWS code-library documentation change
Summary
Updated .NET SDK version from v3 to v4, refactored code to use dependency injection, improved error handling, simplified file paths, and modernized code structure. Added XML documentation and exception handling for DynamoDB operations.
Security assessment
The changes primarily focus on SDK version upgrade and code quality improvements. While added exception handling improves robustness, there's no evidence of addressing specific security vulnerabilities. No new security features or explicit security guidance were introduced.
Diff
diff --git a/code-library/latest/ug/dynamodb_example_dynamodb_Scenario_GettingStartedMovies_section.md b/code-library/latest/ug/dynamodb_example_dynamodb_Scenario_GettingStartedMovies_section.md index 154ee227c..f4d10e624 100644 --- a//code-library/latest/ug/dynamodb_example_dynamodb_Scenario_GettingStartedMovies_section.md +++ b//code-library/latest/ug/dynamodb_example_dynamodb_Scenario_GettingStartedMovies_section.md @@ -29 +29 @@ The following code examples show how to: -**SDK for .NET** +**SDK for .NET (v4)** @@ -34 +34 @@ The following code examples show how to: -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv3/dynamodb#code-examples). +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/dotnetv4/DynamoDB#code-examples). @@ -37,17 +37,14 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // This example application performs the following basic Amazon DynamoDB - // functions: - // - // CreateTableAsync - // PutItemAsync - // UpdateItemAsync - // BatchWriteItemAsync - // GetItemAsync - // DeleteItemAsync - // Query - // Scan - // DeleteItemAsync - // - using Amazon.DynamoDBv2; - using DynamoDB_Actions; - - public class DynamoDB_Basics + /// <summary> + /// This example application performs the following basic Amazon DynamoDB + /// functions: + /// CreateTableAsync + /// PutItemAsync + /// UpdateItemAsync + /// BatchWriteItemAsync + /// GetItemAsync + /// DeleteItemAsync + /// Query + /// Scan + /// DeleteItemAsync. + /// </summary> + public class DynamoDbBasics @@ -54,0 +52,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + public static bool IsInteractive = true; + @@ -58 +57,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - public static async Task Main() + /// <summary> + /// The main entry point for the DynamoDB Basics example application. + /// </summary> + /// <param name="args">Command line arguments.</param> + /// <returns>A task representing the asynchronous operation.</returns> + public static async Task Main(string[] args) @@ -60 +64,9 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var client = new AmazonDynamoDBClient(); + // Set up dependency injection for Amazon DynamoDB. + using var host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) + .ConfigureServices((_, services) => + services.AddAWSService<IAmazonDynamoDB>() + .AddTransient<DynamoDbWrapper>()) + .Build(); + + // Now the wrapper is available for injection. + var dynamoDbWrapper = host.Services.GetRequiredService<DynamoDbWrapper>(); @@ -64,2 +76 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // Relative path to moviedata.json in the local repository. - var movieFileName = @"..\..\..\..\..\..\..\..\resources\sample_files\movies.json"; + var movieFileName = @"movies.json"; @@ -72 +83 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var success = await DynamoDbMethods.CreateMovieTableAsync(client, tableName); + var success = await dynamoDbWrapper.CreateMovieTableAsync(tableName); @@ -74,8 +85,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - if (success) - { - Console.WriteLine($"\nTable: {tableName} successfully created."); - } - else - { - Console.WriteLine($"\nCould not create {tableName}."); - } + Console.WriteLine(success + ? $"\nTable: {tableName} successfully created." + : $"\nCould not create {tableName}."); @@ -92 +98 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - success = await DynamoDbMethods.PutItemAsync(client, newMovie, tableName); + success = await dynamoDbWrapper.PutItemAsync(newMovie, tableName); @@ -114 +120 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - success = await DynamoDbMethods.UpdateItemAsync(client, newMovie, newInfo, tableName); + success = await dynamoDbWrapper.UpdateItemAsync(newMovie, newInfo, tableName); @@ -128 +134 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var itemCount = await DynamoDbMethods.BatchWriteItemsAsync(client, movieFileName); + var itemCount = await dynamoDbWrapper.BatchWriteItemsAsync(movieFileName, tableName); @@ -141,2 +147,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var item = await DynamoDbMethods.GetItemAsync(client, lookupMovie, tableName); - if (item.Count > 0) + var item = await dynamoDbWrapper.GetItemAsync(lookupMovie, tableName); + if (item?.Count > 0) @@ -144 +150 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - DynamoDbMethods.DisplayItem(item); + dynamoDbWrapper.DisplayItem(item); @@ -160 +166 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - success = await DynamoDbMethods.DeleteItemAsync(client, tableName, movieToDelete); + success = await dynamoDbWrapper.DeleteItemAsync(tableName, movieToDelete); @@ -176 +182 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var queryCount = await DynamoDbMethods.QueryMoviesAsync(client, tableName, findYear); + var queryCount = await dynamoDbWrapper.QueryMoviesAsync(tableName, findYear); @@ -184 +190 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - var scanCount = await DynamoDbMethods.ScanTableAsync(client, tableName, startYear, endYear); + var scanCount = await dynamoDbWrapper.ScanTableAsync(tableName, startYear, endYear); @@ -190 +196 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - success = await DynamoDbMethods.DeleteTableAsync(client, tableName); + success = await dynamoDbWrapper.DeleteTableAsync(tableName); @@ -201 +207 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - Console.WriteLine("The DynamoDB Basics example application is done."); + Console.WriteLine("The DynamoDB Basics example application is complete."); @@ -210,0 +217,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + if (!IsInteractive) + { + return; + } + @@ -234,0 +246,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + { + if (IsInteractive) @@ -240,0 +254 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + } @@ -252 +265,0 @@ Creates a table to contain movie data. - /// <param name="client">An initialized Amazon DynamoDB client object.</param> @@ -255 +268,3 @@ Creates a table to contain movie data. - public static async Task<bool> CreateMovieTableAsync(AmazonDynamoDBClient client, string tableName) + public async Task<bool> CreateMovieTableAsync(string tableName) + { + try @@ -257 +272 @@ Creates a table to contain movie data. - var response = await client.CreateTableAsync(new CreateTableRequest + var response = await _amazonDynamoDB.CreateTableAsync(new CreateTableRequest @@ -303 +318 @@ Creates a table to contain movie data. - System.Threading.Thread.Sleep(sleepDuration); + Thread.Sleep(sleepDuration); @@ -305 +320 @@ Creates a table to contain movie data. - var describeTableResponse = await client.DescribeTableAsync(request); + var describeTableResponse = await _amazonDynamoDB.DescribeTableAsync(request); @@ -313,0 +329,16 @@ Creates a table to contain movie data. + catch (ResourceInUseException ex) + { + Console.WriteLine($"Table {tableName} already exists. {ex.Message}"); + throw; + } + catch (AmazonDynamoDBException ex) + { + Console.WriteLine($"An Amazon DynamoDB error occurred while creating table {tableName}. {ex.Message}"); + throw; + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred while creating table {tableName}. {ex.Message}"); + throw; + } + } @@ -324 +354,0 @@ Adds a single movie to the table. - /// <param name="client">An initialized Amazon DynamoDB client object.</param> @@ -329 +359,3 @@ Adds a single movie to the table. - public static async Task<bool> PutItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) + public async Task<bool> PutItemAsync(Movie newMovie, string tableName) + { + try @@ -343,2 +375,18 @@ Adds a single movie to the table. - var response = await client.PutItemAsync(request); - return response.HttpStatusCode == System.Net.HttpStatusCode.OK; + await _amazonDynamoDB.PutItemAsync(request); + return true; + } + catch (ResourceNotFoundException ex) + { + Console.WriteLine($"Table {tableName} was not found. {ex.Message}"); + return false; + } + catch (AmazonDynamoDBException ex) + { + Console.WriteLine($"An Amazon DynamoDB error occurred while putting item. {ex.Message}"); + throw; + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred while putting item. {ex.Message}"); + throw; + } @@ -356 +403,0 @@ Updates a single item in a table. - /// <param name="client">An initialized Amazon DynamoDB client object.</param> @@ -363,2 +410 @@ Updates a single item in a table. - public static async Task<bool> UpdateItemAsync( - AmazonDynamoDBClient client, + public async Task<bool> UpdateItemAsync( @@ -367,0 +414,2 @@ Updates a single item in a table. + { + try @@ -396,3 +444,18 @@ Updates a single item in a table. - var response = await client.UpdateItemAsync(request); - - return response.HttpStatusCode == System.Net.HttpStatusCode.OK;