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

时间:2011-11-04 20:27:28

标签: perl amazon-sns

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我使用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。做你想做的......

答案 2 :(得分:0)

我用布拉德作为起点,谢谢布拉德! 我把当地时间改为gmtime。 我也没有使用主题,而是使用目标ARN并使用基于角色的身份验证。 我必须通过SecurityToken才能使它工作,当我把它放在GCM json包装器中时,Message只适用于android push。 在代码中,我在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;
        }
}