0

我创建了这个 Rails 应用程序来接受 SNS 主题通知数据。在完成弹性转码器作业时会生成 SNS 通知。因此,rails 应用程序只需接受来自 AWS SNS 服务的传入 post 请求,而我只需要解析这些数据。对我不起作用的是,我正在尝试检索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"
    }
  ]
}
4

2 回答 2

1

我遇到了同样的问题,我最终放弃了 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

结尾

于 2017-07-29T06:14:41.723 回答
0

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

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

      puts parsed_message.inspect

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

      puts parsed_message[:jobId]
于 2015-08-21T14:47:33.860 回答