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 = true, perms = nil, headers = {}) ⇒ Object
s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key) …
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all keys from a bucket.
-
#delete(force = false) ⇒ Object
Delete a bucket.
-
#delete_folder(folder, separator = '/') ⇒ Object
Returns an array of string keys that have been deleted.
-
#get(key, headers = {}) ⇒ Object
Retrieve object data from Amazon.
-
#grantees ⇒ Object
Return a list of grantees.
-
#initialize(s3, name, creation_date = nil, owner = nil) ⇒ Bucket
constructor
Use RightAws::S3::Bucket.create or RightAws::S3.bucket instead.
-
#key(key_name, head = false) ⇒ 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]. -
#public_link ⇒ Object
Return a public link to bucket.
-
#put(key, data = nil, meta_headers = {}, perms = nil, headers = {}) ⇒ Object
Store object data.
-
#to_s ⇒ Object
(also: #full_name)
Return bucket name as a String.
Constructor Details
#initialize(s3, name, creation_date = nil, owner = nil) ⇒ Bucket
Use RightAws::S3::Bucket.create or RightAws::S3.bucket instead.
112 113 114 115 116 117 118 119 120 |
# File 'lib/s3/right_s3.rb', line 112 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.
87 88 89 |
# File 'lib/s3/right_s3.rb', line 87 def creation_date @creation_date end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
87 88 89 |
# File 'lib/s3/right_s3.rb', line 87 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
87 88 89 |
# File 'lib/s3/right_s3.rb', line 87 def owner @owner end |
#s3 ⇒ Object (readonly)
Returns the value of attribute s3.
87 88 89 |
# File 'lib/s3/right_s3.rb', line 87 def s3 @s3 end |
Class Method Details
.create(s3, name, create = true, perms = nil, headers = {}) ⇒ Object
104 105 106 |
# File 'lib/s3/right_s3.rb', line 104 def self.create(s3, name, create=true, 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
234 235 236 |
# File 'lib/s3/right_s3.rb', line 234 def clear @s3.interface.clear_bucket(@name) end |
#delete(force = false) ⇒ Object
Delete a bucket. Bucket must be empty. If force
is set, clears and deletes the bucket. Returns true
.
bucket.delete(true) #=> true
255 256 257 |
# File 'lib/s3/right_s3.rb', line 255 def delete(force=false) force ? @s3.interface.force_delete_bucket(@name) : @s3.interface.delete_bucket(@name) end |
#delete_folder(folder, separator = '/') ⇒ Object
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']
245 246 247 |
# File 'lib/s3/right_s3.rb', line 245 def delete_folder(folder, separator='/') @s3.interface.delete_folder(@name, folder, separator) end |
#get(key, headers = {}) ⇒ Object
Retrieve object data from Amazon. The key
is a String
or Key. Returns Key instance.
key = bucket.get('logs/today/1.log') #=>
puts key.data #=> 'sasfasfasdf'
224 225 226 227 |
# File 'lib/s3/right_s3.rb', line 224 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.
261 262 263 |
# File 'lib/s3/right_s3.rb', line 261 def grantees Grantee::grantees(self) end |
#key(key_name, head = false) ⇒ 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
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/s3/right_s3.rb', line 189 def key(key_name, head=false) raise 'Key name can not be empty.' if key_name.blank? key_instance = nil # if this key exists - find it .... keys({'prefix'=>key_name}, head).each do |key| 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'
149 150 151 |
# File 'lib/s3/right_s3.rb', line 149 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}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/s3/right_s3.rb', line 160 def keys_and_service(={}, head=false) opt = {}; .each{ |key, value| opt[key.to_s] = value } service_data = {} thislist = {} list = [] @s3.interface.incrementally_list_bucket(@name, opt) do |thislist| thislist[: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 list << key end end thislist.each_key do |key| service_data[key] = thislist[key] unless (key == :contents || key == :common_prefixes) end [list, service_data] end |
#public_link ⇒ Object
Return a public link to bucket.
bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
136 137 138 139 |
# File 'lib/s3/right_s3.rb', line 136 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 = {}) ⇒ Object
Store object data. The key
is a String
or Key instance. Returns true
.
bucket.put('logs/today/1.log', 'Olala!') #=> true
212 213 214 215 |
# File 'lib/s3/right_s3.rb', line 212 def put(key, data=nil, ={}, perms=nil, headers={}) key = Key.create(self, key.to_s, data, ) unless key.is_a?(Key) key.put(data, perms, headers) end |