Rails beginner
Rails beginner

Reputation: 14504

Rails - Is it possible to give external image a local url?

Because it is not possible to uploade files to a Heroku app.

I am using a Amazon S3 storage server for images.

The problem is just that the image url is to the Amazon S3 server.

I would like it to be mydomain.com/myimage.png instead of s3.amazon.com/bucket/myimage.png

How is it possible to show a image on a amazon s3 server when visiting example: mydomain.com/myimage.png ?

Upvotes: 4

Views: 1577

Answers (2)

Rails beginner
Rails beginner

Reputation: 14504

My solution, I only use png images:

In routes:

match "/images/vind/:style/:id/:basename.png" => "public#image_proxy"

In public controller:

def image_proxy
  image_url = "http://s3-eu-west-1.amazonaws.com/konkurrencerher#{request.path}"
  response.headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
  response.headers['Content-Type'] = 'image/png'
  response.headers['Content-Disposition'] = 'inline'
  render :text => open(image_url, "rb").read,
end

Upvotes: 7

Leventix
Leventix

Reputation: 3859

You can create a rack middleware that will look for a pattern in the url (/images/* or *.png) and when there's a match, it will act as a proxy, requesting the image from S3 and serving the content that it receives.

Make sure you set the caching headers right, so that Heroku's reverse proxy will cache it and serve it quickly.

Upvotes: 3

Related Questions