简体   繁体   中英

How to trigger lambda function using SNS?

Heyy,

I want to trigger my lambda function when i am uploading an image to S3 bucket. After successful upload on S3 i want to trigger my Lambda function on the callback. Can you please help me how to achieve that?

You need to set up a Lambda to trigger from an object creation event within S3, link here - https://docs.aws.amazon.com/AmazonS3/latest/user-guide/enable-event-notifications.html

You will also need to configure the correct IAM permission roles for your Lambda function and S3, a good link for that is here https://docs.amazonaws.cn/en_us/lambda/latest/dg/with-s3-example.html

Here is some code to get you started on the implementation -

    import urllib
    import boto3

    s3 = boto3.client('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')
            response = s3.get_object(Bucket=bucket, Key=key)

This will get the uploaded file, and store it in response. What you want to do from here, is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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