GetItem.cs - AWS Code Sample

GetItem.cs

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX - License - Identifier: Apache - 2.0 using System; using System.Collections.Generic; using System.Configuration; using System.Threading.Tasks; using Amazon; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; namespace DynamoDBCRUD { public class GetItem { public static async Task<QueryResponse> GetItemAsync(IAmazonDynamoDB client, string table, string id) { var response = await client.QueryAsync(new QueryRequest { TableName = table, KeyConditionExpression = "ID = :v_Id", ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":v_Id", new AttributeValue { S = id } } } }); return response; } static void Main(string[] args) { var configfile = "app.config"; var region = ""; var table = ""; var id = ""; int i = 0; while (i < args.Length) { switch (args[i]) { case "-i": i++; id = args[i]; break; default: break; } i++; } if (id == "") { Console.WriteLine("You must supply an item ID (-i ID)"); return; } // Get default Region and table from config file var efm = new ExeConfigurationFileMap { ExeConfigFilename = configfile }; Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(efm, ConfigurationUserLevel.None); if (configuration.HasFile) { AppSettingsSection appSettings = configuration.AppSettings; region = appSettings.Settings["Region"].Value; table = appSettings.Settings["Table"].Value; if ((region == "") || (table == "")) { Console.WriteLine("You must specify Region and Table values in " + configfile); return; } } else { Console.WriteLine("Could not find " + configfile); return; } var newRegion = RegionEndpoint.GetBySystemName(region); IAmazonDynamoDB client = new AmazonDynamoDBClient(newRegion); Task<QueryResponse> response = GetItemAsync(client, table, id); foreach (var item in response.Result.Items) { foreach (string attr in item.Keys) { if (item[attr].S != null) { Console.WriteLine(attr + ": " + item[attr].S); } else if (item[attr].N != null) { Console.WriteLine(attr + ": " + item[attr].N.ToString()); } } Console.WriteLine(""); } } } }