'AWS CLI - Put contents of a file in a single attribute value within a DynamoDB table

I have some C# code that successfully puts contents of a text file into a DynamoDB table attribute value:

var content = File.ReadAllText("file.txt");
var dynamoDB = new AmazonDynamoDBClient(region);
var request = new PutItemRequest
{
  TableName = "mytable",
  Item = new Dictionary<string, AttributeValue>() { 
    { "mykey", new AttributeValue { S = "file.txt" } },
    { "content", new AttributeValue { S = content } },
  },
};
var response = await dynamoDB.PutItemAsync(request);

This works as expected, and the contents of file.txt ends up in the table.

How do I do this using AWS CLI? i.e. how to I put the contents of a file into a single attribute value within a DynamoDB table, using AWS CLI?

aws dynamodb put-item --item is no good because that expects the contents of the file to be json records.



Solution 1:[1]

read the file into a variable and save as a json file

 mykey="file.txt"
 content=$(cat out.txt)
 jq -n \
    --arg key "$mykey" \
    --arg value "$content"  \
    '{mykey: $key, content: $value}' > item.json

then use the aws command e.g.

 aws dynamodb put-item \
    --table-name MyTable \
    --item file://item.json

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt