简体   繁体   English

使用 boto3 获取特定用户 ID

[英]Using boto3 to get specific user ID

I am running a script which essentially will look for the user's "invitation_id" in my dynamo db table.我正在运行一个脚本,它基本上会在我的 dynamo db 表中查找用户的“invitation_id”。 I am essentially writing a try and catch block where I want it to spit out an error if the invitation_id does not exist.我实际上是在写一个 try and catch 块,如果invitation_id 不存在,我希望它吐出一个错误。 It seems to not spit out ValueError when it should be.它似乎没有在应该吐出 ValueError 的时候吐出。 Am not sure whether what am doing is correct or not.我不确定我在做什么是否正确。 My script is as follows:我的脚本如下:

@staticmethod
 def search_delegate():

    CLIENT.get_item(
                    TableName="invtable",
                    Key={
                        'invitation_id': {
                            'S': '123'
                        }
                    }
                )


if __name__ == '__main__':

try:
    AWSDynamoUtils.search_delegate()
    print("Found user")
except:
    raise ValueError

From boto3 documentation:从 boto3 文档:

The GetItem operation returns a set of attributes for the item with the given primary key. GetItem 操作为具有给定主键的项目返回一组属性。 If there is no matching item, GetItem does not return any data and there will be no Item element in the response.如果没有匹配项,GetItem 不返回任何数据,响应中也不会有 Item 元素。

Since get_item does not return anything or does not raise any exceptions if invitation id is not in the table, your try statement executes successfully.由于 get_item 不会返回任何内容,或者如果邀请 id 不在表中,也不会引发任何异常,因此您的 try 语句将成功执行。 Try implementing a check to see if if there are any records returned otherwise raise exception.尝试执行检查以查看是否有任何记录返回,否则会引发异常。

entry = AWSDynamoUtils.search_delegate()
if entry:
    print('Found User')
else:
    raise ValueError

As per boto3 documentation for dynamodb GetItem operation:根据 dynamodb GetItem 操作的 boto3 文档:

The GetItem operation returns a set of attributes for the item with the given primary key. GetItem 操作为具有给定主键的项目返回一组属性。 If there is no matching item, GetItem does not return any data and there will be no Item element in the response.如果没有匹配项,GetItem 不返回任何数据,响应中也不会有 Item 元素。

Thus there won't be any exception raised in case there is no matching invitation_id.因此,如果没有匹配的invitation_id,则不会引发任何异常。 However, the "Item" element will not be present in the response.但是,“Item”元素不会出现在响应中。 You can check on the response and raise the "Value Error" in case Item element is not present.如果 Item 元素不存在,您可以检查响应并引发“Value Error”。

@staticmethod
 def search_delegate():

    return CLIENT.get_item(
                    TableName="invtable",
                    Key={
                        'invitation_id': {
                            'S': '123'
                        }
                    }
                )


if __name__ == '__main__':

response = AWSDynamoUtils.search_delegate()
if "Item" in response:
    print('Found User')
else:
    raise ValueError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM