Class: Aws::S3::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/s3/key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/s3/key.rb', line 53

def initialize(bucket, name, data=nil, headers={}, meta_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?(S3::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.split_meta(headers)
  @meta_headers.merge!(meta_headers)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/s3/key.rb', line 6

def bucket
  @bucket
end

#dataObject

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.



100
101
102
103
# File 'lib/s3/key.rb', line 100

def data
  get if !@data and exists?
  @data
end

#e_tagObject (readonly)

Returns the value of attribute e_tag.



6
7
8
# File 'lib/s3/key.rb', line 6

def e_tag
  @e_tag
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/s3/key.rb', line 7

def headers
  @headers
end

#last_modifiedObject (readonly)

Returns the value of attribute last_modified.



6
7
8
# File 'lib/s3/key.rb', line 6

def last_modified
  @last_modified
end

#meta_headersObject

Returns the value of attribute meta_headers.



7
8
9
# File 'lib/s3/key.rb', line 7

def meta_headers
  @meta_headers
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/s3/key.rb', line 6

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



6
7
8
# File 'lib/s3/key.rb', line 6

def owner
  @owner
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/s3/key.rb', line 6

def size
  @size
end

#storage_classObject (readonly)

Returns the value of attribute storage_class.



6
7
8
# File 'lib/s3/key.rb', line 6

def storage_class
  @storage_class
end

Class Method Details

.add_meta_prefix(meta_headers, prefix = S3Interface::AMAZON_METADATA_PREFIX) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/s3/key.rb', line 23

def self.add_meta_prefix(meta_headers, prefix=S3Interface::AMAZON_METADATA_PREFIX)
  meta = {}
  meta_headers.each do |meta_header, value|
    if meta_header[/#{prefix}/]
      meta[meta_header] = value
    else
      meta["#{S3Interface::AMAZON_METADATA_PREFIX}#{meta_header}"] = value
    end
  end
  meta
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 = Aws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key.exists?                                                  #=> true | false
key.put('Woohoo!')                                           #=> true
key.exists?                                                  #=> true


45
46
47
# File 'lib/s3/key.rb', line 45

def self.create(bucket, name, data=nil, meta_headers={})
  new(bucket, name, data, {}, meta_headers)
end

.split_meta(headers) ⇒ Object

Separate Amazon meta headers from other headers



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/s3/key.rb', line 11

def self.split_meta(headers) #:nodoc:
  hash = headers.dup
  meta = {}
  hash.each do |key, value|
    if key[/^#{S3Interface::AMAZON_METADATA_PREFIX}/]
      meta[key.gsub(S3Interface::AMAZON_METADATA_PREFIX, '')] = value
      hash.delete(key)
    end
  end
  [hash, meta]
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


163
164
165
166
167
# File 'lib/s3/key.rb', line 163

def copy(new_key_or_name)
  new_key_or_name = S3::Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(S3::Key)
  @bucket.s3.interface.copy(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
  new_key_or_name
end

#deleteObject

Remove key from bucket. Returns true.

key.delete #=> true


269
270
271
272
# File 'lib/s3/key.rb', line 269

def delete
  raise 'Key name must be specified.' if Aws::Utils.blank?(@name)
  @bucket.s3.interface.delete(@bucket, @name)
end

#exists?Boolean

Check for existence of the key in the given bucket. Returns true or false.

key = Aws::S3::Key.create(bucket,'logs/today/1.log')
key.exists?        #=> false
key.put('Woohoo!') #=> true
key.exists?        #=> true

Returns:

  • (Boolean)


260
261
262
# File 'lib/s3/key.rb', line 260

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'


84
85
86
# File 'lib/s3/key.rb', line 84

def full_name(separator='/')
  "#{@bucket.to_s}#{separator}#{@name}"
end

#get(headers = {}, &block) ⇒ Object

Retrieve object data and attributes from Amazon. Returns a String.



108
109
110
111
112
113
114
# File 'lib/s3/key.rb', line 108

def get(headers={}, &block)
  response = @bucket.s3.interface.get(@bucket.name, @name, headers, &block)
  @data    = response[:object]
  @headers, @meta_headers = self.class.split_meta(response[:headers])
#        refresh(false) Holy moly, this was doing two extra hits to s3 for making 3 hits for every get!!
  @data
end

#granteesObject

Return a list of grantees.



276
277
278
# File 'lib/s3/key.rb', line 276

def grantees
 S3::Grantee::grantees(self)
end

#headObject

Updates headers and meta-headers from S3. Returns true.

key.meta_headers #=> {"family"=>"qwerty"}
key.head         #=> true
key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}


227
228
229
230
# File 'lib/s3/key.rb', line 227

def head
  @headers, @meta_headers = self.class.split_meta(@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


187
188
189
190
191
# File 'lib/s3/key.rb', line 187

def move(new_key_or_name)
  new_key_or_name = S3::Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(S3::Key)
  @bucket.s3.interface.move(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
  new_key_or_name
end

Return a public link to a key.

key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'


92
93
94
95
# File 'lib/s3/key.rb', line 92

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 = Aws::S3::Key.create(bucket, 'logs/today/1.log')
key.data = 'Qwerty'
key.put             #=> true
 ...
key.put('Olala!')   #=> true


126
127
128
129
130
131
# File 'lib/s3/key.rb', line 126

def put(data=nil, perms=nil, headers={})
  headers['x-amz-acl'] = perms if perms
  @data = data || @data
  meta  = self.class.add_meta_prefix(@meta_headers)
  @bucket.s3.interface.put(@bucket.name, @name, @data, meta.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.meta_headers #=> {}
key.refresh      #=> true
key.e_tag        #=> '12345678901234567890bf11094484b6'
key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/s3/key.rb', line 204

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_metaObject

Reload meta-headers only. Returns meta-headers hash.

key.reload_meta   #=> {"family"=>"qwerty", "name"=>"asdfg"}


236
237
238
# File 'lib/s3/key.rb', line 236

def reload_meta
  @meta_headers = self.class.split_meta(@bucket.s3.interface.head(@bucket, @name)).last
end

#rename(new_name) ⇒ Object

Rename an object. Returns new object name.

key = Aws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
key.rename('logs/today/2.log')   #=> 'logs/today/2.log'
puts key.name                    #=> 'logs/today/2.log'
key.exists?                      #=> true


140
141
142
143
# File 'lib/s3/key.rb', line 140

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.reload_meta   #=> {"family"=>"qwerty", "name"=>"asdfg"}
key.save_meta     #=> {"family"=>"oops", "race" => "troll"}
key.reload_meta   #=> {"family"=>"oops", "race" => "troll"}


246
247
248
249
250
# File 'lib/s3/key.rb', line 246

def save_meta(meta_headers)
  meta = self.class.add_meta_prefix(meta_headers)
  @bucket.s3.interface.copy(@bucket.name, @name, @bucket.name, @name, :replace, meta)
  @meta_headers = self.class.split_meta(meta)[1]
end

#to_sObject

Return key name as a String.

key = Aws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<Aws::S3::Key:0xb7b1e240 ... >
puts key                                                   #=> 'logs/today/1.log'


76
77
78
# File 'lib/s3/key.rb', line 76

def to_s
  @name.to_s
end