class AWS::S3::ObjectCollection
#=> [‘photos/2010’, ‘photos/2011’]
directories = tree.children.select(&:branch?).collect(&:prefix)
tree = bucket.objects.with_prefix(‘photos’).as_tree
photos/2011/summer/family.jpg
photos/2011/summer/vacation.jpg
photos/2011/fall/leaves.jpg
photos/2010/house.jpg
videos/family_reunion.mpg
videos/wedding.mpg
README.txt
Given a bucket with the following keys:
== Exploring Objects with a Tree Interface
#=> [‘videos/comedy.mpg’, ‘videos/dancing.mpg’]
bucket.objects.with_prefix(‘videos’).collect(&:key)
You can list objects that share a prefix:
videos/dancing.mpg
videos/comedy.mpg
photos/winter.jpg
photos/sunrise.jpg
photos/sunset.jpg
Given a bucket with the following keys:
== Finding objects with a Prefix
object.key #=> ‘foo.jpg’
object = bucket.objects[‘foo.jpg’]
# this will not make any requests against S3
If you know the key of the object you want, you can reference it this way:
== Getting an S3Object by Key
Represents a collection of S3 objects.
def [] key
-
(S3Object)
-
Parameters:
-
key
(String
) -- The object key.
def [] key S3Object.new(bucket, key.to_s) end
def create key, *args
-
(S3Object)
-
Parameters:
-
key
(String
) -- Where in S3 to write the object.
Other tags:
- See: S3Object#write -
def create key, *args self[key].write(*args) end
def delete *objects
-
(nil)
-
Raises:
-
(BatchDeleteError)
- If any of the objects failed to delete,
Parameters:
-
objects
(Mixed
) -- One or more objects to delete. Each object
def delete *objects objects = objects.flatten.collect do |obj| case obj when String then { :key => obj } when Hash then obj when S3Object then { :key => obj.key } when ObjectVersion then { :key => obj.key, :version_id => obj.version_id } else msg = "objects must be keys (strings or hashes with :key and " + ":version_id), S3Objects or ObjectVersions, got " + object.class.name raise ArgumentError, msg end end batch_helper = BatchHelper.new(1000) do |batch| client_opts = {} client_opts[:bucket_name] = bucket.name client_opts[:quiet] = true client_opts[:objects] = batch client.delete_objects(client_opts) end error_counts = {} batch_helper.after_batch do |response| response.errors.each do |error| error_counts[error.code] ||= 0 error_counts[error.code] += 1 end end objects.each do |object| batch_helper.add(object) end batch_helper.complete! raise Errors::BatchDeleteError.new(error_counts) unless error_counts.empty? nil end
def delete_all
-
(Array)
- Returns an array of results
Raises:
-
(BatchDeleteError)
- If any of the objects failed to delete,
Other tags:
- Example: Delete objects with a given prefix -
Example: Delete all objects from a bucket -
def delete_all error_counts = {} each_batch do |objects| begin delete(objects) rescue Errors::BatchDeleteError => error error.error_counts.each_pair do |code,count| error_counts[code] ||= 0 error_counts[code] += count end end end raise Errors::BatchDeleteError.new(error_counts) unless error_counts.empty? nil end
def delete_if &block
-
(BatchDeleteError)
- If any of the objects failed to delete,
Other tags:
- Yieldparam: object -
def delete_if &block error_counts = {} batch_helper = BatchHelper.new(1000) do |objects| begin delete(objects) rescue Errors::BatchDeleteError => error error.error_counts.each_pair do |code,count| error_counts[code] ||= 0 error_counts[code] += count end end end each do |object| batch_helper.add(object) if yield(object) end batch_helper.complete! raise Errors::BatchDeleteError.new(error_counts) unless error_counts.empty? nil end
def each options = {}, &block
-
(nil)
-
Options Hash:
(**options)
-
:batch_size
(Integer
) -- The number of objects to -
:limit
(Integer
) -- The maximum number of
Parameters:
-
options
(Hash
) --
def each options = {}, &block super end
def each_member_in_page(page, &block)
def each_member_in_page(page, &block) super page.contents.each do |content| yield(S3Object.new(bucket, content.key)) end end
def initialize(bucket, options = {})
-
The
(Bucket
) -- S3 bucket this object collection belongs to.
def initialize(bucket, options = {}) @bucket = bucket super end
def limit_param
def limit_param :max_keys end
def list_request options
def list_request options client.list_objects(options) end
def next_markers page
def next_markers page marker = (last = page.contents.last and last.key) if marker.nil? raise 'Unable to find marker in S3 list objects response' else { :marker => marker } end end
def with_prefix prefix, mode = :replace
def with_prefix prefix, mode = :replace super(prefix, mode) end