当我的s3上传文件时,我想向所有cognito用户的电子邮件发送电子邮件。我用lambda创建了s3上传触发器。代码如下:
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
text = response["Body"].read().decode()
data = json.loads(text)
print(data)
transactions = data['transactions']
for record in transactions:
print(record['transType'])
return 'Success!'
except Exception as e:
print(e)
raise e
那我该怎么办?
import boto3
client = boto3.client('cognito-idp')
response = client.list_users(UserPoolId='')
print(response)
我尝试了这个代码,它在我的python id上工作,但它不工作我的lambda。你能帮我吗?
注)当我执行s3触发lambda代码时,我可以找到此消息。
"errorMessage": "'Records'",
"errorType": "KeyError",
"stackTrace": [
[
"/var/task/lambda_function.py",
2,
"lambda_handler",
"bucket = event['Records'][0]['s3']['bucket']['name']"
]
)}但是当我上传s3文件时,我可以在我的显示器上找到log和dot。
这就是获取认知用户列表的方法。
import json
import boto3
client = boto3.client('cognito-idp' , region_name='xx-xxx-x')
def print_users(in_UserPoolId):
users = []
list_users = client.list_users(UserPoolId= in_UserPoolId)
users = list_users['Users']
try:
PaginationToken = list_users['PaginationToken']
except:
PaginationToken = ''
while (PaginationToken != ''):
list_users = client.list_users(UserPoolId= in_UserPoolId, PaginationToken=PaginationToken)
for item in list_users['Users']:
users.append( item )
if 'PaginationToken' in list_users:
PaginationToken = list_users['PaginationToken']
else:
PaginationToken = ''
for user_rec in users:
user = user_rec['Username']
print(user)
def lambda_handler(event, context):
UserPoolId = 'xxxxxxxxxxxxxxx'
print_users(UserPoolId)