public static TryLoadTable ( IAmazonDynamoDB ddbClient, string tableName, DynamoDBEntryConversion conversion, |
||
ddbClient | IAmazonDynamoDB | Client to use to access DynamoDB. |
tableName | string | Name of the table. |
conversion | DynamoDBEntryConversion | Conversion to use for converting .NET values to DynamoDB values. |
table | Loaded table. | |
Résultat | bool |
/// <summary> /// Gets the DynamoDB table /// </summary> /// <param name="tableName"></param> /// <returns></returns> private async Task <DynamoDbTable> TableWithName(string tableName) { var hashKeyName = TableConfigProvider.GetTableHashKeyName(tableName); var rangeKeyName = TableConfigProvider.GetTableRangeKeyName(tableName); Logger.Debug("Checking if table '{0}' exists in DynamoDB...", tableName); try { if (!DynamoDbTable.TryLoadTable(DynamoDb, tableName, out var table)) { // if we failed to load the table, this is basically a retry that will throw an exception. // The expectation is that this will expose whatever error caused the TryLoadTable method // to return false, but if for some reason it happens to succed on retry, that also works. if (!TableConfigProvider.CreateIfNotExists) { return(DynamoDbTable.LoadTable(DynamoDb, tableName)); } //throw new Exception($"Table {tableName} does not exist in DynamoDB."); Logger.Info("Table '{0}' does not exist in DynamoDB. Creating it now...", tableName); var createResp = await DynamoDb.CreateTableAsync(tableName, new List <KeySchemaElement> { new KeySchemaElement { AttributeName = hashKeyName, KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = rangeKeyName, KeyType = KeyType.RANGE } }, new List <AttributeDefinition> { new AttributeDefinition { AttributeName = hashKeyName, AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = rangeKeyName, AttributeType = ScalarAttributeType.S } }, new ProvisionedThroughput(1, 1)); if (createResp.HttpStatusCode != HttpStatusCode.OK) { throw new Exception($"Failed to create table '{tableName}' in DynamoDB. Response code is {createResp.HttpStatusCode}."); } Logger.Info("Successfully created DynamoDB table '{0}'.", tableName); table = DynamoDbTable.LoadTable(DynamoDb, tableName); } return(table); } catch (Exception exception) { Logger.Error($"An error occurred loading the DynamoDB table for type {tableName}.", exception); throw; } }