skip to Main Content

I want to get item from DYnamodb table using C#

Here is my method

public static dynamic Dynamodb() {
            string region = "us-east-2";
            string tableName = "RetailTestData";
            var credentials = new BasicAWSCredentials(Environment.GetEnvironmentVariable("awsAccessKeyId"), Environment.GetEnvironmentVariable("awsSecretAccessKey"));
            var client = new AmazonDynamoDBClient(credentials, RegionEndpoint.GetBySystemName(region));
            var table = Table.LoadTable(client, tableName);
             var item=table.GetItem();
return item;
}

reference taken from
Get Data from a DynamoDB Table in JSON Format

but When I am trying to use the code I am getting an error
enter image description here

at the end I am looking to use item.ToJson() method as rest of my code uses the json methods

2

Answers


  1. This works for me:

    using Amazon.DynamoDBv2;
    using Amazon.DynamoDBv2.DocumentModel;
    
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    
    Table table = Table.LoadTable(client, "test");
    string pk = "123";
    
    Document document = table.GetItem(pk);
    
    
    Login or Signup to reply.
  2. To get an item from Amazon DynamoDB, try using the AWS SDK for .NET V3 (which is the recommended version of the .NET SDK).

    You can use the GetItemAsync(). Notice that the call is an async method. If you are not familiar with AWS SDK For .NET V3 – here is the DEV Guide.

    What is the AWS SDK for .NET

    The C# code is:

    public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName)
            {
                var key = new Dictionary<string, AttributeValue>
                {
                    ["title"] = new AttributeValue { S = newMovie.Title },
                    ["year"] = new AttributeValue { N = newMovie.Year.ToString() },
                };
    
                var request = new GetItemRequest
                {
                    Key = key,
                    TableName = tableName,
                };
    
                var response = await client.GetItemAsync(request);
                return response.Item;
            }
    

    This example and other C# examples are located in the Official AWS Code Library.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search