Class: S3::Object

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/s3/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Object.



91
92
93
94
95
96
97
# File 'lib/s3/object.rb', line 91

def initialize(bucket, key, options = {})
  self.bucket = bucket
  self.key = key
  self.last_modified = options[:last_modified]
  self.etag = options[:etag]
  self.size = options[:size]
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



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

def acl
  @acl
end

#bucketObject

Returns the value of attribute bucket.



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

def bucket
  @bucket
end

#content(reload = false) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/s3/object.rb', line 38

def content(reload = false)
  if reload or @content.nil?
    response = object_request(:get)
    parse_headers(response)
    self.content = response.body
  end
  @content
end

#content_dispositionObject

Returns the value of attribute content_disposition.



5
6
7
# File 'lib/s3/object.rb', line 5

def content_disposition
  @content_disposition
end

#content_encodingObject

Returns the value of attribute content_encoding.



5
6
7
# File 'lib/s3/object.rb', line 5

def content_encoding
  @content_encoding
end

#content_typeObject

Returns the value of attribute content_type.



5
6
7
# File 'lib/s3/object.rb', line 5

def content_type
  @content_type
end

#etagObject

Returns the value of attribute etag.



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

def etag
  @etag
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#last_modifiedObject

Returns the value of attribute last_modified.



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

def last_modified
  @last_modified
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#cname_urlObject



83
84
85
# File 'lib/s3/object.rb', line 83

def cname_url
  "#{protocol}#{name}/#{key}" if bucket.vhost?
end

#copy(options = {}) ⇒ Object



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

def copy(options = {})
  key = options[:key] || self.key
  bucket = options[:bucket] || self.bucket

  headers = {}
  headers[:x_amz_acl] = options[:acl] || acl || "public-read"
  headers[:content_type] = options[:content_type] || content_type || "application/octet-stream"
  headers[:content_encoding] = options[:content_encoding] if options[:content_encoding]
  headers[:content_disposition] = options[:content_disposition] if options[:content_disposition]
  headers[:x_amz_copy_source] = full_key
  headers[:x_amz_metadata_directive] = "REPLACE"
  headers[:x_amz_copy_source_if_match] = options[:if_match] if options[:if_match]
  headers[:x_amz_copy_source_if_none_match] = options[:if_none_match] if options[:if_none_match]
  headers[:x_amz_copy_source_if_unmodified_since] = options[:if_modified_since] if options[:if_modified_since]
  headers[:x_amz_copy_source_if_modified_since] = options[:if_unmodified_since] if options[:if_unmodified_since]

  response = bucket.send(:bucket_request, :put, :path => key, :headers => headers)
  self.class.parse_copied(:object => self, :bucket => bucket, :key => key, :body => response.body, :headers => headers)
end

#destroyObject



74
75
76
77
# File 'lib/s3/object.rb', line 74

def destroy
  object_request(:delete)
  true
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  retrieve
  true
rescue Error::NoSuchKey
  false
end

#full_keyObject



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

def full_key
  [name, key].join("/")
end

#inspectObject



87
88
89
# File 'lib/s3/object.rb', line 87

def inspect
  "#<#{self.class}:/#{name}/#{key}>"
end

#retrieveObject



25
26
27
28
29
# File 'lib/s3/object.rb', line 25

def retrieve
  response = object_request(:get, :headers => { :range => 0..0 })
  parse_headers(response)
  self
end

#saveObject



47
48
49
50
51
52
# File 'lib/s3/object.rb', line 47

def save
  body = content.is_a?(IO) ? content.read : content
  response = object_request(:put, :body => body, :headers => dump_headers)
  parse_headers(response)
  true
end

#urlObject



79
80
81
# File 'lib/s3/object.rb', line 79

def url
  "#{protocol}#{host}/#{path_prefix}#{key}"
end