![](/img/trans.png)
[英]Send username to aws Lambda function triggered by aws Cognito user confirm
[英]Send username to AWS Lambda function triggered when user is registered through AWS Cognito
我正在嘗試編寫一個 Lambda function 當新確認的 Cognito 用戶在 s3 存儲桶中創建一個文件夾。 這將允許我將該用戶的訪問權限限制在他們的文件夾中。 我創建了一個 Lambda function 可以列出當前在用戶池中注冊的用戶。 我知道 Cognito 有一個“確認事件”和“身份驗證后”事件觸發器,並且我選擇了我的 function 在該觸發器上運行。
但是當用戶通過該事件進行身份驗證或確認時,我不知道如何制作文件夾。 下面的屏幕截圖是我的 Lambda 代碼。
這是我的身份驗證后觸發器代碼,但它不起作用:
from __future__ import print_function
def lambda_handler(event, context):
# Send post authentication data to Cloudwatch logs
print ("Authentication successful")
print ("Trigger function =", event['triggerSource'])
print ("User pool = us-east-1_EVPcl4p64", event['userPoolId'])
print ("App client ID = ", event['callerContext']['clientId'])
print ("User ID = ", event['userName'])
# Return to Amazon Cognito
return event
這是列表用戶的代碼。 它可以工作,但現在如何僅獲取用戶名並在此基礎上在 s3 存儲桶中創建一個文件夾?
import json
import boto3
import re
def lambda_handler(event, context):
# TODO implement
client = boto3.client('cognito-idp')
response = client.list_users(
UserPoolId='us-east-1_EVPcl4p64',
AttributesToGet=['email']
)
x = response
print json.dumps(x)
print(y["email"])
pattern = '^@gmail.com$'
test_string = response
result = re.match(pattern, test_string)
print(response)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
請改用PostConfirmation
觸發器。 每次用戶登錄時,身份驗證后觸發器都會觸發。
以下是獲取 email 地址的方法:
email = event['request']['userAttributes']['email']
以下是使用 email 作為文件夾名稱為該用戶創建 S3“文件夾”的方法:
s3 = boto3.client('s3')
bucket_name = 'example-bucket-name'
directory_path = f"users/{email}/"
s3.put_object(Bucket=bucket_name, Key=directory_path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.