Nicholas Haley
Nicholas Haley

Reputation: 4014

Best Way to store Scraped Images on S3

To give a little context, I am currently building a recipe app using a Rails API and Ember frontend. To get recipes, I am scraping certain websites and storing the information in a PG database.

Because there are so many images I would like to start storing those on S3. In the past I have used Paperclip to handle image uploads to S3, but since my rails app is just an API I'm not really sure what the best approach/tools are. Has anyone done something similar?

Upvotes: 0

Views: 80

Answers (1)

Sean Huber
Sean Huber

Reputation: 3985

If your S3 bucket is publicly accessible (this is ideal and performant), just use the S3 urls for your images instead of going through Rails.

If you have to go through Rails because you want to keep your S3 bucket private (this approach is more costly to scale), you can retrieve S3 objects in a controller action and serve them that way. This can be done with the send_data method: http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

Rough example:

def MediaController < ApplicationController
  def s3_image
    bucket_get_result = G_CLIENT.execute(
      api_method: G_API.objects.get,
      parameters: {bucket: 'my_bucket', object: params.require(:object_name), alt: 'media'}
    )
    send_data bucket_get_result.body, :disposition => 'inline'
  end
end

Upvotes: 1

Related Questions