简体   繁体   中英

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? ie 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.

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 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM