Class: S3::Object

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

Overview

Class responsible for handling objects stored in S3 buckets

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_acl, #parse_copy_object_result, #parse_error, #parse_is_truncated, #parse_list_all_my_buckets_result, #parse_list_bucket_result, #parse_location_constraint, #rexml_document

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



9
10
11
# File 'lib/s3/object.rb', line 9

def acl
  @acl
end

#bucketObject

Returns the value of attribute bucket.



9
10
11
# File 'lib/s3/object.rb', line 9

def bucket
  @bucket
end

#cache_controlObject

Returns the value of attribute cache_control.



8
9
10
# File 'lib/s3/object.rb', line 8

def cache_control
  @cache_control
end

#content(reload = false) ⇒ Object

Downloads the content of the object, and caches it. Pass true to clear the cache and download the object again.



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

def content(reload = false)
  return @content unless reload or @content.nil?
  get_object
  @content
end

#content_dispositionObject

Returns the value of attribute content_disposition.



8
9
10
# File 'lib/s3/object.rb', line 8

def content_disposition
  @content_disposition
end

#content_encodingObject

Returns the value of attribute content_encoding.



8
9
10
# File 'lib/s3/object.rb', line 8

def content_encoding
  @content_encoding
end

#content_typeObject

Returns the value of attribute content_type.



8
9
10
# File 'lib/s3/object.rb', line 8

def content_type
  @content_type
end

#etagObject

Returns the value of attribute etag.



9
10
11
# File 'lib/s3/object.rb', line 9

def etag
  @etag
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/s3/object.rb', line 9

def key
  @key
end

#last_modifiedObject

Returns the value of attribute last_modified.



9
10
11
# File 'lib/s3/object.rb', line 9

def last_modified
  @last_modified
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/s3/object.rb', line 9

def 
  @metadata
end

#sizeObject

Returns the value of attribute size.



9
10
11
# File 'lib/s3/object.rb', line 9

def size
  @size
end

#storage_classObject

Returns the value of attribute storage_class.



9
10
11
# File 'lib/s3/object.rb', line 9

def storage_class
  @storage_class
end

Instance Method Details

#==(other) ⇒ Object

Compares the object with other object. Returns true if the key of the objects are the same, and both have the same buckets (see Bucket equality)



19
20
21
# File 'lib/s3/object.rb', line 19

def ==(other)
  other.equal?(self) || (other.instance_of?(self.class) && self.key == other.key && self.bucket == other.bucket)
end

#cname_urlObject

Returns Object’s CNAME URL (without s3.amazonaws.com suffix) using protocol specified in Service, e.g. http://domain.com/key/with/path.extension. (you have to set the CNAME in your DNS before using the CNAME URL schema).



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

def cname_url
  Addressable::URI.escape("#{protocol}#{name}/#{key}") if bucket.vhost?
end

#copy(options = {}) ⇒ Object

Copies the file to another key and/or bucket.

Options

  • :key - New key to store object in

  • :bucket - New bucket to store object in (instance of S3::Bucket)

  • :acl - ACL of the copied object (default: “private”)

  • :content_type - Content type of the copied object (default: “application/octet-stream”)



108
109
110
# File 'lib/s3/object.rb', line 108

def copy(options = {})
  copy_object(options)
end

#destroyObject

Destroys the file on the server



113
114
115
116
# File 'lib/s3/object.rb', line 113

def destroy
  delete_object
  true
end

#exists?Boolean

Retrieves the object from the server, returns true if the object exists or false otherwise. Uses #retrieve method, but catches S3::Error::NoSuchKey exception and returns false when it happens

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/s3/object.rb', line 68

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

#full_keyObject

Returns full key of the object: e.g. bucket-name/object/key.ext



24
25
26
# File 'lib/s3/object.rb', line 24

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

#inspectObject

:nodoc:



144
145
146
# File 'lib/s3/object.rb', line 144

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

#request_aclObject

Retrieves acl for object from the server.

Return: hash: user|group => permission



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

def request_acl
  response = object_request(:get, :params => "acl")
  parse_acl(response.body)
end

#retrieveObject

Retrieves the object from the server. Method is used to download object information only (content type, size). Notice: It does NOT download the content of the object (use the #content method to do it). Notice: this do not fetch acl information, use #request_acl method for that.



60
61
62
63
# File 'lib/s3/object.rb', line 60

def retrieve
  object_headers
  self
end

#saveObject

Saves the object, returns true if successfull.



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

def save
  put_object
  true
end

#temporary_url(expires_at = Time.now + 3600) ⇒ Object

Returns a temporary url to the object that expires on the timestamp given. Defaults to one hour expire time.



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

def temporary_url(expires_at = Time.now + 3600)
  signature = Signature.generate_temporary_url_signature(:bucket => name,
                                                         :resource => key,
                                                         :expires_at => expires_at,
                                                         :secret_access_key => secret_access_key)

  "#{url}?AWSAccessKeyId=#{self.bucket.service.access_key_id}&Expires=#{expires_at.to_i.to_s}&Signature=#{signature}"
end

#urlObject

Returns Object’s URL using protocol specified in service, e.g. http://domain.com.s3.amazonaws.com/key/with/path.extension



120
121
122
# File 'lib/s3/object.rb', line 120

def url
  "#{protocol}#{host}/#{path_prefix}#{Addressable::URI.escape(key)}"
end