Class: AWS::S3::Bucket
- Inherits:
-
Object
- Object
- AWS::S3::Bucket
- Includes:
- Core::Model
- Defined in:
- lib/aws/s3/bucket.rb
Overview
Represents a single S3 bucket.
Defined Under Namespace
Modules: ACLProxy, PolicyProxy
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The bucket name.
Attributes included from Core::Model
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if the two buckets have the same name.
-
#acl ⇒ AccessControlList
Returns the bucket’s access control list.
-
#acl=(acl) ⇒ nil
Sets the bucket’s access control list.
-
#as_tree(options = {}) ⇒ Tree
Returns a tree that allows you to expose the bucket contents like a directory structure.
-
#clear! ⇒ nil
Deletes all objects from this bucket.
-
#delete ⇒ nil
Deletes the current bucket.
-
#delete! ⇒ nil
Deletes all objects in a bucket and then deletes the bucket.
-
#empty? ⇒ Boolean
Returns true if the bucket has no objects (this includes versioned objects that are delete markers).
-
#enable_versioning ⇒ nil
Enables versioning on this bucket.
-
#eql?(other_bucket) ⇒ Boolean
Returns true if the two buckets have the same name.
-
#exists? ⇒ Boolean
Returns true if the bucket exists in S3.
-
#initialize(name, options = {}) ⇒ Bucket
constructor
A new instance of Bucket.
- #inspect ⇒ Object
-
#lifecycle_configuration ⇒ BucketLifecycleConfiguration
The primary interface for editing the lifecycle configuration.
-
#lifecycle_configuration=(config) ⇒ nil
You can call this method if you prefer to build your own lifecycle configuration.
-
#location_constraint ⇒ String?
Returns the location constraint for a bucket (if it has one), nil otherwise.
-
#multipart_uploads ⇒ MultipartUploadCollection
Represents all of the multipart uploads that are in progress for this bucket.
-
#objects ⇒ ObjectCollection
Represents all objects(keys) in this bucket.
-
#owner ⇒ String
Bucket owner id.
-
#policy ⇒ Policy?
Returns the bucket policy.
-
#policy=(policy) ⇒ nil
Sets the bucket’s policy.
-
#presigned_post(options = {}) ⇒ Object
Generates fields for a presigned POST to this object.
-
#suspend_versioning ⇒ nil
Suspends versioning on this bucket.
-
#url ⇒ String
Returns the url for this bucket.
-
#versioning_enabled? ⇒ Boolean
(also: #versioned?)
Returns
true
if version is enabled on this bucket. -
#versioning_state ⇒ Symbol
Returns the versioning status for this bucket.
-
#versions ⇒ BucketVersionCollection
Represents all of the versioned objects stored in this bucket.
Methods included from Core::Model
Constructor Details
#initialize(name, options = {}) ⇒ Bucket
Returns a new instance of Bucket.
34 35 36 37 38 39 40 41 |
# File 'lib/aws/s3/bucket.rb', line 34 def initialize(name, = {}) # 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 = [:owner] super end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns The bucket name.
44 45 46 |
# File 'lib/aws/s3/bucket.rb', line 44 def name @name end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the two buckets have the same name.
137 138 139 |
# File 'lib/aws/s3/bucket.rb', line 137 def ==(other) other.kind_of?(Bucket) && other.name == name end |
#acl ⇒ AccessControlList
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
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/aws/s3/bucket.rb', line 204 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 access control list. acl
can be:
-
An XML policy as a string (which is passed to S3 uninterpreted)
-
An AccessControlList object
-
Any object that responds to
to_xml
-
Any Hash that is acceptable as an argument to AccessControlList#initialize.
225 226 227 228 |
# File 'lib/aws/s3/bucket.rb', line 225 def acl=(acl) client.set_bucket_acl(:bucket_name => name, :acl => acl) nil end |
#as_tree(options = {}) ⇒ Tree
Returns a tree that allows you to expose the bucket contents like a directory structure.
378 379 380 |
# File 'lib/aws/s3/bucket.rb', line 378 def as_tree = {} objects.as_tree() end |
#clear! ⇒ nil
Deletes all objects from this bucket.
105 106 107 108 109 |
# File 'lib/aws/s3/bucket.rb', line 105 def clear! versions.each_batch do |versions| objects.delete(versions) end end |
#delete ⇒ nil
Deletes the current bucket. An error will be raised if the bucket is not empty.
114 115 116 117 |
# File 'lib/aws/s3/bucket.rb', line 114 def delete client.delete_bucket(:bucket_name => @name) nil end |
#delete! ⇒ nil
Deletes all objects in a bucket and then deletes the bucket.
121 122 123 124 |
# File 'lib/aws/s3/bucket.rb', line 121 def delete! clear! delete end |
#empty? ⇒ Boolean
Returns true if the bucket has no objects (this includes versioned objects that are delete markers).
58 59 60 |
# File 'lib/aws/s3/bucket.rb', line 58 def empty? versions.first ? false : true end |
#enable_versioning ⇒ nil
Enables versioning on this bucket.
70 71 72 73 74 75 |
# File 'lib/aws/s3/bucket.rb', line 70 def enable_versioning client.set_bucket_versioning( :bucket_name => @name, :state => :enabled) nil end |
#eql?(other_bucket) ⇒ Boolean
Returns true if the two buckets have the same name
142 143 144 |
# File 'lib/aws/s3/bucket.rb', line 142 def eql?(other_bucket) self == other_bucket end |
#exists? ⇒ Boolean
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.
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/aws/s3/bucket.rb', line 149 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 |
#inspect ⇒ Object
132 133 134 |
# File 'lib/aws/s3/bucket.rb', line 132 def inspect "#<AWS::S3::Bucket:#{name}>" end |
#lifecycle_configuration ⇒ BucketLifecycleConfiguration
The primary interface for editing the lifecycle configuration. See AWS::S3::BucketLifecycleConfiguration for more information.
307 308 309 |
# File 'lib/aws/s3/bucket.rb', line 307 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 = <<-XML
<LifecycleConfiguration>
...
</LifecycleConfiguration>
XML
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.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/aws/s3/bucket.rb', line 334 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_constraint ⇒ String?
Returns the location constraint for a bucket (if it has one), nil otherwise.
64 65 66 |
# File 'lib/aws/s3/bucket.rb', line 64 def location_constraint client.get_bucket_location(:bucket_name => name).location_constraint end |
#multipart_uploads ⇒ MultipartUploadCollection
Returns Represents all of the multipart uploads that are in progress for this bucket.
177 178 179 |
# File 'lib/aws/s3/bucket.rb', line 177 def multipart_uploads MultipartUploadCollection.new(self) end |
#objects ⇒ ObjectCollection
Returns Represents all objects(keys) in this bucket.
165 166 167 |
# File 'lib/aws/s3/bucket.rb', line 165 def objects ObjectCollection.new(self) end |
#owner ⇒ String
Returns bucket owner id.
127 128 129 |
# File 'lib/aws/s3/bucket.rb', line 127 def owner @owner || client.list_buckets.owner end |
#policy ⇒ Policy?
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.
269 270 271 272 273 274 275 276 277 |
# File 'lib/aws/s3/bucket.rb', line 269 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.
286 287 288 289 |
# File 'lib/aws/s3/bucket.rb', line 286 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.
386 387 388 |
# File 'lib/aws/s3/bucket.rb', line 386 def presigned_post( = {}) PresignedPost.new(self, ) end |
#suspend_versioning ⇒ nil
Suspends versioning on this bucket.
79 80 81 82 83 84 |
# File 'lib/aws/s3/bucket.rb', line 79 def suspend_versioning client.set_bucket_versioning( :bucket_name => @name, :state => :suspended) nil end |
#url ⇒ String
Returns the url for this bucket.
48 49 50 51 52 53 54 |
# File 'lib/aws/s3/bucket.rb', line 48 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?
Returns true
if version is enabled on this bucket.
87 88 89 |
# File 'lib/aws/s3/bucket.rb', line 87 def versioning_enabled? versioning_state == :enabled end |
#versioning_state ⇒ Symbol
Returns the versioning status for this bucket. States include:
-
:enabled
- currently enabled -
:suspended
- currently suspended -
:unversioned
- versioning has never been enabled
99 100 101 |
# File 'lib/aws/s3/bucket.rb', line 99 def versioning_state client.get_bucket_versioning(:bucket_name => @name).status end |
#versions ⇒ BucketVersionCollection
Returns Represents all of the versioned objects stored in this bucket.
171 172 173 |
# File 'lib/aws/s3/bucket.rb', line 171 def versions BucketVersionCollection.new(self) end |