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