Skip to content

How to: Store private public uploads in different Cloud Files Containers with Fog

Chris Blunt edited this page May 9, 2016 · 6 revisions

When using Cloud Files Containers, you cannot set individual file permissions - a container and its files are either public (delivered over CDN), or private.

When using Fog in your Uploader, you can specify a different container from the configured defaults by simply calling fog_host and fog_directory directly in your uploader.

For example, given a simple web store selling documents, the uploaded document files that people can buy should be private:

# config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider             => 'Rackspace',
    :rackspace_username   => 'xxxxxxxxx',
    :rackspace_api_key    => 'yyyyyyyyy',
    :rackspace_servicenet => Rails.env.production?
  }

  config.fog_directory = 'my_private_container'
  config.fog_public = false
  config.fog_authenticated_url_expiration = 1.minute
end
# app/uploaders/document_uploader.rb
class DocumentUploader < CarrierWave::Uploader::Base
  # Uploader will use the private container configured in config/initializer/carrier_wave.rb
  storage :fog 
end

Whilst the uploaded screenshots to be used on the product pages should be publicly available and delivered via CDN:

# app/uploaders/screenshot_uploader.rb
class ScreenshotUploader < CarrierWave::Uploader::Base
  storage :fog 

  # Configure uploads to be stored in a public Cloud Files container
  def fog_directory
    'my_public_container'
  end

  # Configure uploads to be delivered over Rackspace CDN
  def asset_host
   "c000000.cdn.rackspacecloud.com"
  end

  # Configure fog to serve using public URL
  def fog_public
    true
  end
end
Clone this wiki locally