Permalink
Browse files

ported fake s3 server to rack

Signed-off-by: David A. Cuadrado <krawek@gmail.com>
  • Loading branch information...
1 parent 7590def commit cdb66544796a58a6d681c614dc9023459a8e193b @dcu dcu committed Apr 22, 2012
Showing with 156 additions and 71 deletions.
  1. +4 −0 Gemfile
  2. +17 −0 Gemfile.lock
  3. +4 −0 config.ru
  4. +2 −0 fakes3.gemspec
  5. +6 −10 lib/fakes3/cli.rb
  6. +11 −2 lib/fakes3/file_store.rb
  7. +110 −59 lib/fakes3/server.rb
  8. +2 −0 test/right_aws_commands_test.rb
View
@@ -2,3 +2,7 @@ source :rubygems
gem 'fakes3', :path => '.' # for dev and test, use local fakes3
# Specify your gem's dependencies in fakes3.gemspec
gemspec
+
+group :development do
+ gem 'pry'
+end
View
@@ -3,6 +3,8 @@ PATH
specs:
fakes3 (0.1.5)
builder
+ rack
+ thin
thor
GEM
@@ -13,11 +15,25 @@ GEM
mime-types
xml-simple
builder (3.0.0)
+ coderay (1.0.6)
+ daemons (1.1.8)
+ eventmachine (0.12.10)
+ method_source (0.7.1)
mime-types (1.18)
+ pry (0.9.8.4)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7.1)
+ slop (>= 2.4.4, < 3)
+ rack (1.4.1)
rake (0.9.2.2)
right_aws (3.0.4)
right_http_connection (>= 1.2.5)
right_http_connection (1.3.0)
+ slop (2.4.4)
+ thin (1.3.1)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
thor (0.14.6)
xml-simple (1.1.1)
@@ -28,5 +44,6 @@ DEPENDENCIES
aws-s3
bundler (>= 1.0.0)
fakes3!
+ pry
rake
right_aws
View
@@ -0,0 +1,4 @@
+$:.unshift File.expand_path("../lib", __FILE__)
+require 'fakes3'
+
+run FakeS3::App.new(FakeS3::FileStore.new(ENV['FAKE_S3_ROOT']), ENV['FAKE_S3_HOSTNAME'])
View
@@ -22,6 +22,8 @@ Gem::Specification.new do |s|
#s.add_development_dependency "ruby-debug19"
s.add_dependency "thor"
s.add_dependency "builder"
+ s.add_dependency "rack"
+ s.add_dependency "thin"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
@@ -9,19 +9,15 @@ class CLI < Thor
desc "server", "Run a server on a particular hostname"
method_option :root, :type => :string, :aliases => '-r', :required => true
method_option :port, :type => :numeric, :aliases => '-p', :required => true
- method_option :hostname, :type => :string, :aliases => '-h', :desc => "The root name of the host. Defaults to s3.amazonaws.com."
+ method_option :hostname, :type => :string, :aliases => '-h', :desc => "The root name of the host. Defaults to 0.0.0.0"
method_option :limit, :aliases => '-l', :type => :string, :desc => 'Rate limit for serving (ie. 50K, 1.0M)'
def server
- store = nil
- if options[:root]
- root = File.expand_path(options[:root])
- # TODO Do some sanity checking here
- store = FileStore.new(root)
- end
-
- if store.nil?
+ root = nil
+ if options[:root].nil?
puts "You must specify a root to use a file store (the current default)"
exit(-1)
+ else
+ root = File.expand_path(options[:root])
end
hostname = 's3.amazonaws.com'
@@ -43,7 +39,7 @@ def server
end
puts "Loading FakeS3 with #{root} on port #{options[:port]} with hostname #{hostname}"
- server = FakeS3::Server.new(options[:port],store,hostname)
+ server = FakeS3::Server.new(options[:port],root,hostname)
server.serve
end
@@ -19,6 +19,12 @@ def initialize(root)
bucket_obj = Bucket.new(bucket_name,Time.now,[])
@buckets << bucket_obj
@bucket_hash[bucket_name] = bucket_obj
+
+ # FIXME: this is not the best place to do this
+ # Dir[File.join(bucket, "*")].each do |s3object|
+ # obj = get_object(bucket_name, File.basename(s3object), nil)
+ # bucket_obj.add obj
+ # end
end
end
@@ -148,15 +154,18 @@ def store_object(bucket,object_name,request)
# TODO put a tmpfile here first and mv it over at the end
File.open(content,'wb') do |f|
- request.body do |chunk|
+ loop do
+ chunk = request.body.read(512)
+ break if chunk.nil? || chunk.empty?
+
f << chunk
md5 << chunk
end
end
metadata_struct = {}
metadata_struct[:md5] = md5.hexdigest
- metadata_struct[:content_type] = request.header["content-type"].first
+ metadata_struct[:content_type] = request.media_type || ""
File.open(metadata,'w') do |f|
f << YAML::dump(metadata_struct)
Oops, something went wrong.

0 comments on commit cdb6654

Please sign in to comment.