Class: RightAws::S3::Key
- Inherits:
-
Object
- Object
- RightAws::S3::Key
- Defined in:
- lib/s3/right_s3.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#data ⇒ Object
Return Key data.
-
#e_tag ⇒ Object
readonly
Returns the value of attribute e_tag.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
-
#meta_headers ⇒ Object
Returns the value of attribute meta_headers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#storage_class ⇒ Object
readonly
Returns the value of attribute storage_class.
Class Method Summary collapse
- .add_meta_prefix(meta_headers, prefix = S3Interface::AMAZON_METADATA_PREFIX) ⇒ Object
-
.create(bucket, name, data = nil, meta_headers = {}) ⇒ Object
Create a new Key instance, but do not create the actual key.
-
.split_meta(headers) ⇒ Object
Separate Amazon meta headers from other headers.
Instance Method Summary collapse
-
#delete ⇒ Object
Remove key from bucket.
-
#exists? ⇒ Boolean
Check for existence of the key in the given bucket.
-
#full_name(separator = '/') ⇒ Object
Return the full S3 path to this key (bucket/key).
-
#get(headers = {}) ⇒ Object
Retrieve object data and attributes from Amazon.
-
#grantees ⇒ Object
Return a list of grantees.
-
#head ⇒ Object
Retrieve meta-headers from S3.
-
#initialize(bucket, name, data = nil, headers = {}, meta_headers = {}, last_modified = nil, e_tag = nil, size = nil, storage_class = nil, owner = nil) ⇒ Key
constructor
Use RightAws::S3::Key.create or bucket.key() instead.
-
#public_link ⇒ Object
Return a public link to a key.
-
#put(data = nil, perms = nil, headers = {}) ⇒ Object
Store object data on S3.
-
#refresh(head = true) ⇒ Object
Retrieve key info from bucket and update attributes.
-
#to_s ⇒ Object
Return key name as a String.
Constructor Details
#initialize(bucket, name, data = nil, headers = {}, meta_headers = {}, last_modified = nil, e_tag = nil, size = nil, storage_class = nil, owner = nil) ⇒ Key
Use RightAws::S3::Key.create or bucket.key() instead.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/s3/right_s3.rb', line 316 def initialize(bucket, name, data=nil, headers={}, ={}, last_modified=nil, e_tag=nil, size=nil, storage_class=nil, owner=nil) raise 'Bucket must be a Bucket instance.' unless bucket.is_a?(Bucket) @bucket = bucket @name = name @data = data @e_tag = e_tag @size = size.to_i @storage_class = storage_class @owner = owner @last_modified = last_modified if @last_modified && !@last_modified.is_a?(Time) @last_modified = Time.parse(@last_modified) end @headers, = self.class.(headers) .merge!() end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def bucket @bucket end |
#data ⇒ Object
Return Key data. Retrieve this data from Amazon if it is the first time call. TODO TRB 6/19/07 What does the above mean? Clarify.
363 364 365 366 |
# File 'lib/s3/right_s3.rb', line 363 def data get if !@data and exists? @data end |
#e_tag ⇒ Object (readonly)
Returns the value of attribute e_tag.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def e_tag @e_tag end |
#headers ⇒ Object
Returns the value of attribute headers.
270 271 272 |
# File 'lib/s3/right_s3.rb', line 270 def headers @headers end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def last_modified @last_modified end |
#meta_headers ⇒ Object
Returns the value of attribute meta_headers.
270 271 272 |
# File 'lib/s3/right_s3.rb', line 270 def end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def owner @owner end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def size @size end |
#storage_class ⇒ Object (readonly)
Returns the value of attribute storage_class.
269 270 271 |
# File 'lib/s3/right_s3.rb', line 269 def storage_class @storage_class end |
Class Method Details
.add_meta_prefix(meta_headers, prefix = S3Interface::AMAZON_METADATA_PREFIX) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/s3/right_s3.rb', line 286 def self.(, prefix=S3Interface::AMAZON_METADATA_PREFIX) = {} .each do |, value| if [/#{prefix}/] [] = value else ["#{S3Interface::AMAZON_METADATA_PREFIX}#{meta_header}"] = value end end end |
.create(bucket, name, data = nil, meta_headers = {}) ⇒ Object
Create a new Key instance, but do not create the actual key. The name is a String. Returns a new Key instance.
key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
key.exists? #=> true | false
key.put('Woohoo!') #=> true
key.exists? #=> true
308 309 310 |
# File 'lib/s3/right_s3.rb', line 308 def self.create(bucket, name, data=nil, ={}) new(bucket, name, data, {}, ) end |
.split_meta(headers) ⇒ Object
Separate Amazon meta headers from other headers
274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/s3/right_s3.rb', line 274 def self.(headers) #:nodoc: hash = headers.dup = {} hash.each do |key, value| if key[/^#{S3Interface::AMAZON_METADATA_PREFIX}/] [key.gsub(S3Interface::AMAZON_METADATA_PREFIX,'')] = value hash.delete(key) end end [hash, ] end |
Instance Method Details
#delete ⇒ Object
Remove key from bucket. Returns true.
key.delete #=> true
452 453 454 455 |
# File 'lib/s3/right_s3.rb', line 452 def delete raise 'Key name must be specified.' if @name.blank? @bucket.s3.interface.delete(@bucket, @name) end |
#exists? ⇒ Boolean
Check for existence of the key in the given bucket. Returns true or false.
key = RightAws::S3::Key.create(bucket,'logs/today/1.log')
key.exists? #=> false
key.put('Woohoo!') #=> true
key.exists? #=> true
443 444 445 |
# File 'lib/s3/right_s3.rb', line 443 def exists? @bucket.key(self).last_modified ? true : false end |
#full_name(separator = '/') ⇒ Object
Return the full S3 path to this key (bucket/key).
key.full_name #=> 'my_awesome_bucket/cool_key'
347 348 349 |
# File 'lib/s3/right_s3.rb', line 347 def full_name(separator='/') "#{@bucket.to_s}#{separator}#{@name}" end |
#get(headers = {}) ⇒ Object
Retrieve object data and attributes from Amazon. Returns a String.
371 372 373 374 375 376 377 |
# File 'lib/s3/right_s3.rb', line 371 def get(headers={}) response = @bucket.s3.interface.get(@bucket.name, @name, headers) @data = response[:object] @headers, = self.class.(response[:headers]) refresh(false) @data end |
#grantees ⇒ Object
Return a list of grantees.
459 460 461 |
# File 'lib/s3/right_s3.rb', line 459 def grantees Grantee::grantees(self) end |
#head ⇒ Object
Retrieve meta-headers from S3. Returns true.
key. #=> {"family"=>"qwerty"}
key.head #=> true
key. #=> {"family"=>"qwerty", "name"=>"asdfg"}
430 431 432 433 |
# File 'lib/s3/right_s3.rb', line 430 def head @headers, = self.class.(@bucket.s3.interface.head(@bucket, @name)) true end |
#public_link ⇒ Object
Return a public link to a key.
key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
355 356 357 358 |
# File 'lib/s3/right_s3.rb', line 355 def public_link params = @bucket.s3.interface.params "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}" end |
#put(data = nil, perms = nil, headers = {}) ⇒ Object
Store object data on S3. Parameter data is a String or S3Object instance. Returns true.
key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
key.data = 'Qwerty'
key.put #=> true
...
key.put('Olala!') #=> true
389 390 391 392 393 394 |
# File 'lib/s3/right_s3.rb', line 389 def put(data=nil, perms=nil, headers={}) headers['x-amz-acl'] = perms if perms @data = data || @data = self.class.() @bucket.s3.interface.put(@bucket.name, @name, @data, .merge(headers)) end |
#refresh(head = true) ⇒ Object
Retrieve key info from bucket and update attributes. Refresh meta-headers (by calling head method) if head is set. Returns true if the key exists in bucket and false otherwise.
key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
key.e_tag #=> nil
key. #=> {}
key.refresh #=> true
key.e_tag #=> '12345678901234567890bf11094484b6'
key. #=> {"family"=>"qwerty", "name"=>"asdfg"}
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/s3/right_s3.rb', line 407 def refresh(head=true) new_key = @bucket.key(self) @last_modified = new_key.last_modified @e_tag = new_key.e_tag @size = new_key.size @storage_class = new_key.storage_class @owner = new_key.owner if @last_modified self.head true else @headers = = {} false end end |
#to_s ⇒ Object
Return key name as a String.
key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
puts key #=> 'logs/today/1.log'
339 340 341 |
# File 'lib/s3/right_s3.rb', line 339 def to_s @name.to_s end |