Class: Aws::S3::Key
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
-
#copy(new_key_or_name) ⇒ Object
Create an object copy.
-
#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 = {}, &block) ⇒ Object
Retrieve object data and attributes from Amazon.
-
#grantees ⇒ Object
Return a list of grantees.
-
#head ⇒ Object
Updates headers and 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
Create a new Key instance, but do not create the actual key.
-
#move(new_key_or_name) ⇒ Object
Move an object to other location.
-
#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.
-
#reload_meta ⇒ Object
Reload meta-headers only.
-
#rename(new_name) ⇒ Object
Rename an object.
-
#save_meta(meta_headers) ⇒ Object
Replace meta-headers by new hash at S3.
-
#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
Create a new Key instance, but do not create the actual key. In normal use this method should not be called directly. Use Aws::S3::Key.create or bucket.key() instead.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/s3/right_s3.rb', line 419 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, @meta_headers = self.class.(headers) @meta_headers.merge!() end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 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.
466 467 468 469 |
# File 'lib/s3/right_s3.rb', line 466 def data get if !@data and exists? @data end |
#e_tag ⇒ Object (readonly)
Returns the value of attribute e_tag.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def e_tag @e_tag end |
#headers ⇒ Object
Returns the value of attribute headers.
373 374 375 |
# File 'lib/s3/right_s3.rb', line 373 def headers @headers end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def last_modified @last_modified end |
#meta_headers ⇒ Object
Returns the value of attribute meta_headers.
373 374 375 |
# File 'lib/s3/right_s3.rb', line 373 def @meta_headers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def owner @owner end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def size @size end |
#storage_class ⇒ Object (readonly)
Returns the value of attribute storage_class.
372 373 374 |
# File 'lib/s3/right_s3.rb', line 372 def storage_class @storage_class end |
Class Method Details
.add_meta_prefix(meta_headers, prefix = S3Interface::AMAZON_METADATA_PREFIX) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/s3/right_s3.rb', line 389 def self.(, prefix=S3Interface::AMAZON_METADATA_PREFIX) = {} .each do |, value| if [/#{prefix}/] [] = value else ["#{S3Interface::AMAZON_METADATA_PREFIX}#{}"] = value end end end |
.create(bucket, name, data = nil, meta_headers = {}) ⇒ Object
411 412 413 |
# File 'lib/s3/right_s3.rb', line 411 def self.create(bucket, name, data=nil, ={}) new(bucket, name, data, {}, ) end |
.split_meta(headers) ⇒ Object
Separate Amazon meta headers from other headers
377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/s3/right_s3.rb', line 377 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
#copy(new_key_or_name) ⇒ Object
Create an object copy. Returns a destination Aws::S3::Key instance.
# Key instance as destination
key1 = Aws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key2 = Aws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<Aws::S3::Key:0xb7b5e240 ... >
key1.put('Olala!') #=> true
key1.copy(key2) #=> #<Aws::S3::Key:0xb7b5e240 ... >
key1.exists? #=> true
key2.exists? #=> true
puts key2.data #=> 'Olala!'
# String as destination
key = Aws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key.put('Olala!') #=> true
new_key = key.copy('logs/today/888.log') #=> #<Aws::S3::Key:0xb7b5e240 ... >
key.exists? #=> true
new_key.exists? #=> true
529 530 531 532 533 |
# File 'lib/s3/right_s3.rb', line 529 def copy(new_key_or_name) new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key) @bucket.s3.interface.copy(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name) new_key_or_name end |
#delete ⇒ Object
Remove key from bucket. Returns true
.
key.delete #=> true
635 636 637 638 |
# File 'lib/s3/right_s3.rb', line 635 def delete raise 'Key name must be specified.' if @name.blank? @bucket.s3.interface.delete(@bucket, @name) end |
#exists? ⇒ Boolean
626 627 628 |
# File 'lib/s3/right_s3.rb', line 626 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'
450 451 452 |
# File 'lib/s3/right_s3.rb', line 450 def full_name(separator='/') "#{@bucket.to_s}#{separator}#{@name}" end |
#get(headers = {}, &block) ⇒ Object
Retrieve object data and attributes from Amazon. Returns a String
.
474 475 476 477 478 479 480 |
# File 'lib/s3/right_s3.rb', line 474 def get(headers={}, &block) response = @bucket.s3.interface.get(@bucket.name, @name, headers, &block) @data = response[:object] @headers, @meta_headers = self.class.(response[:headers]) refresh(false) @data end |
#grantees ⇒ Object
Return a list of grantees.
642 643 644 |
# File 'lib/s3/right_s3.rb', line 642 def grantees Grantee::grantees(self) end |
#head ⇒ Object
Updates headers and meta-headers from S3. Returns true
.
key. #=> {"family"=>"qwerty"}
key.head #=> true
key. #=> {"family"=>"qwerty", "name"=>"asdfg"}
593 594 595 596 |
# File 'lib/s3/right_s3.rb', line 593 def head @headers, @meta_headers = self.class.(@bucket.s3.interface.head(@bucket, @name)) true end |
#move(new_key_or_name) ⇒ Object
Move an object to other location. Returns a destination Aws::S3::Key instance.
# Key instance as destination
key1 = Aws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key2 = Aws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<Aws::S3::Key:0xb7b5e240 ... >
key1.put('Olala!') #=> true
key1.move(key2) #=> #<Aws::S3::Key:0xb7b5e240 ... >
key1.exists? #=> false
key2.exists? #=> true
puts key2.data #=> 'Olala!'
# String as destination
key = Aws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key.put('Olala!') #=> true
new_key = key.move('logs/today/888.log') #=> #<Aws::S3::Key:0xb7b5e240 ... >
key.exists? #=> false
new_key.exists? #=> true
553 554 555 556 557 |
# File 'lib/s3/right_s3.rb', line 553 def move(new_key_or_name) new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key) @bucket.s3.interface.move(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name) new_key_or_name end |
#public_link ⇒ Object
Return a public link to a key.
key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
458 459 460 461 |
# File 'lib/s3/right_s3.rb', line 458 def public_link params = @bucket.s3.interface.params "#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}" end |
#put(data = nil, perms = nil, headers = {}) ⇒ Object
492 493 494 495 496 497 |
# File 'lib/s3/right_s3.rb', line 492 def put(data=nil, perms=nil, headers={}) headers['x-amz-acl'] = perms if perms @data = data || @data = self.class.(@meta_headers) @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 = Aws::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"}
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/s3/right_s3.rb', line 570 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 = @meta_headers = {} false end end |
#reload_meta ⇒ Object
Reload meta-headers only. Returns meta-headers hash.
key. #=> {"family"=>"qwerty", "name"=>"asdfg"}
602 603 604 |
# File 'lib/s3/right_s3.rb', line 602 def @meta_headers = self.class.(@bucket.s3.interface.head(@bucket, @name)).last end |
#rename(new_name) ⇒ Object
506 507 508 509 |
# File 'lib/s3/right_s3.rb', line 506 def rename(new_name) @bucket.s3.interface.rename(@bucket.name, @name, new_name) @name = new_name end |
#save_meta(meta_headers) ⇒ Object
Replace meta-headers by new hash at S3. Returns new meta-headers hash.
key. #=> {"family"=>"qwerty", "name"=>"asdfg"}
key. #=> {"family"=>"oops", "race" => "troll"}
key. #=> {"family"=>"oops", "race" => "troll"}
612 613 614 615 616 |
# File 'lib/s3/right_s3.rb', line 612 def () = self.class.() @bucket.s3.interface.copy(@bucket.name, @name, @bucket.name, @name, :replace, ) @meta_headers = self.class.()[1] end |