【发布时间】:2021-09-17 14:47:46
【问题描述】:
我有一个字符串转义的奇怪问题:因为我试图查询的值是字节字符串,如果我通过 shell 运行命令,例如:
aws --endpoint-url=http://localhost:4566 \
dynamodb query \
--table-name user_storage \
--key-condition-expression "id = :v1" \
--expression-attribute-values '{":v1": {"S": "b\'id-value-here\'"}}'
它正确执行并打印输出:
{
"Items": [
{
"result": {
"B": "this is the result value"
},
"id": {
"S": "b'id-value-here'"
},
"timestamp": {
"N": "1630340264.7819724"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
但是,我正在尝试通过 python 的Invoke 任务运行程序来执行它。这是相同的命令,但我似乎无法正确地将字节字符串的编码传递给 shell。我尝试了许多编码和转义 id 值周围的内引号的组合,似乎没有什么与 shell 命令相同。 (注意:我使用的是 Python 3.6.9)
@task
def get_user(cmd, user_id):
c1 = f'''aws --endpoint-url=http://localhost:4566 \
dynamodb query \
--table-name user_storage \
--key-condition-expression "id = :v1" \
--expression-attribute-values '{{":v1": {{"S": "{user_id.encode()}"}}}}'
'''
cmd.run(c1, echo=True)
> invoke get-user id-value-here
aws --endpoint-url=http://localhost:4566 dynamodb query --table-name user_storage --key-condition-expression "id = :v1" --expression-attribute-values '{":v1": {"S": "b'id-value-here'"}}'
{
"Items": [],
"Count": 0,
"ScannedCount": 0,
"ConsumedCapacity": null
}
什么给了?一段时间以来,我一直在用头撞墙。似乎无法让它正确传递编码的 id 值。
【问题讨论】:
标签: python python-3.x aws-cli dynamodb-queries