Reputation: 475
As I have configured multiple profile in .aws/credentials file. I need to read a file that contains a list of keys to be used as parameter in boto3 dynamodb query. So my code looks like this:
import boto3
from boto3.dynamodb.conditions import Key
def query_keys(dynamodb, bucketAndKey):
table = dynamodb.Table('media-import-address')
response = table.query(
KeyConditionExpression=Key('bucketAndKey').eq(bucketAndKey)
)
return response['Items']
if __name__ == '__main__':
file_name = input("Enter your file name: ")
dynamodb = boto3.Session(profile_name='prod-cn').resource('dynamodb')
with open(file_name) as f:
lines = f.readlines()
for line in lines:
result = query_keys(dynamodb, line)
print(result)
My input file is a plain text file which looks like this:
aaa/abc.jpg
aaa/bbb.png
abc/aa.gif
The running result looks this:
[]
[]
[{'seq': Decimal('2'), 'schemaVersion': Decimal('1'), 'media': 'MDAVD'}]
Why the previous 2 not get executed?
Upvotes: 0
Views: 169
Reputation: 238995
A probable reason is that your line
will include new line character (i.e. \n
). Thus it will search for bucketAndKey
with the \n
at the end.
To remove the \n
from the line, you can check the following:
result = query_keys(dynamodb, line.rstrip())
Upvotes: 1