php小編小新為您介紹適用于Dynamodb的Golang SDK中的一個重要特性:ReturnValuesOnConditionCheckFailure。這個特性在條件檢查失敗時不會返回關(guān)于條件的詳細(xì)信息,從而保護(hù)了敏感數(shù)據(jù)。使用這個特性,開發(fā)者可以更加安全地處理條件檢查失敗的情況,并且提高應(yīng)用程序的可靠性。在本文中,我們將深入探討這個特性的用法和優(yōu)勢,幫助開發(fā)者更好地理解和應(yīng)用于實際項目中。
問題內(nèi)容
我正在使用 golang sdk https://pkg.go.dev/github.com/aws/[email?protected]/ 進(jìn)行調(diào)試條件檢查錯誤并找到有關(guān)單個寫入操作失敗的原因的信息,但我只能看到錯誤 Message_:“條件請求失敗”。在 UpdateItemInput 中使用參數(shù) ReturnValuesOnConditionCheckFailure: ALL_OLD 時,未提供有關(guān)具體原因的其他信息。對于 TransactWriteItems,使用相同參數(shù)時我可以看到條件檢查失敗的具體原因。如何獲取單個寫入操作的這些詳細(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ù)制
解決方法
該項目應(yīng)位于錯誤組件內(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,則此功能尚不存在