Skip to content
This repository
Browse code

Add the ability to retrieve object metadata using a #metadata method …

…on an object
  • Loading branch information...
commit 5197b3c2d15ecddc862bd09c2b65e3636169e8e9 1 parent 26a8353
Steve Smith scsmith authored

Showing 2 changed files with 11 additions and 1 deletion. Show diff stats Hide diff stats

  1. +2 1  lib/s3/object.rb
  2. +9 0 test/object_test.rb
3  lib/s3/object.rb
@@ -6,7 +6,7 @@ class Object
6 6 extend Forwardable
7 7
8 8 attr_accessor :content_type, :content_disposition, :content_encoding, :cache_control
9   - attr_reader :last_modified, :etag, :size, :bucket, :key, :acl, :storage_class
  9 + attr_reader :last_modified, :etag, :size, :bucket, :key, :acl, :storage_class, :metadata
10 10 attr_writer :content
11 11
12 12 def_instance_delegators :bucket, :name, :service, :bucket_request, :vhost?, :host, :path_prefix
@@ -235,6 +235,7 @@ def dump_headers
235 235 end
236 236
237 237 def parse_headers(response)
  238 + @metadata = response.to_hash.select{|k,v| k.to_s.start_with?('x-amz-meta') }
238 239 self.etag = response["etag"] if response.key?("etag")
239 240 self.content_type = response["content-type"] if response.key?("content-type")
240 241 self.content_disposition = response["content-disposition"] if response.key?("content-disposition")
9 test/object_test.rb
@@ -22,6 +22,7 @@ def setup
22 22 @response_binary["content-encoding"] = nil
23 23 @response_binary["last-modified"] = Time.now.httpdate
24 24 @response_binary["content-length"] = 20
  25 + @response_binary["x-amz-meta-test"] = 'metadata'
25 26
26 27 @xml_body = <<-EOXML
27 28 <?xml version="1.0" encoding="UTF-8"?>
@@ -132,6 +133,14 @@ def setup
132 133 assert @object_lena.retrieve
133 134 end
134 135
  136 + test "retrieve headers" do
  137 + @object_lena.expects(:object_request).twice.with(:head, {}).returns(@response_binary)
  138 + assert @object_lena.retrieve
  139 +
  140 + meta = {"x-amz-meta-test"=>["metadata"]}
  141 + assert_equal meta, @object_lena.retrieve.metadata
  142 + end
  143 +
135 144 test "exists" do
136 145 @object_lena.expects(:retrieve).returns(true)
137 146 assert @object_lena.exists?

0 comments on commit 5197b3c

Please sign in to comment.
Something went wrong with that request. Please try again.