CreateTable.cs - AWS Code Sample

CreateTable.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 CreateTable { public static async Task<CreateTableResponse> MakeTableAsync(IAmazonDynamoDB client, string table) { var response = await client.CreateTableAsync(new CreateTableRequest { TableName = table, AttributeDefinitions = new List<AttributeDefinition> { new AttributeDefinition { AttributeName = "ID", AttributeType = "S" }, new AttributeDefinition { AttributeName = "Area", AttributeType = "S" } }, KeySchema = new List<KeySchemaElement> { new KeySchemaElement { AttributeName = "ID", KeyType = "HASH" }, new KeySchemaElement { AttributeName = "Area", KeyType = "RANGE" } }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 10, WriteCapacityUnits = 5 } }); return response; } static void Main() { var configfile = "app.config"; var region = ""; var table = ""; // 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); var response = MakeTableAsync(client, table); Console.WriteLine("Created table " + response.Result.TableDescription.TableName + " in region " + region); } } }