Skip to content

Instantly share code, notes, and snippets.

@geemus
Created November 22, 2010 22:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geemus/710869 to your computer and use it in GitHub Desktop.
Save geemus/710869 to your computer and use it in GitHub Desktop.
fog - resume uploading example

setup by placing resume.rb and Gemfile in a directory together, then run bundle install

You can configure the running of this script with ENV variables. In every case specify DIRECTORY_NAME, FILE, and PROVIDER and then just add the appropriate credentials for the selected provider

Amazon S3: DIRECTORY_NAME=fogresumes FILE=resume.html PROVIDER=AWS AWS_ACCESS_KEY_ID=XXX AWS_SECRET_ACCESS_KEY=YYY bundle exec ruby resume.rb

Google Storage For Developers: DIRECTORY_NAME=fogresumes FILE=resume.html PROVIDER=Google GOOGLE_STORAGE_ACCESS_KEY_ID=XXX GOOGLE_STORAGE_SECRET_ACCESS_KEY=YYY bundle exec ruby resume.rb

Local: DIRECTORY_NAME=fogresumes FILE=resume.html PROVIDER=Local LOCAL_ROOT=XXX bundle exec ruby resume.rb

Rackspace Cloud Files: DIRECTORY_NAME=fogresumes FILE=resume.html PROVIDER=Rackspace RACKSPACE_API_KEY_ID=XXX RACKSPACE_USERNAME=YYY bundle exec ruby resume.rb

You can also add MOCK=true if you'd like to run in a mocked mode (except for Rackspace which doesn't have mocks yet, contributions are welcome). Just note that in mock mode the url it lists won't actually work.

source "http://rubygems.org"
gem 'fog'
#!/usr/bin/env ruby
require 'rubygems'
require 'fog'
if ENV['MOCK'] == 'true'
Fog.mock!
end
# pull valid configuration values from ENV and map them to lowercase symbols
credentials = {}
for key, value in ENV
valid_keys = [
'PROVIDER',
'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY',
'GOOGLE_STORAGE_ACCESS_KEY_ID', 'GOOGLE_STORAGE_SECRET_ACCESS_KEY',
'LOCAL_ROOT',
'RACKSPACE_API_KEY', 'RACKSPACE_USERNAME'
]
if valid_keys.include?(key)
credentials[key.downcase.to_sym] = value
end
end
# ensure provider was set
unless credentials[:provider]
raise ArgumentError.new('Please specify PROVIDER to use, choose one of [AWS, Google, Local, Rackspace]')
end
# pull directory name from ENV
unless directory_name = ENV['DIRECTORY_NAME']
raise ArgumentError.new('Please specify DIRECTORY_NAME to upload to')
end
# pull file from ENV to set file data and name
unless file = ENV['FILE']
raise ArgumentError.new('Please specify FILE to upload')
else
file_path = File.expand_path(file)
file_name = file_path.split(File::SEPARATOR).last
file_data = File.open(file_path)
end
Formatador.display_line # header padding
# create connection from credentials
Formatador.display_lines("Creating connection [bold]#{credentials.inspect}[/]")
connection = Fog::Storage.new(credentials)
# find directory or create it if it doesn't exist
begin
directory = connection.directories.get(directory_name)
rescue Excon::Errors::Forbidden
raise ArgumentError.new("DIRECTORY_NAME is already in use by another user, please change DIRECTORY_NAME and try again")
end
if directory
Formatador.display_line("Found directory [bold]#{directory_name}[/]")
else
directory = connection.directories.create(
:key => directory_name,
:public => true
)
Formatador.display_line("Created directory [bold]#{directory_name}[/]")
end
Formatador.display_line("Creating file [bold]#{file_name}[/]")
file = directory.files.create(
:key => file_name,
:body => file_data,
:public => true
)
unless ENV['PROVIDER'] == 'Local'
Formatador.display_line("File is available at [negative]#{file.public_url}[/]")
else
path = File.join(credentials[:local_root], directory_name, file_name)
Formatador.display_line("File is available at [negative]#{path}[/]")
end
Formatador.display("Press return to begin cleanup...")
STDIN.getc
Formatador.display_line("Deleting file [bold]#{file_name}[/]")
file.destroy
Formatador.display_line("Deleting directory [bold]#{directory_name}[/]")
directory.destroy
Formatador.display_line # footer padding
@bobbdelsol
Copy link

have you implemented the s3.get_link which provides a temporary URL to a private file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment