Class: RightAws::S3::Bucket
- Inherits:
-
Object
- Object
- RightAws::S3::Bucket
- Defined in:
- lib/s3/right_s3.rb
Instance Attribute Summary collapse
-
#creation_date ⇒ Object
readonly
Returns the value of attribute creation_date.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#s3 ⇒ Object
readonly
Returns the value of attribute s3.
Class Method Summary collapse
-
.create(s3, name, create = false, perms = nil, headers = {}) ⇒ Object
Create a Bucket instance.
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all keys from a bucket.
-
#copy_key(old_key_or_name, new_key_or_name) ⇒ Object
Create an object copy.
-
#delete(options = {}) ⇒ Object
Delete a bucket.
-
#delete_folder(folder, separator = '/') ⇒ Object
Delete all keys where the ‘folder_key’ can be interpreted as a ‘folder’ name.
-
#disable_logging ⇒ Object
Disables S3 server access logging on a bucket.
-
#enable_logging(params) ⇒ Object
Enables S3 server access logging on a bucket.
-
#get(key, headers = {}) ⇒ Object
Retrieve data object from Amazon.
-
#grantees ⇒ Object
Return a list of grantees.
-
#initialize(s3, name, creation_date = nil, owner = nil) ⇒ Bucket
constructor
Create a bucket instance.
-
#key(key_name, head = false, &blck) ⇒ Object
Retrieve key information from Amazon.
-
#keys(options = {}, head = false) ⇒ Object
Retrieve a group of keys from Amazon.
-
#keys_and_service(options = {}, head = false) ⇒ Object
Same as
keys
method but return an array of [keys, service_data]. -
#location ⇒ Object
Returns the bucket location.
-
#logging_info ⇒ Object
Retrieves the logging configuration for a bucket.
-
#move_key(old_key_or_name, new_key_or_name) ⇒ Object
Move an object to other location.
-
#public_link ⇒ Object
Return a public link to bucket.
-
#put(key, data = nil, meta_headers = {}, perms = nil, headers = {}, &blck) ⇒ Object
Store object data.
-
#rename_key(old_key_or_name, new_name) ⇒ Object
Rename object.
-
#to_s ⇒ Object
(also: #full_name)
Return bucket name as a String.
Constructor Details
#initialize(s3, name, creation_date = nil, owner = nil) ⇒ Bucket
Create a bucket instance. In normal use this method should not be called directly. Use RightAws::S3::Bucket.create or RightAws::S3.bucket instead.
152 153 154 155 156 157 158 159 160 |
# File 'lib/s3/right_s3.rb', line 152 def initialize(s3, name, creation_date=nil, owner=nil) @s3 = s3 @name = name @owner = owner @creation_date = creation_date if @creation_date && !@creation_date.is_a?(Time) @creation_date = Time.parse(@creation_date) end end |
Instance Attribute Details
#creation_date ⇒ Object (readonly)
Returns the value of attribute creation_date.
122 123 124 |
# File 'lib/s3/right_s3.rb', line 122 def creation_date @creation_date end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
122 123 124 |
# File 'lib/s3/right_s3.rb', line 122 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
122 123 124 |
# File 'lib/s3/right_s3.rb', line 122 def owner @owner end |
#s3 ⇒ Object (readonly)
Returns the value of attribute s3.
122 123 124 |
# File 'lib/s3/right_s3.rb', line 122 def s3 @s3 end |
Class Method Details
.create(s3, name, create = false, perms = nil, headers = {}) ⇒ Object
Create a Bucket instance. If the bucket does not exist and create
is set, a new bucket is created on S3. Launching this method with create
=true
may affect on the bucket’s ACL if the bucket already exists. Returns Bucket instance or nil
if the bucket does not exist and create
is not set.
s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
...
bucket1 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_1')
bucket1.keys #=> exception here if the bucket does not exists
...
bucket2 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_2', true)
bucket2.keys #=> list of keys
# create a bucket at the European location with public read access
bucket3 = RightAws::S3::Bucket.create(s3,'my-awesome-bucket-3', true, 'public-read', :location => :eu)
see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
(section: Canned Access Policies)
144 145 146 |
# File 'lib/s3/right_s3.rb', line 144 def self.create(s3, name, create=false, perms=nil, headers={}) s3.bucket(name, create, perms, headers) end |
Instance Method Details
#clear ⇒ Object
Remove all keys from a bucket. Returns true
.
bucket.clear #=> true
342 343 344 |
# File 'lib/s3/right_s3.rb', line 342 def clear @s3.interface.clear_bucket(@name) end |
#copy_key(old_key_or_name, new_key_or_name) ⇒ Object
Create an object copy. Returns a destination RightAws::S3::Key instance.
new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
puts key.name #=> 'logs/today/2.log'
key.exists? #=> true
321 322 323 324 |
# File 'lib/s3/right_s3.rb', line 321 def copy_key(old_key_or_name, new_key_or_name) old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key) old_key_or_name.copy(new_key_or_name) end |
#delete(options = {}) ⇒ Object
Delete a bucket. Bucket must be empty. If force
is set, clears and deletes the bucket. Returns true
.
bucket.delete(:force => true) #=> true
363 364 365 366 |
# File 'lib/s3/right_s3.rb', line 363 def delete(={}) force = .is_a?(Hash) && [:force]==true force ? @s3.interface.force_delete_bucket(@name) : @s3.interface.delete_bucket(@name) end |
#delete_folder(folder, separator = '/') ⇒ Object
Delete all keys where the ‘folder_key’ can be interpreted as a ‘folder’ name. Returns an array of string keys that have been deleted.
bucket.keys.map{|key| key.name}.join(', ') #=> 'test, test/2/34, test/3, test1, test1/logs'
bucket.delete_folder('test') #=> ['test','test/2/34','test/3']
353 354 355 |
# File 'lib/s3/right_s3.rb', line 353 def delete_folder(folder, separator='/') @s3.interface.delete_folder(@name, folder, separator) end |
#disable_logging ⇒ Object
Disables S3 server access logging on a bucket. Takes no arguments.
211 212 213 214 |
# File 'lib/s3/right_s3.rb', line 211 def disable_logging xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"></BucketLoggingStatus>" @s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc) end |
#enable_logging(params) ⇒ Object
Enables S3 server access logging on a bucket. The target bucket must have been properly configured to receive server access logs.
Params:
:targetbucket - either the target bucket object or the name of the target bucket
:targetprefix - the prefix under which all logs should be stored
bucket.enable_logging(:targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/")
=> true
203 204 205 206 207 208 |
# File 'lib/s3/right_s3.rb', line 203 def enable_logging(params) AwsUtils.mandatory_arguments([:targetbucket, :targetprefix], params) AwsUtils.allow_only([:targetbucket, :targetprefix], params) xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><LoggingEnabled><TargetBucket>#{params[:targetbucket]}</TargetBucket><TargetPrefix>#{params[:targetprefix]}</TargetPrefix></LoggingEnabled></BucketLoggingStatus>" @s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc) end |
#get(key, headers = {}) ⇒ Object
Retrieve data object from Amazon. The key
is a String
or Key. Returns String instance.
data = bucket.get('logs/today/1.log') #=>
puts data #=> 'sasfasfasdf'
298 299 300 301 |
# File 'lib/s3/right_s3.rb', line 298 def get(key, headers={}) key = Key.create(self, key.to_s) unless key.is_a?(Key) key.get(headers) end |
#grantees ⇒ Object
Return a list of grantees.
370 371 372 |
# File 'lib/s3/right_s3.rb', line 370 def grantees Grantee::grantees(self) end |
#key(key_name, head = false, &blck) ⇒ Object
Retrieve key information from Amazon. The key_name
is a String
or Key instance. Retrieves meta-header information if head
is true
. Returns new Key instance.
key = bucket.key('logs/today/1.log', true) #=> #<RightAws::S3::Key:0xb7b1e240 ... >
# is the same as:
key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
key.head
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/s3/right_s3.rb', line 262 def key(key_name, head=false, &blck) raise 'Key name can not be empty.' if key_name.right_blank? key_instance = nil # if this key exists - find it .... keys({'prefix'=>key_name}, head).each do |key| blck.call if block_given? if key.name == key_name.to_s key_instance = key break end end # .... else this key is unknown unless key_instance key_instance = Key.create(self, key_name.to_s) end key_instance end |
#keys(options = {}, head = false) ⇒ Object
Retrieve a group of keys from Amazon. options
is a hash: { ‘prefix’=>”, ‘marker’=>”, ‘max-keys’=>5, ‘delimiter’=>” }). Retrieves meta-headers information if head
it true
. Returns an array of Key instances.
bucket.keys #=> # returns all keys from bucket
bucket.keys('prefix' => 'logs') #=> # returns all keys that starts with 'logs'
224 225 226 |
# File 'lib/s3/right_s3.rb', line 224 def keys(={}, head=false) keys_and_service(, head)[0] end |
#keys_and_service(options = {}, head = false) ⇒ Object
Same as keys
method but return an array of [keys, service_data]. where service_data
is a hash with additional output information.
keys, service = bucket.keys_and_service({'max-keys'=> 2, 'prefix' => 'logs'})
p keys #=> # 2 keys array
p service #=> {"max-keys"=>"2", "prefix"=>"logs", "name"=>"my_awesome_bucket", "marker"=>"", "is_truncated"=>true, :common_prefixes=>[]}
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/s3/right_s3.rb', line 235 def keys_and_service(={}, head=false) opt = {}; .each{ |key, value| opt[key.to_s] = value } service = {} keys = [] @s3.interface.incrementally_list_bucket(@name, opt) do |_service| service = _service service[:contents].each do |entry| owner = Owner.new(entry[:owner_id], entry[:owner_display_name]) key = Key.new(self, entry[:key], nil, {}, {}, entry[:last_modified], entry[:e_tag], entry[:size], entry[:storage_class], owner) key.head if head keys << key end end service.delete(:contents) [keys, service] end |
#location ⇒ Object
Returns the bucket location
182 183 184 |
# File 'lib/s3/right_s3.rb', line 182 def location @location ||= @s3.interface.bucket_location(@name) end |
#logging_info ⇒ Object
Retrieves the logging configuration for a bucket. Returns a hash of :targetbucket, :targetprefix
bucket.logging_info()
=> {:enabled=>true, :targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/"}
191 192 193 |
# File 'lib/s3/right_s3.rb', line 191 def logging_info @s3.interface.get_logging_parse(:bucket => @name) end |
#move_key(old_key_or_name, new_key_or_name) ⇒ Object
Move an object to other location. Returns a destination RightAws::S3::Key instance.
new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
puts key.name #=> 'logs/today/2.log'
key.exists? #=> true
332 333 334 335 |
# File 'lib/s3/right_s3.rb', line 332 def move_key(old_key_or_name, new_key_or_name) old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key) old_key_or_name.move(new_key_or_name) end |
#public_link ⇒ Object
Return a public link to bucket.
bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
176 177 178 179 |
# File 'lib/s3/right_s3.rb', line 176 def public_link params = @s3.interface.params "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name}" end |
#put(key, data = nil, meta_headers = {}, perms = nil, headers = {}, &blck) ⇒ Object
Store object data. The key
is a String
or Key instance. Returns true
.
bucket.put('logs/today/1.log', 'Olala!') #=> true
286 287 288 289 |
# File 'lib/s3/right_s3.rb', line 286 def put(key, data=nil, ={}, perms=nil, headers={}, &blck) key = Key.create(self, key.to_s, data, ) unless key.is_a?(Key) key.put(data, perms, headers, &blck) end |
#rename_key(old_key_or_name, new_name) ⇒ Object
Rename object. Returns RightAws::S3::Key instance.
new_key = bucket.rename_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
puts key.name #=> 'logs/today/2.log'
key.exists? #=> true
309 310 311 312 313 |
# File 'lib/s3/right_s3.rb', line 309 def rename_key(old_key_or_name, new_name) old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key) old_key_or_name.rename(new_name) old_key_or_name end |