Amazon.DynamoDBv2.DocumentModel.Table.TryLoadTable C# (CSharp) Method

TryLoadTable() public static method

Creates a Table object with the specified name, using the passed-in client to load the table definition. This method will return false if the table does not exist.
public static TryLoadTable ( IAmazonDynamoDB ddbClient, string tableName, DynamoDBEntryConversion conversion, Table &table ) : bool
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 Table Loaded table.
return bool
        public static bool TryLoadTable(IAmazonDynamoDB ddbClient, string tableName, DynamoDBEntryConversion conversion, out Table table)
        {
            try
            {
                table = LoadTable(ddbClient, tableName, conversion);
                return true;
            }
            catch
            {
                table = null;
                return false;
            }
        }
#endif

Same methods

Table::TryLoadTable ( IAmazonDynamoDB ddbClient, string tableName, Table &table ) : bool
Table::TryLoadTable ( IAmazonDynamoDB ddbClient, string tableName, Table consumer, DynamoDBEntryConversion conversion, Table &table ) : bool

Usage Example

        /// <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;
            }
        }