Class: UberS3::Object

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UberS3::Operation::Object::StorageClass

included

Methods included from UberS3::Operation::Object::Meta

included

Methods included from UberS3::Operation::Object::HttpCache

included

Methods included from UberS3::Operation::Object::ContentType

included

Methods included from UberS3::Operation::Object::ContentMd5

included

Methods included from UberS3::Operation::Object::ContentEncoding

included

Methods included from UberS3::Operation::Object::ContentDisposition

included

Methods included from UberS3::Operation::Object::AccessPolicy

included

Constructor Details

#initialize(bucket, key, value = nil, options = {}) ⇒ Object

Returns a new instance of Object.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/uber-s3/object.rb', line 14

def initialize(bucket, key, value=nil, options={})
  self.bucket        = bucket
  self.key           = key
  self.value         = value
  
  # Init current state
  infer_content_type!
  
  # Call operation methods based on options passed
  bucket.connection.defaults.merge(options).each {|k,v| self.send((k.to_s+'=').to_sym, v) }
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def bucket
  @bucket
end

#errorObject

Returns the value of attribute error.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def error
  @error
end

#keyObject

Returns the value of attribute key.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def key
  @key
end

#responseObject

Returns the value of attribute response.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def response
  @response
end

#sizeObject

Returns the value of attribute size.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def size
  @size
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/uber-s3/object.rb', line 12

def value
  @value
end

Instance Method Details

#deleteObject



103
104
105
# File 'lib/uber-s3/object.rb', line 103

def delete
  bucket.connection.delete(key).status == 204
end

#exists?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/uber-s3/object.rb', line 30

def exists?
  # TODO.. refactor this as if we've already called head
  # on the object, there is no need to do it again..
  # perhaps move some things into class methods?
  bucket.connection.head(key).status == 200
end

#fetchObject



44
45
46
47
48
49
50
# File 'lib/uber-s3/object.rb', line 44

def fetch
  self.response = bucket.connection.get(key)
  self.value = response.body

  parse_response_header!
  self
end

#headObject



37
38
39
40
41
42
# File 'lib/uber-s3/object.rb', line 37

def head
  self.response = bucket.connection.head(key)
  
  parse_response_header!
  self
end

#persisted?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/uber-s3/object.rb', line 112

def persisted?
  # TODO
end

#saveObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/uber-s3/object.rb', line 52

def save
  headers = {}
  
  # Encode data if necessary
  gzip_content!
  
  # Standard pass through values
  headers['Content-Disposition']  = content_disposition
  headers['Content-Encoding']     = content_encoding
  headers['Content-Length']       = size.to_s
  headers['Content-Type']         = content_type
  headers['Cache-Control']        = cache_control
  headers['Expires']              = expires
  headers['Pragma']               = pragma

  headers.each {|k,v| headers.delete(k) if v.nil? || v.empty? }

  # Content MD5 integrity check
  if !content_md5.nil?
    self.content_md5 = Digest::MD5.hexdigest(value) if content_md5 == true

    # We expect a md5 hex digest here
    md5_digest = content_md5.unpack('a2'*16).collect {|i| i.hex.chr }.join
    headers['Content-MD5'] = Base64.encode64(md5_digest).strip
  end

  # ACL
  if !access.nil?
    headers['x-amz-acl'] = access.to_s.gsub('_', '-')
  end
  
  # Storage class
  if !storage_class.nil?
    headers['x-amz-storage-class'] = storage_class.to_s.upcase
  end

  # Meta
  if !meta.nil? && !meta.empty?
    meta.each {|k,v| headers["x-amz-meta-#{k}"] = v.to_s }
  end
  
  # Let's do it
  response = bucket.connection.put(key, headers, value)
  
  # Update error state
  self.error = response.error
  
  # Test for success....
  response.status == 200      
end

#to_sObject



26
27
28
# File 'lib/uber-s3/object.rb', line 26

def to_s
  "#<#{self.class} @key=\"#{self.key}\">"
end

#urlObject



116
117
118
# File 'lib/uber-s3/object.rb', line 116

def url
  # TODO
end