繁体   English   中英

Rails / Ruby解析AWS SNS主题通知数据

[英]Rails/Ruby Parse AWS SNS Topic Notification Data

我创建了一个Rails应用程序来接受SNS主题通知数据。 在夏季,SNS通知在弹性代码转换器作业完成时生成。 因此,rails应用程序仅接受来自AWS SNS服务的传入发帖请求,而我需要解析所有数据。 对我不起作用的是,我正在尝试检索Message => jobId但我不知道如何。 感谢任何帮助。

def sns_transcode_completed

      amz_message_type = request.headers['x-amz-sns-message-type']
      amz_sns_topic = request.headers['x-amz-sns-topic-arn']

      if !amz_sns_topic.nil? &&
        amz_sns_topic.to_s.downcase == 'arn:aws:sns:us-east-1:XXXXXXXXXX:MyApp_transcode_completed'

        request_body = JSON.parse(request.body.read, {:symbolize_names => true})

        notification = Hashie::Mash.new(request_body)


        if amz_message_type.to_s.downcase == 'subscriptionconfirmation'
          subscribe_url = request_body['SubscribeURL']
          if !subscribe_url.to_s.empty? && !subscribe_url.nil?
            subscribe_confirm = HTTParty.get subscribe_url

            puts subscribe_confirm
            puts subscribe_url
          end


        end

        if amz_message_type.to_s.downcase == 'notification'

          puts "--------------------------"
          puts notification.inspect # See output 1
          puts "--------------------------"
          puts notification.MessageId # This works I can get the MessageId 
          puts "--------------------------"
          puts notification.Message => # From here I need to get the jobId, but it comes as a String? See output 2
          puts "--------------------------"



        end

      end
      render :nothing => true, :status => 200, :content_type => 'text/html'


    end

输出1

          #<Hashie::Mash Message="{\n \"state\" : \"COMPLETED\",\n \"version\" : \"2012-09-25\",\n \"jobId\" : \"1440122777052-XXXXXX\",\n \"pipelineId\" :
   \"1432361831290-XXXXX\",\n \"input\" : {\n \"key\" : \"web-b796ab20-297c-0133-4ccf-378cf690e3b1.mp3\"\n },\n \"outputKeyPrefix\" :
   \"hlsv4/246-21632840-29d7-0133-1d8e-XXXXXXXX/\",\n \"outputs\" : [ {\n \"id\" : \"1\",\n \"presetId\" : \"1351620000001-200071\",\n \"key\" : \"hls_64k\",\n
   \"segmentDuration\" : 10.0,\n \"status\" : \"Complete\",\n \"duration\" : 60\n } ],\n \"playlists\" : [ {\n \"name\" : \"index\",\n \"format\" : \"HLSv4\",\n
   \"outputKeys\" : [ \"hls_64k\" ],\n \"status\" : \"Complete\"\n } ]\n}" MessageId="2d26f6d0-1715-5edb-af39-b8809eff521f" Signature="XXXXXX" SignatureVersion="1"
   SigningCertURL="https://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11fXXXXXXX.pem" Subject="Amazon Elastic Transcoder has
   finished transcoding job 1440122777052-XXXXXX." Timestamp="2015-08-21T02:06:34.523Z" TopicArn="arn:aws:sns:us-east-1:005550634774:MyApp_transcode_completed"
   Type="Notification"
   UnsubscribeURL="https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:005550634774:MyApp_transcode_completed:8c0e48fe-c34
   9-4185- bb94-XXXXXXXXXX">

输出2

 {
  "state": "COMPLETED",
  "version": "2012-09-25",
  "jobId": "1440122777052-XXXXX",
  "pipelineId": "1432361831290-XXXXXX",
  "input": {
    "key": "web-b796ab20-297c-0133-4ccf-378cf690e3b1.mp3"
  },
  "outputKeyPrefix": "hlsv4/246-21632840-29d7-0133-1d8e-35ebbdecd855/",
  "outputs": [
    {
      "id": "1",
      "presetId": "1351620000001-200071",
      "key": "hls_64k",
      "segmentDuration": 10,
      "status": "Complete",
      "duration": 60
    }
  ],
  "playlists": [
    {
      "name": "index",
      "format": "HLSv4",
      "outputKeys": [
        "hls_64k"
      ],
      "status": "Complete"
    }
  ]
}

我再次解析了notification.Message值。 我希望有比这更好的方法。

parsed_message = JSON.parse(notification.Message, {:symbolize_names => true})

      puts parsed_message.inspect

      puts "--------------------------"

      puts parsed_message[:jobId]

我遇到了同样的问题,我最终放弃了Hashie。(在这种情况下,除了可以使用点表示法之外,实际上并不需要。)我仍然必须进行双重解析(由于在提取消息时返回了字符串) )。 一旦抓住了它,我想获取的对象就在一个数组中,因此我使用了dig方法来拉出对象,然后访问所需的数据。

def sns_endpoint
notification = JSON.parse(request.raw_post, {:symbolize_names => true})
case notification[:Type]
  when "SubscriptionConfirmation"
    confirm(notification[:TopicArn], notification[:Token])
  when "Notification"
    message_data = JSON.parse(notification[:Message])
    thing_you_are_trying_to_get = message_data["Records"].dig(0)["s3"]...etc...(access the data you need here.)
     else
    Rails.logger.error "Unknown notification type #{notification[:Type]}"
end

render body: nil

结束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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