Skip to content

Instantly share code, notes, and snippets.

@jeremyruppel
Created July 30, 2012 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyruppel/3208764 to your computer and use it in GitHub Desktop.
Save jeremyruppel/3208764 to your computer and use it in GitHub Desktop.
A simple fog/s3 client
require 'fog'
module AWS
class Client
def initialize( options={} )
@access_key = options.delete( :access_key ) or raise "AWS::Client requires an :access_key"
@secret_key = options.delete( :secret_key ) or raise "AWS::Client requires a :secret_key"
@bucket_name = options.delete( :bucket_name ) or raise "AWS::Client requires a :bucket_name"
@aws_region = options.delete( :aws_region )
end
def get( filename )
bucket.files.get filename
end
def read( filename )
get( filename ).body rescue "Can't read file at #{filename}"
end
def write( filename, body )
if file = get( filename )
file.body = body
file.save
else
storage.put_object @bucket_name, filename, body, headers
end
end
protected
def headers
{ 'x-amz-acl' => 'public-read' }
end
def bucket
@bucket ||= storage.directories.get @bucket_name
end
def storage
@storage ||= begin
storage = Fog::Storage.new :provider => 'AWS',
:aws_access_key_id => @access_key,
:aws_secret_access_key => @secret_key,
:region => @aws_region
storage.sync_clock
storage
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment