Search code examples
ruby-on-railsrubyamazon-s3carrierwavefog

Download file from Amazon S3 through Carrierwave and Fog


I have a simple app in Rails which allows me to upload file to Amazon S3 by using Carrierwave and fog. It works fine. I'm now working on downloading the files and display them on the browser. It works. However, the private link (which I set it to be a temporary link) from S3 is shown on browser, like:

https://xxx.s3-ap-southeast-1.amazonaws.com/uploads/resume/attachment/4/xxx.pdf?X-Amz-Expires=100&X-Amz-Date=20150404T082207Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXX/20150404/ap-southeast-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=b72ac956fcd52ba887d9e4567d9e512207ba17dc4e5876bfc8c9d16b2db2367d

I don't want to expose the private link from S3. Instead, I wish the link like below is shown: www.xxx.com/user/1/resume/1

I have a model resume.rb

class Resume < ActiveRecord::Base
  mount_uploader :attachment, ResumeUploader
end

Controller with show action

def show
  @resume = Resume.find(params[:id])
  @resume_name = @resume.attachment.path.split("/").last
end

And my view: show.html.erb

<%= link_to @resume_name,  @resume.attachment.url  %>

I'm new to the ROR. Could anyone please tell me how can I show the file on the browser without showing the private url from S3?


Solution

  • You can route your images through CloudFront. Cloud Front has many advantages but it does comes at a cost. Once you route your images through cloud front you get a domain kind of link for your assets same as s3 does, but in cloudfront you can change the link.

    REF: Using Alternate Domain Names