DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > CarrierWave on Heroku with CloudFront

CarrierWave on Heroku with CloudFront

Jeff Dickey user avatar by
Jeff Dickey
·
Dec. 12, 11 · Cloud Zone · Interview
Like (0)
Save
Tweet
7.99K Views

Join the DZone community and get the full member experience.

Join For Free

This post will enable you to have your Rails app accept file uploads through a gem called CarrierWave. Optionally, you can use CloudFront in order to have a CDN backbone for those files. This is currently the best method for file uploading on Heroku.

For a full-fledged working project with this on Heroku's cedar stack, check out http://github.com/dickeytk/painting-gallery

Prerequisites

  • Go to S3 and get an AWS Access Key ID and an AWS Secret Access Key.
  • Create a new S3 bucket in the US East Availability Zone. (Since that's the same one that your Heroku dyno is already in)
  • If you want to use CloudFront, add a CloudFront distribution pointing to the S3 bucket you just created.

Instructions

Add the gems to your Gemfile:

gem 'fog'
gem 'carrierwave'
gem 'rmagick' # optional, for image resizing support

Add the file './config/initializers/carrierwave.rb' for your CarrierWave configuration:

# ./config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider => "AWS",
    :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
  }
  config.fog_directory = ENV['S3_BUCKET']

  # use only one of the following 2 settings
  config.fog_host = "http://#{ENV['S3_BUCKET']}.s3.amazonaws.com" # for no cloudfront
  config.fog_host = ENV['S3_CDN'] # for cloudfront

end

Add your environment variables:

$ heroku config:add AWS_ACCESS_KEY_ID=[your_access_key_here]
$ heroku config:add AWS_SECRET_ACCESS_KEY=[your_access_key_here]
$ heroku config:add S3_BUCKET=[your_s3_bucket]
$ heroku config:add S3_CDN=[your_cloudfront_url] # optional for cloudfront support

That's it! Should be configured now. All you need to do is add a string column to a model for the file name, a file uploader to add files to a model, and a form to actually accept the uploads. CarrierWave's docs explain how to do that all very well.

Using your own domain

If you want to use your own domain (such as content.jeffdickey.info like I use on this site) simply add a CNAME in your dns config pointing to the S3 bucket, then change the S3_CDN environment variable to point to it.

Optional CloudFront note

Now, if you are going to use CloudFront, you should add this bit to your uploader which will append a file's hash to the end of a filename. This is so CloudFront won't cache old content.

  # ./app/uploaders/image_uploader.rb

  # ...

  # Appends hash to filename to prevent CloudFront from caching old content
  def filename
    if original_filename
      @hash ||= Digest::MD5.hexdigest(File.dirname(current_path))
      "#{file.basename.split('_').last}-#{@hash}.#{file.extension}"
    end
  end

Known issue

There is a potential issue to look out for here. Uploading files that will take >30 seconds will result in Heroku killing the request, so you need to directly upload the files. This S3 SWF upload plugin should help with that. But don't throw that on just 'because'. It's likely to just cause grief.

Summary

I hope this works out well for you! I've configured this type of setup several times now and seems to work great! I did it all from memory though, so let me know if you run into any issues.

 

Source: http://jeffdickey.info/using-carrierwave-on-heroku-for-image-uploading-w-cloudfront

 

 

CloudFront

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Composable Architecture
  • DZone's Article Submission Guidelines
  • Which JVM Version Is the Fastest?
  • 12 Kick-Ass Software Prototyping and Mockup Tools

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo