boto3,python:将值附加到 DynamoDB 字符串集

boto3, python: appending value to DynamoDB String Set

我在 DynamoDB 中有一个对象:

{ 'UserID' : 'Hank', ConnectionList : {'con1', 'con2'} }

通过在 lambda 函数中使用 boto3,我想将 'con3' 添加到字符串集中。 到目前为止,我一直在尝试使用以下代码但没有成功:

ddbClient = boto3.resource('dynamodb')
table = ddbClient.Table("UserInfo")
table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "SET ConnectionList = list_append(ConnectionList, :i)",
    ExpressionAttributeValues = {
        ":i": { "S": "Something" }
    },
    ReturnValues="ALL_NEW"
)

但是,无论我尝试将信息放入 String Set 中的方式如何,它总是运行错误。

由于您正在使用资源 API,因此您必须在语句中使用 Python 数据类型 set

table.update_item(
    Key={
        "UserId" : 'Hank'
    },
    UpdateExpression = 
        "ADD ConnectionList :i",
    ExpressionAttributeValues = {
        ":i": {"Something"},  # needs to be a set type
    },
    ReturnValues="ALL_NEW"
)