我创建了这个 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"
}
]
}