Amazon SNS(简单通知服务)Perl 库

发布于 2024-12-13 12:40:46 字数 186 浏览 1 评论 0原文

我想开始使用 Amazon Simple notification Service,但我还没有找到任何可以使用的 Perl 库用于访问服务。我宁愿不创建自己的库,我想看看是否有人使用过用于 SNS 服务的 Perl 库,以及他们是否会推荐任何库。

I wanted to start using the Amazon Simple Notification Service, but I have not found any Perl libraries that I can use to access the service. I would rather not create my own library, I wanted to see if anybody has used any Perl libraries for the SNS service, and if they would recommend any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万人眼中万个我 2024-12-20 12:40:46

Amazon::SNS 存在。这些文档非常稀疏,但看起来它提供了基础知识,并且代码质量对我来说看起来很好。

Amazon::SNS exists. The docs are pretty sparse but it looks like it does the basics, and the code quality looks fine to me.

又爬满兰若 2024-12-20 12:40:46

我将 Net::Amazon::AWSSign 与一个小脚本结合使用:

#!/usr/bin/perl

use Net::Amazon::AWSSign;

$ACCESS_KEY_ID="<my key id>";
$SECRET_KEY="<my secret key>";
$TOPIC_ARN='<my topic arn>';
$TOPIC_ARN =~ s/:/%3A/g;
$MESSAGE="This is a test";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$year += 1900;
$mon+=1;

$timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
        $year,$mon,$mday,$hour,$min,$sec);
$timestamp =~ s/:/%3A/g;

$REQUEST="http://sns.us-east-1.amazonaws.com/".
"?TopicArn=$TOPIC_ARN".
"&Message=$MESSAGE".
"&Action=Publish".
"&SignatureVersion=2".
"&SignatureMethod=HmacSHA256".
"&Timestamp=$timestamp".
"&AWSAccessKeyId=$ACCESS_KEY_ID";

my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");

$signed = $awsSign->addRESTSecret($REQUEST);

$res = `curl -s -o- '$signed'`;
if ($res =~ /<error>/) {
        print "ERROR!\n";
        return 1;
}

0;

我实际上最终使用了 XML::Simple,并将结果从 Curl 传递到 XMLIn,以解析 Amazon 返回的 XML。做你想做的事...

I used Net::Amazon::AWSSign in conjunction with a tiny script:

#!/usr/bin/perl

use Net::Amazon::AWSSign;

$ACCESS_KEY_ID="<my key id>";
$SECRET_KEY="<my secret key>";
$TOPIC_ARN='<my topic arn>';
$TOPIC_ARN =~ s/:/%3A/g;
$MESSAGE="This is a test";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$year += 1900;
$mon+=1;

$timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
        $year,$mon,$mday,$hour,$min,$sec);
$timestamp =~ s/:/%3A/g;

$REQUEST="http://sns.us-east-1.amazonaws.com/".
"?TopicArn=$TOPIC_ARN".
"&Message=$MESSAGE".
"&Action=Publish".
"&SignatureVersion=2".
"&SignatureMethod=HmacSHA256".
"&Timestamp=$timestamp".
"&AWSAccessKeyId=$ACCESS_KEY_ID";

my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");

$signed = $awsSign->addRESTSecret($REQUEST);

$res = `curl -s -o- '$signed'`;
if ($res =~ /<error>/) {
        print "ERROR!\n";
        return 1;
}

0;

I actually used XML::Simple in the end, and passed the result from Curl to XMLIn, to parse the XML that Amazon returns. Do what you will...

静赏你的温柔 2024-12-20 12:40:46

我以布拉德的为起点,谢谢布拉德!
我将当地时间更改为 gmtime。
我也没有使用主题,而是使用目标 ARN 并使用基于角色的身份验证。
我必须传递 SecurityToken 才能使其工作,并且当我将消息放入 GCM json 包装器中时,该消息仅适用于 Android 推送。
在代码中,我使用 TargetARN 中的应用程序名称来检测平台并相应地调整有效负载。
注意:Windows 代码未经测试。

最后一项值得注意的是 SNS 似乎需要的疯狂嵌套 json 编码。

    sub send_sns
{
# required arguments: endpoint (AWS SNS endpoint), message
        my $args = shift;
        my $TargetArn=encode_url($args->{endpoint});
        my $message=$args->{message};
        my $data = {};
        my $json = JSON->new->utf8->allow_nonref;
        if ($args->{endpoint} =~ /GCM\/[a-z]+_android\//) {
#               Android
                $data->{data}{message}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"GCM": '.$json->encode( $dataString ).'}';
        } elsif ($args->{endpoint} =~ /APNS\/[a-z]+_apple_ios\//) {
#               iOS
                $data->{aps}{alert}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"APNS": '.$json->encode( $dataString ).'}';
        } elsif ($args->{endpoint} =~ /ADM\/[a-z]+_windows\//) {
#               windows (incomplete)
                $data->{data}{message}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"ADM": '.$json->encode( $dataString ).'}';
        }
        use Net::Amazon::AWSSign;
        my $credentials = qx[ curl -s --fail  http://169.254.169.254/latest/meta-data/iam/security-credentials/myrole ];
        my $credObj = decode_json($credentials);
        my $ACCESS_KEY_ID=$credObj->{AccessKeyId};
        my $SECRET_KEY=$credObj->{SecretAccessKey};
        my $token=$credObj->{Token};
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
        $year += 1900;
        $mon+=1;
        my $timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
                        $year,$mon,$mday,$hour,$min,$sec);
        $timestamp =~ s/:/%3A/g;
        my $REQUEST="http://sns.us-east-1.amazonaws.com/".
                "?TargetArn=$TargetArn".
                "&Message=$message".
                "&Action=Publish".
                "&SignatureVersion=2".
                "&SignatureMethod=HmacSHA256".
                "&Timestamp=$timestamp".
                "&SecurityToken=$token".
                "&MessageStructure=json".
                "&AWSAccessKeyId=$ACCESS_KEY_ID";
        my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");
        my $signed = $awsSign->addRESTSecret($REQUEST);
        $res = `curl -s -o- '$signed'`;
print "returns: $res\n" if -t;
        if ($res =~ /<error>/) {
                print "ERROR!\n";
                return 1;
        } else {
                return 0;
        }
}

I used Brad's as the starting point, thanks Brad!
I had the change the localtime to gmtime.
I was also not using topics but Target ARNs and using role based authentication.
I had to pass the SecurityToken to get it to work and the Message only worked for android push when I put it in a GCM json wrapper.
In the code I use my Application name in the TargetARN to detect the platform and adjust the payload accordingly.
Note: Windows code it untested.

One last item of note is the crazy nested json encoding that seems to be required of SNS.

    sub send_sns
{
# required arguments: endpoint (AWS SNS endpoint), message
        my $args = shift;
        my $TargetArn=encode_url($args->{endpoint});
        my $message=$args->{message};
        my $data = {};
        my $json = JSON->new->utf8->allow_nonref;
        if ($args->{endpoint} =~ /GCM\/[a-z]+_android\//) {
#               Android
                $data->{data}{message}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"GCM": '.$json->encode( $dataString ).'}';
        } elsif ($args->{endpoint} =~ /APNS\/[a-z]+_apple_ios\//) {
#               iOS
                $data->{aps}{alert}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"APNS": '.$json->encode( $dataString ).'}';
        } elsif ($args->{endpoint} =~ /ADM\/[a-z]+_windows\//) {
#               windows (incomplete)
                $data->{data}{message}=$args->{message};
                my $dataString = $json->encode($data);
                $message = '{"ADM": '.$json->encode( $dataString ).'}';
        }
        use Net::Amazon::AWSSign;
        my $credentials = qx[ curl -s --fail  http://169.254.169.254/latest/meta-data/iam/security-credentials/myrole ];
        my $credObj = decode_json($credentials);
        my $ACCESS_KEY_ID=$credObj->{AccessKeyId};
        my $SECRET_KEY=$credObj->{SecretAccessKey};
        my $token=$credObj->{Token};
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
        $year += 1900;
        $mon+=1;
        my $timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
                        $year,$mon,$mday,$hour,$min,$sec);
        $timestamp =~ s/:/%3A/g;
        my $REQUEST="http://sns.us-east-1.amazonaws.com/".
                "?TargetArn=$TargetArn".
                "&Message=$message".
                "&Action=Publish".
                "&SignatureVersion=2".
                "&SignatureMethod=HmacSHA256".
                "&Timestamp=$timestamp".
                "&SecurityToken=$token".
                "&MessageStructure=json".
                "&AWSAccessKeyId=$ACCESS_KEY_ID";
        my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");
        my $signed = $awsSign->addRESTSecret($REQUEST);
        $res = `curl -s -o- '$signed'`;
print "returns: $res\n" if -t;
        if ($res =~ /<error>/) {
                print "ERROR!\n";
                return 1;
        } else {
                return 0;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文