简体   繁体   中英

How to Receive Push Notifications from AWS S3 to Rails App?

Disclaimer, I'm a Rails newb. So I may be doing this wrong altogether.

I need to display the latest image in the view of a rails app immediately after it is uploaded to an AWS S3 bucket (from another source). Rather than repeatedly updating / polling for latest image, I think it'd be less taxing and costly to get a notification from AWS when a new image has been uploaded.

I looked into SNS and it seems like perhaps an HTTP notification with the rails url as endpoint could be an option. But I'm not sure how to set that up.

Any ideas or suggestions?

  1. Create a Rails endpoint that can accept the SNS notifications.
  2. Enable S3 event notifications to SNS
  3. Configure the SNS topic that is receiving the S3 events to push to your Rails application's endpoint.

Figured out an approach that works, not sure if it's the most optimal or if this follows proper rails standards, but it worked and is relatively simple to implement. Therefore, I would like to spell out the process for anyone looking to do the same, which is to subscribe a Rails app to the Amazon SNS push notifications.

First, you need to set up the AWS side of this configuration, so follow the instructions from Mark B, the first answer to this question. You'll find that the AWS documentation instructs you to have your app ready to handle HTTP Post requests before setting up the SNS subscription. That said, the following is what you can do on the Rails side of the setup to get everything configured

In order to receive the HTTP post request, you need to setup a simple API. Which you can do by creating a controller and custom routes to handle get/post requests from outside sources. Since Amazon only sends post requests, you only need one controller action and one route for this. Start with a new controller and store the controller file in apps/controllers/api/v1/ , with format:

class Api::V1::ControllerNameController < ApplicationController
  def create

  end 
end

Then set up your routes config/routes.rb :

 namespace :api, defaults: {format: :json}  do
    namespace :v1 do
      post "/controller_name" => 'controller_name#create'
    end
  end

You'll notice the controller file location and namespace calls to create a URL with format /api/v1/ , this is done for good practice, which this article has a good explanation as to why in section 4.1.3

In AWS, you'll then set your endpoint for the subscription of the notifications to be https://your-site-url/api/v1/controller_name which matches the URL for a post request.

Next, you're ready to receive the Post request from AWS. The important thing I discovered is that the incoming post request comes is accessible by the variable request . In order to convert this into readable JSON format, you can call the following:

request.body.read()

Which you can put inside the create action of the new controller. This you can print out to your console with something like the following:

@request = request.body.read()
puts "this is the content of the request: #{@request}"

From the output in your console you can grab the "SubscribeURL" inside this JSON object, copy and paste it into a web browser to confirm your subscription. And boom, that's half the battle!

Also, this new variable can be used to store your JSON object and to access all the pertinent information from AWS and do what you need to do with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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