Tempfile creation directly on Amazon S3 which can be utilized by a Ruby on Rails application.
This was initially built and is intended to work on Heroku but can work on any platform.
Add the gem to your Gemfile
gem 'cloud_tempfile'CloudTempfile supports the following methods of configuration.
- Built-in Initializer (configured through environment variables)
- Rails Initializer
- A YAML config file
Using the Built-in Initializer is the default method and is supposed to be used with environment variables. It's the recommended approach for deployments on Heroku.
If you need more control over configuration you will want to use a custom rails initializer.
Configuration using a YAML file (a common strategy for Capistrano deployments) is also supported.
The recommend way to configure cloud_tempfile is by using environment variables however it's up to you, it will work fine if you hard code them too. The main reason is that then your access keys are not checked into version control.
The Built-in Initializer will configure CloudTempfile based on the contents of your environment variables.
Add your configuration details to heroku
heroku config:add AWS_ACCESS_KEY_ID=xxxx
heroku config:add AWS_SECRET_ACCESS_KEY=xxxx
heroku config:add FOG_DIRECTORY=xxxx
heroku config:add FOG_PROVIDER=AWS
# and optionally:
heroku config:add FOG_REGION=eu-west-1Or add to a traditional unix system
export AWS_ACCESS_KEY_ID=xxxx
export AWS_SECRET_ACCESS_KEY=xxxx
export FOG_DIRECTORY=xxxxRackspace configuration is also supported
heroku config:add RACKSPACE_USERNAME=xxxx
heroku config:add RACKSPACE_API_KEY=xxxx
heroku config:add FOG_DIRECTORY=xxxx
heroku config:add FOG_PROVIDER=RackspaceGoogle Storage Cloud configuration is supported as well
heroku config:add FOG_PROVIDER=Google
heroku config:add GOOGLE_STORAGE_ACCESS_KEY_ID=xxxx
heroku config:add GOOGLE_STORAGE_SECRET_ACCESS_KEY=xxxx
heroku config:add FOG_DIRECTORY=xxxxIf you want to enable some of the advanced configuration options you will want to create your own initializer.
Run the included Rake task to generate a starting point.
rails g cloud_tempfile:install --provider=Rackspace
rails g cloud_tempfile:install --provider=AWS
The generator will create a Rails initializer at config/initializers/cloud_tempfile.rb.
CloudTempfile.configure do |config|
config.enabled = true
config.fog_provider = 'AWS'
config.fog_directory = ENV['FOG_DIRECTORY']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
# Increase upload performance by configuring your region
# config.fog_region = 'eu-west-1'
#
# Fail silently. Useful for environments such as Heroku
# config.fail_silently = true
endRun the included Rake task to generate a starting point.
rails g cloud_tempfile:install --use-yml --provider=Rackspace
rails g cloud_tempfile:install --use-yml --provider=AWS
The generator will create a YAML file at config/cloud_tempfile.yml.
defaults: &defaults
enabled: true
fog_provider: "AWS"
fog_directory: "rails-app-assets"
aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
# To use AWS reduced redundancy storage.
# (http://aws.amazon.com/about-aws/whats-new/2010/05/19/announcing-amazon-s3-reduced-redundancy-storage/)
# aws_reduced_redundancy: true
# You may need to specify what region your storage bucket is in
# fog_region: "eu-west-1"
# Fail silently. Useful for environments such as Heroku
# fail_silently: true
development:
enabled: true
test:
enabled: true
production:
<<: *defaultsAll CloudTempfile configuration can be modified directly using environment variables with the Built-in initializer. e.g.
CloudTempfile.config.fog_provider == ENV['FOG_PROVIDER']- enabled: (
true, false) when false, will disable CloudTempfile and local Tempfile will be created instead. - default:
'true'(enabled)
- fog_provider: your storage provider AWS (S3) or Rackspace (Cloud Files) or Google (Google Storage)
- fog_directory: your bucket name
- fog_region: the region your storage bucket is in e.g. eu-west-1
- aws_access_key_id: your Amazon S3 access key
- aws_secret_access_key: your Amazon S3 access secret
- rackspace_username: your Rackspace username
- rackspace_api_key: your Rackspace API Key.
- google_storage_access_key_id: your Google Storage access key
- google_storage_secret_access_key: your Google Storage access secret
- rackspace_auth_url: Rackspace auth URL, for Rackspace London use:
https://lon.identity.api.rackspacecloud.com/v2.0
If you are using anything other than the US buckets with S3 then you'll want to set the region. For example with an EU bucket you could set the following environment variable.
heroku config:add FOG_REGION=eu-west-1Or via a custom initializer
CloudTempfile.configure do |config|
# ...
config.fog_region = 'eu-west-1'
endOr via YAML
production:
# ...
fog_region: 'eu-west-1'A rake task is included within the cloud_tempfile gem to perform the clean up:
namespace :cloud_tempfile do
desc "Clean up expired temp files from the remote storage"
task :clear => :environment do
CloudTempfile.clear
end
endTo automate the packaging of this gem, please use Jeweler
- Support more cloud storage providers
- Better test coverage
Inspired by:
MIT License. Copyright 2014 Kevin Bolduc.


