Class: RackFakeS3::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_fake_s3/file_store.rb

Constant Summary collapse

SHUCK_METADATA_DIR =
".rack_fake_s3_metadataFFF"

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack_fake_s3/file_store.rb', line 15

def initialize(root)
  @root = root
  @buckets = []
  @bucket_hash = {}
  Dir[File.join(root,"*")].each do |bucket|
    bucket_name = File.basename(bucket)
    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

Instance Method Details

#bucketsObject



52
53
54
# File 'lib/rack_fake_s3/file_store.rb', line 52

def buckets
  @buckets
end

#copy_object(src_bucket_name, src_name, dst_bucket_name, dst_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rack_fake_s3/file_store.rb', line 104

def copy_object(src_bucket_name,src_name,dst_bucket_name,dst_name)
  src_root = File.join(@root,src_bucket_name,src_name,SHUCK_METADATA_DIR)
   = File.join(src_root,"metadata")
   = YAML.load(File.open(,'rb').read)
  src_content_filename = File.join(src_root,"content")

  dst_filename= File.join(@root,dst_bucket_name,dst_name)
  FileUtils.mkdir_p(dst_filename)

   = File.join(dst_filename,SHUCK_METADATA_DIR)
  FileUtils.mkdir_p()

  content = File.join(,"content")
   = File.join(,"metadata")

  File.open(content,'wb') do |f|
    File.open(src_content_filename,'rb') do |input|
      f << input.read
    end
  end

  File.open(,'w') do |f|
    File.open(,'r') do |input|
      f << input.read
    end
  end

  src_bucket = self.get_bucket(src_bucket_name)
  dst_bucket = self.get_bucket(dst_bucket_name)

  obj = S3Object.new
  obj.name = dst_name
  obj.md5 = [:md5]
  obj.content_type = [:content_type]

  src_obj = src_bucket.find(src_name)
  dst_bucket.add(obj)
  src_bucket.remove(src_obj)
  return obj
end

#create_bucket(bucket) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/rack_fake_s3/file_store.rb', line 64

def create_bucket(bucket)
  FileUtils.mkdir_p(File.join(@root,bucket))
  bucket_obj = Bucket.new(bucket,Time.now,[])
  if !@bucket_hash[bucket]
    @buckets << bucket_obj
    @bucket_hash[bucket] = bucket_obj
  end
  bucket_obj
end

#delete_bucket(bucket_name) ⇒ Object

Raises:



74
75
76
77
78
79
80
# File 'lib/rack_fake_s3/file_store.rb', line 74

def delete_bucket(bucket_name)
  bucket = get_bucket(bucket_name)
  raise NoSuchBucket if !bucket
  raise BucketNotEmpty if bucket.objects.count > 0
  FileUtils.rm_r(get_bucket_folder(bucket))
  @bucket_hash.delete(bucket_name)
end

#delete_object(bucket, object_name, request) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rack_fake_s3/file_store.rb', line 201

def delete_object(bucket,object_name,request)
  begin
    filename = File.join(@root,bucket.name,object_name)
    FileUtils.rm_rf(filename)
    object = bucket.find(object_name)
    bucket.remove(object)
  rescue
    puts $!
    $!.backtrace.each { |line| puts line }
    return nil
  end
end

#get_bucket(bucket) ⇒ Object



60
61
62
# File 'lib/rack_fake_s3/file_store.rb', line 60

def get_bucket(bucket)
  @bucket_hash[bucket]
end

#get_bucket_folder(bucket) ⇒ Object



56
57
58
# File 'lib/rack_fake_s3/file_store.rb', line 56

def get_bucket_folder(bucket)
  File.join(@root,bucket.name)
end

#get_object(bucket, object_name, request) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rack_fake_s3/file_store.rb', line 82

def get_object(bucket,object_name, request)
  begin
    real_obj = S3Object.new
    object_name = URI.decode(object_name)
    obj_root = File.join(@root,bucket,object_name,SHUCK_METADATA_DIR)
     = YAML.load(File.open(File.join(obj_root,"metadata"),'rb'))
    real_obj.name = object_name
    real_obj.md5 = [:md5]
    real_obj.content_type = .fetch(:content_type) { "application/octet-stream" }
    #real_obj.io = File.open(File.join(obj_root,"content"),'rb')
    real_obj.io = RateLimitableFile.open(File.join(obj_root,"content"),'rb')
    return real_obj
  rescue
    puts $!
    $!.backtrace.each { |line| puts line }
    return nil
  end
end

#object_metadata(bucket, object) ⇒ Object



101
102
# File 'lib/rack_fake_s3/file_store.rb', line 101

def (bucket,object)
end

#rate_limit=(rate_limit) ⇒ Object

Pass a rate limit in bytes per second



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rack_fake_s3/file_store.rb', line 34

def rate_limit=(rate_limit)
  if rate_limit.is_a?(String)
    if rate_limit =~ /^(\d+)$/
      RateLimitableFile.rate_limit = rate_limit.to_i
    elsif rate_limit =~ /^(.*)K$/
      RateLimitableFile.rate_limit = $1.to_f * 1000
    elsif rate_limit =~ /^(.*)M$/
      RateLimitableFile.rate_limit = $1.to_f * 1000000
    elsif rate_limit =~ /^(.*)G$/
      RateLimitableFile.rate_limit = $1.to_f * 1000000000
    else
      raise "Invalid Rate Limit Format: Valid values include (1000,10K,1.1M)"
    end
  else
    RateLimitableFile.rate_limit = nil
  end
end

#store_object(bucket, object_name, request) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rack_fake_s3/file_store.rb', line 145

def store_object(bucket,object_name,request)
  begin
    filename = File.join(@root,bucket.name,object_name)
    FileUtils.mkdir_p(filename)

     = File.join(filename,SHUCK_METADATA_DIR)
    FileUtils.mkdir_p()

    content = File.join(filename,SHUCK_METADATA_DIR,"content")
     = File.join(filename,SHUCK_METADATA_DIR,"metadata")

    md5 = Digest::MD5.new
    # TODO put a tmpfile here first and mv it over at the end

    match=request.content_type.match(/^multipart\/form-data; boundary=(.+)/)
    boundary = match[1] if match
    if boundary
      boundary = WEBrick::HTTPUtils::dequote(boundary)
      filedata = WEBrick::HTTPUtils::parse_form_data(request.body, boundary)
      raise HTTPStatus::BadRequest if filedata['file'].empty?
      File.open(content, 'wb') do |f|
        f<<filedata['file']
        md5<<filedata['file']
      end
    else
      File.open(content,'wb') do |f|
        request.body do |chunk|
          f << chunk
          md5 << chunk
        end
      end
    end

     = {}
    [:md5] = md5.hexdigest
    [:content_type] = MIME::Types.type_for(object_name).first.to_s

    yaml = YAML::dump()
    File.open(,'w') do |f|
      f << yaml
    end

    obj = S3Object.new
    obj.name = object_name
    obj.md5 = [:md5]
    obj.content_type = [:content_type]

    bucket.add(obj)
    return obj
  rescue
    puts $!
    $!.backtrace.each { |line| puts line }
    return nil
  end
end