Created
July 30, 2012 18:07
-
-
Save jeremyruppel/3208764 to your computer and use it in GitHub Desktop.
A simple fog/s3 client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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