Class: AWS::S3::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/s3/bucket.rb

Overview

Represents a single S3 bucket.

Examples:

Creating a Bucket


bucket = s3.buckets.create('mybucket')

Getting an Existing Bucket


bucket = s3.buckets['mybucket']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Bucket

Returns a new instance of Bucket.

Options Hash (options):

  • :owner (String) — default: nil

    The owner id of this bucket.



35
36
37
38
39
40
41
42
# File 'lib/aws/s3/bucket.rb', line 35

def initialize(name, options = {})
  # the S3 docs disagree with what the service allows,
  # so it's not safe to toss out invalid bucket names
  # S3::Client.validate_bucket_name!(name)
  @name = name
  @owner = options[:owner]
  super
end

Instance Attribute Details

#nameString (readonly)



45
46
47
# File 'lib/aws/s3/bucket.rb', line 45

def name
  @name
end

Instance Method Details

#==(other) ⇒ Boolean



138
139
140
# File 'lib/aws/s3/bucket.rb', line 138

def ==(other)
  other.kind_of?(Bucket) && other.name == name
end

#aclAccessControlList

Returns the bucket’s access control list. This will be an instance of AccessControlList, plus an additional change method:

bucket.acl.change do |acl|
  acl.grants.reject! do |g|
    g.grantee.canonical_user_id != bucket.owner.id
  end
end


205
206
207
208
209
210
211
212
213
214
# File 'lib/aws/s3/bucket.rb', line 205

def acl

  resp = client.get_bucket_acl(:bucket_name => name)

  acl = AccessControlList.new(resp.data)
  acl.extend ACLProxy
  acl.bucket = self
  acl

end

#acl=(acl) ⇒ nil

Sets the bucket’s ACL (access control list). You can provide an ACL in a number of different formats.



220
221
222
223
# File 'lib/aws/s3/bucket.rb', line 220

def acl= acl
  client.set_bucket_acl(acl_options(acl).merge(:bucket_name => name))
  nil
end

#as_tree(options = {}) ⇒ Tree

Returns a tree that allows you to expose the bucket contents like a directory structure.

Options Hash (options):

  • :prefix (String) — default: nil

    Set prefix to choose where the top of the tree will be. A value of nil means that the tree will include all objects in the collection.

  • :delimiter (String) — default: '/'

    The string that separates each level of the tree. This is usually a directory separator.

  • :append (Boolean) — default: true

    If true, the delimiter is appended to the prefix when the prefix does not already end with the delimiter.

See Also:



373
374
375
# File 'lib/aws/s3/bucket.rb', line 373

def as_tree options = {}
  objects.as_tree(options)
end

#clear!nil

Deletes all objects from this bucket.



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

def clear!
  versions.each_batch do |versions|
    objects.delete(versions)
  end
end

#deletenil

Deletes the current bucket. An error will be raised if the bucket is not empty.



115
116
117
118
# File 'lib/aws/s3/bucket.rb', line 115

def delete
  client.delete_bucket(:bucket_name => @name)
  nil
end

#delete!nil

Deletes all objects in a bucket and then deletes the bucket.



122
123
124
125
# File 'lib/aws/s3/bucket.rb', line 122

def delete!
  clear!
  delete
end

#empty?Boolean



59
60
61
# File 'lib/aws/s3/bucket.rb', line 59

def empty?
  versions.first ? false : true
end

#enable_versioningnil

Enables versioning on this bucket.



71
72
73
74
75
76
# File 'lib/aws/s3/bucket.rb', line 71

def enable_versioning
  client.set_bucket_versioning(
    :bucket_name => @name,
    :state => :enabled)
  nil
end

#eql?(other_bucket) ⇒ Boolean



143
144
145
# File 'lib/aws/s3/bucket.rb', line 143

def eql?(other_bucket)
  self == other_bucket
end

#exists?Boolean

Note:

This method only indicates if there is a bucket in S3, not if you have permissions to work with the bucket or not.

Returns true if the bucket exists in S3.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/aws/s3/bucket.rb', line 150

def exists?
  begin
    versioned? # makes a get bucket request without listing contents
               # raises a client error if the bucket doesn't exist or
               # if you don't have permission to get the bucket 
               # versioning status.
    true
  rescue Errors::NoSuchBucket => e
    false # bucket does not exist
  rescue Errors::ClientError => e
    true # bucket exists
  end
end

#lifecycle_configurationBucketLifecycleConfiguration

The primary interface for editing the lifecycle configuration. See AWS::S3::BucketLifecycleConfiguration for more information.

Examples:

Adding rules to a bucket’s lifecycle configuration


bucket.lifecycle_configuration.update do
  add_rule 'cache-1/', 30
  add_rule 'cache-2/', 30
end

Deleting the lifecycle configuration


bucket.lifecycle_configuration.clear


302
303
304
# File 'lib/aws/s3/bucket.rb', line 302

def lifecycle_configuration
  @lifecycle_cfg ||= BucketLifecycleConfiguration.new(self)
end

#lifecycle_configuration=(config) ⇒ nil

You can call this method if you prefer to build your own lifecycle configuration.

bucket.lifecycle_configuration = "  <LifecycleConfiguration>\n    ...\n  </LifecycleConfiguration>\n"

You can also use this method to copy a lifecycle configuration from another bucket.

bucket.lifecycle_configuration = other_bucket.lifecycle_configuration

If you call this method, passing nil, the lifecycle configuration for this bucket will be deleted.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/aws/s3/bucket.rb', line 329

def lifecycle_configuration= config

  if config.nil?

    client_opts = {}
    client_opts[:bucket_name] = name
    client.delete_bucket_lifecycle_configuration(client_opts)

    @lifecycle_cfg = BucketLifecycleConfiguration.new(self, :empty => true)

  else
  
    xml = config.is_a?(String) ? config : config.to_xml

    client_opts = {}
    client_opts[:bucket_name] = name
    client_opts[:lifecycle_configuration] = xml
    client.set_bucket_lifecycle_configuration(client_opts)

    @lifecycle_cfg = BucketLifecycleConfiguration.new(self, :xml => xml)

  end

  nil

end

#location_constraintString?



65
66
67
# File 'lib/aws/s3/bucket.rb', line 65

def location_constraint
  client.get_bucket_location(:bucket_name => name).location_constraint
end

#multipart_uploadsMultipartUploadCollection



178
179
180
# File 'lib/aws/s3/bucket.rb', line 178

def multipart_uploads
  MultipartUploadCollection.new(self)
end

#objectsObjectCollection



166
167
168
# File 'lib/aws/s3/bucket.rb', line 166

def objects
  ObjectCollection.new(self)
end

#ownerString



128
129
130
# File 'lib/aws/s3/bucket.rb', line 128

def owner
  @owner || client.list_buckets.owner
end

#policyPolicy?

Returns the bucket policy. This will be an instance of Policy. The returned policy will also have the methods of PolicyProxy mixed in, so you can use it to change the current policy or delete it, for example:

if policy = bucket.policy
  # add a statement
  policy.change do |p|
    p.allow(...)
  end

  # delete the policy
  policy.delete
end

Note that changing the policy is not an atomic operation; it fetches the current policy, yields it to the block, and then sets it again. Therefore, it’s possible that you may overwrite a concurrent update to the policy using this method.



264
265
266
267
268
269
270
271
272
# File 'lib/aws/s3/bucket.rb', line 264

def policy
  resp = client.get_bucket_policy(:bucket_name => name)
  policy = Policy.from_json(resp.data[:policy])
  policy.extend(PolicyProxy)
  policy.bucket = self
  policy
rescue Errors::NoSuchBucketPolicy => e
  nil
end

#policy=(policy) ⇒ nil

Sets the bucket’s policy.

See Also:



281
282
283
284
# File 'lib/aws/s3/bucket.rb', line 281

def policy=(policy)
  client.set_bucket_policy(:bucket_name => name, :policy => policy)
  nil
end

#presigned_post(options = {}) ⇒ Object

Generates fields for a presigned POST to this object. All options are sent to the PresignedPost constructor.

See Also:



381
382
383
# File 'lib/aws/s3/bucket.rb', line 381

def presigned_post(options = {})
  PresignedPost.new(self, options)
end

#suspend_versioningnil

Suspends versioning on this bucket.



80
81
82
83
84
85
# File 'lib/aws/s3/bucket.rb', line 80

def suspend_versioning
  client.set_bucket_versioning(
    :bucket_name => @name,
    :state => :suspended)
  nil
end

#urlString

Returns the url for this bucket.



49
50
51
52
53
54
55
# File 'lib/aws/s3/bucket.rb', line 49

def url
  if client.dns_compatible_bucket_name?(name)
    "http://#{name}.s3.amazonaws.com/"
  else
    "http://s3.amazonaws.com/#{name}/"
  end
end

#versioning_enabled?Boolean Also known as: versioned?



88
89
90
# File 'lib/aws/s3/bucket.rb', line 88

def versioning_enabled?
  versioning_state == :enabled
end

#versioning_stateSymbol

Returns the versioning status for this bucket. States include:

  • :enabled - currently enabled

  • :suspended - currently suspended

  • :unversioned - versioning has never been enabled



100
101
102
# File 'lib/aws/s3/bucket.rb', line 100

def versioning_state
  client.get_bucket_versioning(:bucket_name => @name).status
end

#versionsBucketVersionCollection



172
173
174
# File 'lib/aws/s3/bucket.rb', line 172

def versions
  BucketVersionCollection.new(self)
end