parm530
11/27/2019 - 1:53 PM

Masking an Amazon S3 Url

  • If an image is stored on amazon s3 or any file storage system and you want to hide the url that it generates:
  • Create a named route in your routes.rb file (a url that you send to your app, www.mysite.com/gimme-this-image)
    • get '/email-images/:id', to: 'controller_name#masked_url', as: :masked_url
    • In the controller's action #masked_url you will return the actual file by using send_data:
    # if using carrierwave
      image = EImage.find_by_id(params[:id])
      if image
          send_data open(image.email_image.url).read, filename: image.email_image.url, disposition: "inline" # attachment
      else
          raise ActionController::RoutingError.new("No Page Found")
      end
    
  • Basically, you are creating your own route to get this image or file, so by assigning it to a defined route, your request will go to the controller action to actually retrieve the file uing its amazon s3 url (through the use of carrierwave). In a way, you have masked the real file/image url!