Class AWS::S3::S3Object
In: lib/aws/s3/object.rb
Parent: Base

S3Objects represent the data you store on S3. They have a key (their name) and a value (their data). All objects belong to a bucket.

You can store an object on S3 by specifying a key, its data and the name of the bucket you want to put it in:'me.jpg', open('headshot.jpg'), 'photos')

The content type of the object will be inferred by its extension. If the appropriate content type can not be inferred, S3 defaults to binary/octect-stream.

If you want to override this, you can explicitly indicate what content type the object should have with the :content_type option:

  file = 'black-flowers.m4a'
    :content_type => 'audio/mp4a-latm'

You can read more about storing files on S3 in the documentation for

If you just want to fetch an object you’ve stored on S3, you just specify its name and its bucket:

  picture = S3Object.find 'headshot.jpg', 'photos'

N.B. The actual data for the file is not downloaded in both the example where the file appeared in the bucket and when fetched directly. You get the data for the file like this:


You can fetch just the object’s data directly:

  S3Object.value 'headshot.jpg', 'photos'

Or stream it by passing a block to stream:

  open('song.mp3', 'w') do |file|'song.mp3', 'jukebox') do |chunk|
      file.write chunk

The data of the file, once download, is cached, so subsequent calls to value won’t redownload the file unless you tell the object to reload its value:

  # Redownloads the file's data

Other functionality includes:

  # Check if an object exists?
  S3Object.exists? 'headshot.jpg', 'photos'

  # Copying an object
  S3Object.copy 'headshot.jpg', 'headshot2.jpg', 'photos'

  # Renaming an object
  S3Object.rename 'headshot.jpg', 'portrait.jpg', 'photos'

  # Deleting an object
  S3Object.delete 'headshot.jpg', 'photos'

More about objects and their metadata

You can find out the content type of your object with the content_type method:

  # => "audio/mpeg"

You can change the content type as well if you like:

  song.content_type = 'application/pdf'

(Keep in mind that due to limitiations in S3’s exposed API, the only way to change things like the content_type is to PUT the object onto S3 again. In the case of large files, this will result in fully re-uploading the file.)

A bevie of information about an object can be had using the about method:

  pp song.about
  {"last-modified"    => "Sat, 28 Oct 2006 21:29:26 GMT",
   "content-type"     => "binary/octect-stream",
   "etag"             => "\"dc629038ffc674bee6f62eb64ff3a\"",
   "date"             => "Sat, 28 Oct 2006 21:30:41 GMT",
   "x-amz-request-id" => "B7BC68F55495B1C8",
   "server"           => "AmazonS3",
   "content-length"   => "3418766"}

You can get and set metadata for an object:

  # => {}
  song.metadata[:album] = "A River Ain't Too Much To Love"
  # => "A River Ain't Too Much To Love"
  song.metadata[:released] = 2005
  pp song.metadata
  {"x-amz-meta-released" => 2005,
    "x-amz-meta-album"   => "A River Ain't Too Much To Love"}

That metadata will be saved in S3 and is hence forth available from that object:

  song = S3Object.find('black-flowers.mp3', 'jukebox')
  pp song.metadata
  {"x-amz-meta-released" => "2005",
    "x-amz-meta-album"   => "A River Ain't Too Much To Love"}
  # => "2005"
  song.metada[:released] = 2006
  pp song.metada
  {"x-amz-meta-released" => 2006,
   "x-amz-meta-album"    => "A River Ain't Too Much To Love"}


about   about   belongs_to_bucket?   bucket   bucket=   copy   copy   create   delete   delete   etag   exists?   find   key   key=   key_set?   metadata   new   orphan?   owner   rename   rename   save   store   store   stored?   stream   url   url_for   value   value  

Included Modules


External Aliases

store -> create
store -> save

Public Class methods

Fetch information about the object with key from bucket. Information includes content type, content length, last modified time, and others.

If the specified key does not exist, NoSuchKey is raised.


     # File lib/aws/s3/object.rb, line 200
200:         def about(key, bucket = nil, options = {})
201:           response = head(path!(bucket, key, options), options)
202:           raise"No such key `#{key}'", bucket) if response.code == 404
204:         end

Makes a copy of the object with key to copy_name.


     # File lib/aws/s3/object.rb, line 182
182:         def copy(key, copy_key, bucket = nil, options = {})
183:           bucket          = bucket_name(bucket)
184:           original        = open(url_for(key, bucket))
185:           default_options = {:content_type => original.content_type}
186:           store(copy_key, original, bucket, default_options.merge(options))
187:           acl(copy_key, bucket, acl(key, bucket))
188:         end

Delete object with key from bucket.


     # File lib/aws/s3/object.rb, line 218
218:         def delete(key, bucket = nil, options = {})
219:           # A bit confusing. Calling super actually makes an HTTP DELETE request. The delete method is
220:           # defined in the Base class. It happens to have the same name.
221:           super(path!(bucket, key, options), options).success?
222:         end

Checks if the object with key in bucket exists.

  S3Object.exists? 'kiss.jpg', 'marcel'
  # => true


     # File lib/aws/s3/object.rb, line 210
210:         def exists?(key, bucket = nil)
211:           about(key, bucket)
212:           true
213:         rescue NoSuchKey
214:           false
215:         end

Returns the object whose key is name in the specified bucket. If the specified key does not exist, a NoSuchKey exception will be raised.


     # File lib/aws/s3/object.rb, line 145
145:         def find(key, bucket = nil)
146:           # N.B. This is arguably a hack. From what the current S3 API exposes, when you retrieve a bucket, it
147:           # provides a listing of all the files in that bucket (assuming you haven't limited the scope of what it returns).
148:           # Each file in the listing contains information about that file. It is from this information that an S3Object is built.
149:           #
150:           # If you know the specific file that you want, S3 allows you to make a get request for that specific file and it returns
151:           # the value of that file in its response body. This response body is used to build an S3Object::Value object. 
152:           # If you want information about that file, you can make a head request and the headers of the response will contain 
153:           # information about that file. There is no way, though, to say, give me the representation of just this given file the same 
154:           # way that it would appear in a bucket listing.
155:           #
156:           # When fetching a bucket, you can provide options which narrow the scope of what files should be returned in that listing.
157:           # Of those options, one is <tt>marker</tt> which is a string and instructs the bucket to return only object's who's key comes after
158:           # the specified marker according to alphabetic order. Another option is <tt>max-keys</tt> which defaults to 1000 but allows you
159:           # to dictate how many objects should be returned in the listing. With a combination of <tt>marker</tt> and <tt>max-keys</tt> you can
160:           # *almost* specify exactly which file you'd like it to return, but <tt>marker</tt> is not inclusive. In other words, if there is a bucket
161:           # which contains three objects who's keys are respectively 'a', 'b' and 'c', then fetching a bucket listing with marker set to 'b' will only
162:           # return 'c', not 'b'. 
163:           #
164:           # Given all that, my hack to fetch a bucket with only one specific file, is to set the marker to the result of calling String#previous on
165:           # the desired object's key, which functionally makes the key ordered one degree higher than the desired object key according to 
166:           # alphabetic ordering. This is a hack, but it should work around 99% of the time. I can't think of a scenario where it would return
167:           # something incorrect.
169:           # We need to ensure the key doesn't have extended characters but not uri escape it before doing the lookup and comparing since if the object exists, 
170:           # the key on S3 will have been normalized
171:           key    = key.remove_extended unless key.utf8?
172:           bucket = Bucket.find(bucket_name(bucket), :marker => key.previous, :max_keys => 1)
173:           # If our heuristic failed, trigger a NoSuchKey exception
174:           if (object = bucket.objects.first) && object.key == key
175:             object 
176:           else 
177:             raise"No such key `#{key}'", bucket)
178:           end
179:         end

Initializes a new S3Object.


     # File lib/aws/s3/object.rb, line 409
409:       def initialize(attributes = {}, &block)
410:         super
411:         self.value  = attributes.delete(:value) 
412:         self.bucket = attributes.delete(:bucket)
413:         yield self if block_given?
414:       end

Rename the object with key from to have key in to.


     # File lib/aws/s3/object.rb, line 191
191:         def rename(from, to, bucket = nil, options = {})
192:           copy(from, to, bucket, options)
193:           delete(from, bucket)
194:         end

When storing an object on the S3 servers using, the data argument can be a string or an I/O stream. If data is an I/O stream it will be read in segments and written to the socket incrementally. This approach may be desirable for very large files so they are not read into memory all at once.

  # Non streamed upload'greeting.txt', 'hello world!', 'marcel')

  # Streamed upload'roots.mpeg', open('roots.mpeg'), 'marcel')


     # File lib/aws/s3/object.rb, line 233
233:         def store(key, data, bucket = nil, options = {})
234:           validate_key!(key)
235:           # Must build path before infering content type in case bucket is being used for options
236:           path = path!(bucket, key, options)
237:           infer_content_type!(key, options)
239:           put(path, options, data) # Don't call .success? on response. We want to get the etag.
240:         end


     # File lib/aws/s3/object.rb, line 137
137:         def stream(key, bucket = nil, options = {}, &block)
138:           value(key, bucket, options) do |response|
139:             response.read_body(&block)
140:           end
141:         end

All private objects are accessible via an authenticated GET request to the S3 servers. You can generate an authenticated url for an object like this:

  S3Object.url_for('beluga_baby.jpg', 'marcel_molina')

By default authenticated urls expire 5 minutes after they were generated.

Expiration options can be specified either with an absolute time since the epoch with the :expires options, or with a number of seconds relative to now with the :expires_in options:

  # Absolute expiration date
  # (Expires January 18th, 2038)
  doomsday = Time.mktime(2038, 1, 18).to_i
                   :expires => doomsday)

  # Expiration relative to now specified in seconds
  # (Expires in 3 hours)
                   :expires_in => 60 * 60 * 3)

You can specify whether the url should go over SSL with the :use_ssl option:

  # Url will use https protocol
                   :use_ssl => true)

By default, the ssl settings for the current connection will be used.

If you have an object handy, you can use its url method with the same objects:

  song.url(:expires_in => 30)


     # File lib/aws/s3/object.rb, line 279
279:         def url_for(name, bucket = nil, options = {})
280:           connection.url_for(path!(bucket, name, options), options) # Do not normalize options
281:         end

Returns the value of the object with key in the specified bucket.

Conditional GET options

  • :if_modified_since - Return the object only if it has been modified since the specified time, otherwise return a 304 (not modified).
  • :if_unmodified_since - Return the object only if it has not been modified since the specified time, otherwise raise PreconditionFailed.
  • :if_match - Return the object only if its entity tag (ETag) is the same as the one specified, otherwise raise PreconditionFailed.
  • :if_none_match - Return the object only if its entity tag (ETag) is different from the one specified, otherwise return a 304 (not modified).

Other options

  • :range - Return only the bytes of the object in the specified range.


     # File lib/aws/s3/object.rb, line 133
133:         def value(key, bucket = nil, options = {}, &block)
134: !(bucket, key, options), options, &block))
135:         end

Public Instance methods

Interface to information about the current object. Information is read only, though some of its data can be modified through specific methods, such as content_type and content_type=.

  pp some_object.about
    {"last-modified"    => "Sat, 28 Oct 2006 21:29:26 GMT",
     "x-amz-id-2"       =>  "LdcQRk5qLwxJQiZ8OH50HhoyKuqyWoJ67B6i+rOE5MxpjJTWh1kCkL+I0NQzbVQn",
     "content-type"     => "binary/octect-stream",
     "etag"             => "\"dc629038ffc674bee6f62eb68454ff3a\"",
     "date"             => "Sat, 28 Oct 2006 21:30:41 GMT",
     "x-amz-request-id" => "B7BC68F55495B1C8",
     "server"           => "AmazonS3",
     "content-length"   => "3418766"}

 # => "binary/octect-stream"
 some_object.content_type = 'audio/mpeg'
 # => 'audio/mpeg'


     # File lib/aws/s3/object.rb, line 501
501:       def about
502:         stored? ? self.class.about(key, :
503:       end

Returns true if the current object has been assigned to a bucket yet. Objects must belong to a bucket before they can be saved onto S3.


     # File lib/aws/s3/object.rb, line 430
430:       def belongs_to_bucket?
431:         !@bucket.nil?
432:       end

The current object’s bucket. If no bucket has been set, a NoBucketSpecified exception will be raised. For cases where you are not sure if the bucket has been set, you can use the belongs_to_bucket? method.


     # File lib/aws/s3/object.rb, line 418
418:       def bucket
419:         @bucket or raise NoBucketSpecified
420:       end

Sets the bucket that the object belongs to.


     # File lib/aws/s3/object.rb, line 423
423:       def bucket=(bucket)
424:         @bucket = bucket
425:         self
426:       end

Copies the current object, given it the name copy_name. Keep in mind that due to limitations in S3’s API, this operation requires retransmitting the entire object to S3.


     # File lib/aws/s3/object.rb, line 541
541:       def copy(copy_name, options = {})
542:         self.class.copy(key, copy_name,, options)
543:       end
create(options = {})

Alias for store

Deletes the current object. Trying to save an object after it has been deleted with raise a DeletedObject exception.


     # File lib/aws/s3/object.rb, line 533
533:       def delete
534:         bucket.update(:deleted, self)
535:         freeze
536:         self.class.delete(key,
537:       end


     # File lib/aws/s3/object.rb, line 551
551:       def etag(reload = false)
552:         return nil unless stored?
553:         memoize(reload) do
554:           reload ? about(reload)['etag'][1...-1] : attributes['e_tag'][1...-1]
555:         end
556:       end

Returns the key of the object. If the key is not set, a NoKeySpecified exception will be raised. For cases where you are not sure if the key has been set, you can use the key_set? method. Objects must have a key set to be saved onto S3. Objects which have already been saved onto S3 will always have their key set.


     # File lib/aws/s3/object.rb, line 438
438:       def key
439:         attributes['key'] or raise NoKeySpecified
440:       end

Sets the key for the current object.


     # File lib/aws/s3/object.rb, line 443
443:       def key=(value)
444:         attributes['key'] = value
445:       end

Returns true if the current object has had its key set yet. Objects which have already been saved will always return true. This method is useful for objects which have not been saved yet so you know if you need to set the object’s key since you can not save an object unless its key has been set. if object.key_set? && object.belongs_to_bucket?


     # File lib/aws/s3/object.rb, line 452
452:       def key_set?
453:         !attributes['key'].nil?
454:       end

Interface to viewing and editing metadata for the current object. To be treated like a Hash.

  # => {}
  some_object.metadata[:author] = 'Dave Thomas'
  # => {"x-amz-meta-author" => "Dave Thomas"}
  # => "Dave Thomas"


     # File lib/aws/s3/object.rb, line 515
515:       def metadata
516:         about.metadata
517:       end

Alias for belongs_to_bucket?

Returns the owner of the current object.


     # File lib/aws/s3/object.rb, line 559
559:       def owner 
561:       end

Rename the current object. Keep in mind that due to limitations in S3’s API, this operation requires retransmitting the entire object to S3.


     # File lib/aws/s3/object.rb, line 547
547:       def rename(to, options = {})
548:         self.class.rename(key, to,, options)
549:       end
save(options = {})

Alias for store

Saves the current object with the specified options. Valid options are listed in the documentation for S3Object::store.


     # File lib/aws/s3/object.rb, line 521
521:       def store(options = {})
522:         raise DeletedObject if frozen?
523:         options  = about.to_headers.merge(options) if stored?
524:         response =, value,, options)
525:         bucket.update(:stored, self)
526:         response.success?
527:       end

Returns true if the current object has been stored on S3 yet.


     # File lib/aws/s3/object.rb, line 571
571:       def stored?
572:         !attributes['e_tag'].nil?
573:       end

Generates an authenticated url for the current object. Accepts the same options as its class method counter part S3Object.url_for.


     # File lib/aws/s3/object.rb, line 566
566:       def url(options = {})
567:         self.class.url_for(key,, options)
568:       end

Lazily loads object data.

Force a reload of the data by passing :reload.


When loading the data for the first time you can optionally yield to a block which will allow you to stream the data in segments.

  object.value do |segment|
    send_data segment

The full list of options are listed in the documentation for its class method counter part, S3Object::value.


     # File lib/aws/s3/object.rb, line 470
470:       def value(options = {}, &block)
471:         if options.is_a?(Hash)
472:           reload = !options.empty?
473:         else
474:           reload  = options
475:           options = {}
476:         end
477:         memoize(reload) do
478: ,, options, &block)
479:         end
480:       end