AWS Security ChangesHomeSearch

AWS amazondynamodb documentation change

Service: amazondynamodb · 2025-07-16 · Documentation low

File: amazondynamodb/latest/developerguide/example_dynamodb_Scenario_GettingStartedMovies_section.md

Summary

Updated .NET SDK example code from v3 to v4 with dependency injection, error handling improvements, code structure changes, and GitHub link updates

Security assessment

Changes focus on SDK version migration (v3 to v4), code quality improvements (dependency injection, exception handling), and documentation links. No explicit security vulnerabilities addressed or security features added. Error handling improvements enhance reliability but don't directly mitigate known security risks.

Diff

diff --git a/amazondynamodb/latest/developerguide/example_dynamodb_Scenario_GettingStartedMovies_section.md b/amazondynamodb/latest/developerguide/example_dynamodb_Scenario_GettingStartedMovies_section.md
index d5f6e426c..b9d3a5440 100644
--- a//amazondynamodb/latest/developerguide/example_dynamodb_Scenario_GettingStartedMovies_section.md
+++ b//amazondynamodb/latest/developerguide/example_dynamodb_Scenario_GettingStartedMovies_section.md
@@ -27 +27 @@ The following code examples show how to:
-**SDK for .NET**
+**SDK for .NET (v4)**
@@ -32 +32 @@ 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). 
@@ -35,17 +35,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
@@ -52,0 +50,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        public static bool IsInteractive = true;
+    
@@ -56 +55,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)
@@ -58 +62,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>();
@@ -62,2 +74 @@ 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";
@@ -70 +81 @@ 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);
@@ -72,8 +83,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}.");
@@ -90 +96 @@ 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);
@@ -112 +118 @@ 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);
@@ -126 +132 @@ 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);
@@ -139,2 +145,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)
@@ -142 +148 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                DynamoDbMethods.DisplayItem(item);
+                dynamoDbWrapper.DisplayItem(item);
@@ -158 +164 @@ 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);
@@ -174 +180 @@ 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);
@@ -182 +188 @@ 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);
@@ -188 +194 @@ 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);
@@ -199 +205 @@ 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.");
@@ -208,0 +215,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+            if (!IsInteractive)
+            {
+                return;
+            }
+    
@@ -232,0 +244,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        {
+            if (IsInteractive)
@@ -238,0 +252 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    }
@@ -250 +263,0 @@ Creates a table to contain movie data.
-            /// <param name="client">An initialized Amazon DynamoDB client object.</param>
@@ -253 +266,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
@@ -255 +270 @@ Creates a table to contain movie data.
-                var response = await client.CreateTableAsync(new CreateTableRequest
+                var response = await _amazonDynamoDB.CreateTableAsync(new CreateTableRequest
@@ -301 +316 @@ Creates a table to contain movie data.
-                    System.Threading.Thread.Sleep(sleepDuration);
+                    Thread.Sleep(sleepDuration);
@@ -303 +318 @@ Creates a table to contain movie data.
-                    var describeTableResponse = await client.DescribeTableAsync(request);
+                    var describeTableResponse = await _amazonDynamoDB.DescribeTableAsync(request);
@@ -311,0 +327,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;
+            }
+        }
@@ -322 +352,0 @@ Adds a single movie to the table.
-            /// <param name="client">An initialized Amazon DynamoDB client object.</param>
@@ -327 +357,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
@@ -341,2 +373,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;
+            }
@@ -354 +401,0 @@ Updates a single item in a table.
-            /// <param name="client">An initialized Amazon DynamoDB client object.</param>
@@ -361,2 +408 @@ Updates a single item in a table.
-            public static async Task<bool> UpdateItemAsync(
-                AmazonDynamoDBClient client,
+        public async Task<bool> UpdateItemAsync(
@@ -365,0 +412,2 @@ Updates a single item in a table.
+        {
+            try
@@ -394,3 +442,18 @@ Updates a single item in a table.
-                var response = await client.UpdateItemAsync(request);
-    
-                return response.HttpStatusCode == System.Net.HttpStatusCode.OK;