Skip to content

Commit

Permalink
Merge pull request #935 from rubygems/aws-sdk
Browse files Browse the repository at this point in the history
Aws sdk
  • Loading branch information
Arthur Nogueira Neves committed Apr 13, 2015
2 parents d978b26 + 673ddf1 commit 4be294e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gem 'rails', '~> 4.2.1'
gem 'psych', '~> 2.0.12'
gem 'builder'
gem 'dynamic_form'
gem 's3'
gem 'aws-sdk-core'
gem 'gchartrb', require: 'google_chart'
gem 'gravtastic'
gem 'high_voltage'
Expand Down
11 changes: 7 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ GEM
arel (6.0.0)
autoprefixer-rails (4.0.2.2)
execjs
aws-sdk-core (2.0.38)
builder (~> 3.0)
jmespath (~> 1.0)
multi_json (~> 1.0)
bcrypt (3.1.10)
builder (3.2.2)
capistrano (2.15.5)
Expand Down Expand Up @@ -99,6 +103,8 @@ GEM
honeybadger (1.16.7)
json
i18n (0.7.0)
jmespath (1.0.2)
multi_json (~> 1.0)
jquery-rails (3.1.2)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
Expand Down Expand Up @@ -131,7 +137,6 @@ GEM
paul_revere (1.3)
rails (>= 3.0)
pg (0.18.1)
proxies (0.2.1)
psych (2.0.13)
rack (1.6.0)
rack-test (0.6.3)
Expand Down Expand Up @@ -175,8 +180,6 @@ GEM
netrc (~> 0.7)
rr (1.1.2)
ruby-graphviz (1.0.9)
s3 (0.3.21)
proxies (~> 0.2.0)
sass (3.4.13)
sass-rails (5.0.1)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -229,6 +232,7 @@ PLATFORMS

DEPENDENCIES
autoprefixer-rails
aws-sdk-core
builder
capistrano (~> 2.0)
capistrano-notification
Expand Down Expand Up @@ -266,7 +270,6 @@ DEPENDENCIES
redis
rest-client
rr
s3
sass-rails (~> 5.0.0)
shoulda
statsd-instrument (~> 2.0.6)
Expand Down
59 changes: 38 additions & 21 deletions lib/rubygem_fs.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
module RubygemFs

def self.instance
@fs ||=
if Rails.env.development?
RubygemFs::Local.new
else
RubygemFs::S3.new
RubygemFs::S3.new(access_key_id: ENV['S3_KEY'],
secret_access_key: ENV['S3_SECRET'],
region: 'us-east-1',
endpoint: "https://s3.amazonaws.com")
end
end

def self.mock!
@fs = RubygemFs::Local.new.tap do |fs|
def fs.base_dir
@dir ||= Dir.mktmpdir
end
@fs = RubygemFs::Local.new
def @fs.base_dir
@dir ||= Dir.mktmpdir
end
end

def self.s3!(host)
uri = URI(host)
@fs = RubygemFs::S3.new
s3 = @fs.s3(access_key_id: 'k', secret_access_key: 's', proxy: {host: uri.host, port: uri.port})
s3.buckets.build(Gemcutter.config['s3_bucket']).save
@fs = RubygemFs::S3.new(access_key_id: 'k',
secret_access_key: 's',
region: 'us-east-1',
endpoint: host,
force_path_style: true)
def @fs.init
s3.create_bucket(bucket: bucket)
end
@fs.init
end

class Local
Expand Down Expand Up @@ -50,30 +56,41 @@ def base_dir
end

class S3
def initialize(config)
@config = config
end

def store(key, body)
object = bucket.objects.build(key)
object.content = body
object.save
s3.put_object(key: key, body: body, bucket: bucket, acl: 'public-read')
end

def get(key)
if object = bucket.objects.find(key)
object.content
end
s3.get_object(key: key, bucket: bucket).body.read
rescue Aws::S3::Errors::NoSuchKey
nil
end

def remove(key)
if object = bucket.objects.find(key)
object.destroy
s3.delete_object(key: key, bucket: bucket)
end

def restore(key)
begin
s3.get_object(key: key, bucket: bucket)
rescue Aws::S3::Errors::NoSuchKey => e
version_id = e.context.http_response.headers["x-amz-version-id"]
s3.delete_object(key: key, bucket: bucket, version_id: version_id)
end
end

private

def bucket
@bucket ||= s3.buckets.find(Gemcutter.config['s3_bucket'])
Gemcutter.config['s3_bucket']
end

def s3(options = nil)
@s3 ||= ::S3::Service.new(options || { access_key_id: ENV['S3_KEY'], secret_access_key: ENV['S3_SECRET'] })
def s3
@s3 ||= Aws::S3::Client.new(@config)
end
end
end

0 comments on commit 4be294e

Please sign in to comment.