猿问

AWS Lambda函数在S3上传上触发SNS主题

我对SNS和Lambda相当陌生。我已经成功创建了一个SNS主题,并且能够发送文本消息。上传文件时,我确实设置了S3事件。但是,我想更改该消息的文本,因此从应该向SNS主题发送消息的蓝图创建了Lambda函数。

这是设计器的屏幕截图

这是我正在使用的蓝图代码:


from __future__ import print_function


import json

import urllib

import boto3


print('Loading message function...')



    def send_to_sns(message, context):

    

        # This function receives JSON input with three fields: the ARN of an SNS topic,

        # a string with the subject of the message, and a string with the body of the message.

        # The message is then sent to the SNS topic.

        #

        # Example:

        #   {

        #       "topic": "arn:aws:sns:REGION:123456789012:MySNSTopic",

        #       "subject": "This is the subject of the message.",

        #       "message": "This is the body of the message."

        #   }

    

        sns = boto3.client('sns')

        sns.publish(

            TopicArn=message['arn:aws:sns:MySNS_ARN'],

            Subject=message['File upload'],

            Message=message['Files uploaded successfully']

        )

    

        return ('Sent a message to an Amazon SNS topic.')


米脂
浏览 234回答 2
2回答

繁星点点滴滴

我发现一个有效的NodeJs示例:console.log('Loading function');var AWS = require('aws-sdk');  AWS.config.region = 'us-east-1';exports.handler = function(event, context) {      console.log("\n\nLoading handler\n\n");    var sns = new AWS.SNS();    sns.publish({        Message: 'File(s) uploaded successfully',        TopicArn: 'arn:aws:sns:_my_ARN'    }, function(err, data) {        if (err) {            console.log(err.stack);            return;        }        console.log('push sent');        console.log(data);        context.done(null, 'Function Finished!');      });};
随时随地看视频慕课网APP

相关分类

Python
我要回答