php小編小新為您介紹適用于Dynamodb的Golang SDK中的一個(gè)重要特性:ReturnValuesOnConditionCheckFailure。這個(gè)特性在條件檢查失敗時(shí)不會(huì)返回關(guān)于條件的詳細(xì)信息,從而保護(hù)了敏感數(shù)據(jù)。使用這個(gè)特性,開發(fā)者可以更加安全地處理?xiàng)l件檢查失敗的情況,并且提高應(yīng)用程序的可靠性。在本文中,我們將深入探討這個(gè)特性的用法和優(yōu)勢(shì),幫助開發(fā)者更好地理解和應(yīng)用于實(shí)際項(xiàng)目中。
問題內(nèi)容
我正在使用 golang sdk https://pkg.go.dev/github.com/aws/[email?protected]/ 進(jìn)行調(diào)試條件檢查錯(cuò)誤并找到有關(guān)單個(gè)寫入操作失敗的原因的信息,但我只能看到錯(cuò)誤 Message_:“條件請(qǐng)求失敗”。在 UpdateItemInput 中使用參數(shù) ReturnValuesOnConditionCheckFailure: ALL_OLD 時(shí),未提供有關(guān)具體原因的其他信息。對(duì)于 TransactWriteItems,使用相同參數(shù)時(shí)我可以看到條件檢查失敗的具體原因。如何獲取單個(gè)寫入操作的這些詳細(xì)信息?參考:https://aws.amazon.com/about-aws/whats-new/2023/06/amazon-dynamodb-cost-failed-conditional-writes 我正在使用的語法:
input := &dynamodb.UpdateItemInput{ TableName: aws.String("DummyTable"), Key: keyAttr, ExpressionAttributeValues: updateExpr.Values(), ExpressionAttributeNames: updateExpr.Names(), ConditionExpression: updateExpr.Condition(), ReturnValues: aws.String(dynamodb.ReturnValueAllOld), UpdateExpression: updateExpr.Update(), ReturnValuesOnConditionCheckFailure: aws.String(dynamodb.ReturnValuesOnConditionCheckFailureAllOld), } output, err := dl.ddbI.UpdateItem(input)
登錄后復(fù)制
解決方法
該項(xiàng)目應(yīng)位于錯(cuò)誤組件內(nèi),通常位于 error.response.Item
中。
例如在Python中:
except botocore.exceptions.ClientError as error: if error.response["Error"]["Code"] == "ConditionalCheckFailedException": print("The conditional expression is not met") current_value = error.response.get("Item")
登錄后復(fù)制
注意:如果您使用的是 DynamoDB Local,則此功能尚不存在