为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

米脂 2021-04-05 12:47:33
我对SNS和Lambda相当陌生。我已经成功创建了一个SNS主题,并且能够发送文本消息。上传文件时,我确实设置了S3事件。但是,我想更改该消息的文本,因此从应该向SNS主题发送消息的蓝图创建了Lambda函数。这是设计器的屏幕截图这是我正在使用的蓝图代码:from __future__ import print_functionimport jsonimport urllibimport boto3print('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.')
查看完整描述

2 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

我发现一个有效的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!');  

    });

};


查看完整回答
反对 回复 2021-04-13
  • 2 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号