我正在编写一个 shell 脚本来查询 dynamoDB 表中的一个属性。我正在使用 AWS CLI 编写脚本。
我想从 MY_TABLE_NAME 中找到 ReferenceId gfsdgrhfsh 的 AccountId。当我在 AttributeValueList 中提供属性的准确值时,查询操作成功,我得到了正确的属性值。
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": "gfsdgrhfsh" } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'
上述命令为我提供了正确的帐户 ID。
但是,当我将 ReferenceId gfsdgrhfsh 分配给一个变量,然后将此变量放入命令中时,我没有收到响应。
referenceId=gfsdgrhfsh
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": "$referenceId" } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'
有人可以建议如何在命令中注入(inject)变量的值。我必须通过从文件中读取大量 referenceIds 来执行查询操作,因此我需要在命令中注入(inject)变量值。
如有任何帮助,我们将不胜感激。
最佳答案
想通了,我们需要传递用双引号括起来的值。以下命令有效:
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": '\"$referenceId\"' } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'
关于amazon-web-services - 在 AWS CLI DynamoDB 查询操作中替换变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809068/