Class: AWS::S3::BucketCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws/s3/bucket_collection.rb

Overview

Represents a collection of buckets.

You can use this to create a bucket:

s3.buckets.create(:name => "mybucket")

You can get a handle for a specific bucket with indifferent access:

bucket = s3.buckets[:mybucket]
bucket = s3.buckets['mybucket']

You can also use it to find out which buckets are in your account:

s3.buckets.collect(&:name)
#=> ['bucket1', 'bucket2', ...]

Instance Method Summary collapse

Instance Method Details

#[](bucket_name) ⇒ Bucket

Returns the Bucket with the given name.

Makes no requests. The returned bucket object can be used to make requets for the bucket and its objects.

Examples:


bucket = s3.buckets[:mybucket],
bucket = s3.buckets['mybucket'],

Parameters:

  • bucket_name (String)

Returns:



92
93
94
# File 'lib/aws/s3/bucket_collection.rb', line 92

def [] bucket_name
  bucket_named(bucket_name)
end

#create(bucket_name, options = {}) ⇒ Bucket

Creates and returns a new Bucket. For example:

Examples:


bucket = s3.buckets.create('my-bucket')
bucket.name    #=> "my-bucket"
bucket.exists? #=> true

Parameters:

  • bucket_name (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :location_constraint (String) — default: nil

    The location where the bucket should be created. Defaults to the classic US region.

  • :acl (String) — default: :private

    Sets the ACL of the bucket you are creating. Valid Values include :private, :public_read, :public_read_write, :authenticated_read, :bucket_owner_read and :bucket_owner_full_control

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aws/s3/bucket_collection.rb', line 60

def create(bucket_name, options = {})

  # auto set the location constraint for the user if it is not 
  # passed in and the endpoint is not the us-standard region.  don't 
  # override the location constraint though, even it is wrong, 
  unless 
    config.s3_endpoint == 's3.amazonaws.com' or
    options[:location_constraint]
  then
    options[:location_constraint] = case config.s3_endpoint
    when 's3-eu-west-1.amazonaws.com' then 'EU'
    else config.s3_endpoint.match(/^s3-(.*)\.amazonaws\.com$/)[1]
    end
  end

  client.create_bucket(options.merge(:bucket_name => bucket_name))
  bucket_named(bucket_name)

end

#each(&block) ⇒ nil

Iterates the buckets in this collection.

Examples:


s3.buckets.each do |bucket|
  puts bucket.name
end

Returns:

  • (nil)


105
106
107
108
109
110
111
# File 'lib/aws/s3/bucket_collection.rb', line 105

def each &block
  response = client.list_buckets
  response.buckets.each do |b|
    yield(bucket_named(b.name, response.owner))
  end
  nil
end