1/26/2021

AWS dynamoDB, search item by condition in 3 ways

 

Most secure way is 3rd, first and second way can fail when items are many.


code start

#pre define
UID_common = '24dea8d41ef74efda24a4b37805fe6de'

#### way 1 ###
dynamodb_resource = boto3.resource('dynamodb')
docs_table = dynamodb_resource.Table(DOCUMENTS_TABLE_NAME)
resp = docs_table.scan(FilterExpression = Attr('UID_COMMON').eq(UID_common))
for item in resp['Items']:
print( item['UID'] )

#### pre-define ###
dynamo_client = boto3.client('dynamodb',
aws_access_key_id = ACCESS_KEY,
aws_secret_access_key = SECRET_KEY,
region_name = REGION
)

#### way 2 ###
response = dynamo_client.scan(
TableName = DOCUMENTS_TABLE_NAME,
ExpressionAttributeValues={':search':{'S': UID_common} },
FilterExpression = "UID_COMMON = :search"
)
for item in response['Items']:
print(item['UID']['S'] )

### way 3 ###
paginator = dynamo_client.get_paginator('scan')
operation_parameters = {
'TableName': DOCUMENTS_TABLE_NAME,
'FilterExpression': 'UID_COMMON = :search',
'ExpressionAttributeValues': {
':search': {'S': UID_common},
}
}

page_iterator = paginator.paginate(**operation_parameters)
for page in page_iterator:
for item in page['Items']:
print( item['UID']['S'] )

code end


Enjoy AWS!

No comments:

Post a Comment