Class: Fog::Storage::Rackspace::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/rackspace/models/storage/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_control_allow_originString

A space delimited list of URLs allowed to make Cross Origin Requests. Format is www.example.com. An asterisk (*) allows all.

Returns:

  • (String)

    string containing a list of space delimited URLs

See Also:



36
# File 'lib/fog/rackspace/models/storage/file.rb', line 36

attribute :access_control_allow_origin, :aliases => ['Access-Control-Allow-Origin']

#content_encodingString

Returns The content encoding of the file.



46
# File 'lib/fog/rackspace/models/storage/file.rb', line 46

attribute :content_encoding, :aliases => 'Content-Encoding'

#content_lengthInteger (readonly)

Returns The content length of the file.

Returns:

  • (Integer)

    The content length of the file



13
# File 'lib/fog/rackspace/models/storage/file.rb', line 13

attribute :content_length,  :aliases => ['bytes', 'Content-Length'], :type => :integer

#content_typeString

Returns The MIME Media Type of the file.

Returns:

  • (String)

    The MIME Media Type of the file

See Also:



18
# File 'lib/fog/rackspace/models/storage/file.rb', line 18

attribute :content_type,    :aliases => ['content_type', 'Content-Type']

#delete_afterInteger

A number of seconds representing how long from now this object will be automatically deleted.

Returns:

  • (Integer)

    the number of seconds until this object will be automatically deleted

See Also:



58
# File 'lib/fog/rackspace/models/storage/file.rb', line 58

attribute :delete_after, :aliases => ['X-Delete-After']

#delete_atInteger

A Unix Epoch Timestamp, in integer form, representing the time when this object will be automatically deleted.

Returns:

  • (Integer)

    the unix epoch timestamp of when this object will be automatically deleted

See Also:



52
# File 'lib/fog/rackspace/models/storage/file.rb', line 52

attribute :delete_at, :aliases => ['X-Delete-At']

#directoryFog::Storage::Rackspace::Directory

Returns directory containing file.

Returns:



62
63
64
# File 'lib/fog/rackspace/models/storage/file.rb', line 62

def directory
  @directory
end

#etagString

The MD5 checksum of file. If included file creation request, will ensure integrity of the file.

Returns:

  • (String)

    MD5 checksum of file.



25
# File 'lib/fog/rackspace/models/storage/file.rb', line 25

attribute :etag,            :aliases => ['hash', 'Etag']

#keyString (readonly)

Returns The name of the file.

Returns:

  • (String)

    The name of the file



9
# File 'lib/fog/rackspace/models/storage/file.rb', line 9

identity  :key,             :aliases => 'name'

#last_modifiedTime

The last time the file was modified

Returns:

  • (Time)

    The last time the file was modified



30
# File 'lib/fog/rackspace/models/storage/file.rb', line 30

attribute :last_modified,   :aliases => ['last_modified', 'Last-Modified'], :type => :time

#originString

Returns The origin is the URI of the object’s host.

Returns:

  • (String)

    The origin is the URI of the object’s host.

See Also:



41
# File 'lib/fog/rackspace/models/storage/file.rb', line 41

attribute :origin,          :aliases => ['Origin']

#public=(value) ⇒ Object (writeonly)

Note:

Required for compatibility with other Fog providers. Not Used.



66
67
68
# File 'lib/fog/rackspace/models/storage/file.rb', line 66

def public=(value)
  @public = value
end

Instance Method Details

#bodyObject

Returns the body/contents of file

Examples:

Retrieve and download contents of Cloud Files object to file system

file_object = directory.files.get('germany.jpg')
File.open('germany.jpg', 'w') {|f| f.write(file_object.body) }

Raises:

See Also:



77
78
79
80
81
82
83
# File 'lib/fog/rackspace/models/storage/file.rb', line 77

def body
  attributes[:body] ||= if last_modified
    collection.get(identity).body
  else
    ''
  end
end

#body=(new_body) ⇒ Object

Sets the body/contents of file

Parameters:



87
88
89
# File 'lib/fog/rackspace/models/storage/file.rb', line 87

def body=(new_body)
  attributes[:body] = new_body
end

#copy(target_directory_key, target_file_key, options = {}) ⇒ Object

Copy file to another directory or directory

Parameters:

  • target_directory_key (String)
  • target_file_key (String)
  • options (Hash) (defaults to: {})

    used to pass in file attributes

Raises:

See Also:



100
101
102
103
104
105
106
107
108
109
# File 'lib/fog/rackspace/models/storage/file.rb', line 100

def copy(target_directory_key, target_file_key, options={})
  requires :directory, :key
  options['Content-Type'] ||= content_type if content_type
  options['Access-Control-Allow-Origin'] ||= access_control_allow_origin if access_control_allow_origin
  options['Origin'] ||= origin if origin
  options['Content-Encoding'] ||= content_encoding if content_encoding
  service.copy_object(directory.key, key, target_directory_key, target_file_key, options)
  target_directory = service.directories.new(:key => target_directory_key)
  target_directory.files.get(target_file_key)
end

#destroyBoolean

Destroy the file

Returns:

  • (Boolean)

    returns true if file is destroyed

Raises:

See Also:



118
119
120
121
122
# File 'lib/fog/rackspace/models/storage/file.rb', line 118

def destroy
  requires :directory, :key
  service.delete_object(directory.key, key)
  true
end

#ios_urlString

URL used to stream video to iOS devices without needing to convert your video



221
222
223
# File 'lib/fog/rackspace/models/storage/file.rb', line 221

def ios_url
  Files::file_url directory.ios_url, key
end

#metadataFog::Storage::Rackspace::Metadata

File metadata

Returns:



137
138
139
# File 'lib/fog/rackspace/models/storage/file.rb', line 137

def 
  attributes[:metadata] ||= Fog::Storage::Rackspace::Metadata.new(self)
end

#metadata=(hash) ⇒ Object

Set file metadata

Parameters:



126
127
128
129
130
131
132
133
# File 'lib/fog/rackspace/models/storage/file.rb', line 126

def metadata=(hash)
  if hash.is_a? Fog::Storage::Rackspace::Metadata
    attributes[:metadata] = hash
  else
    attributes[:metadata] = Fog::Storage::Rackspace::Metadata.new(self, hash)
  end
  attributes[:metadata]
end

#owner=(new_owner) ⇒ Object

Required for compatibility with other Fog providers. Not Used.



142
143
144
145
146
147
148
149
# File 'lib/fog/rackspace/models/storage/file.rb', line 142

def owner=(new_owner)
  if new_owner
    attributes[:owner] = {
      :display_name => new_owner['DisplayName'],
      :id           => new_owner['ID']
    }
  end
end

#public?Boolean

Is file published to CDN

Returns:

  • (Boolean)

    return true if published to CDN

Raises:



175
176
177
# File 'lib/fog/rackspace/models/storage/file.rb', line 175

def public?
  directory.public?
end

#public_urlString

Returns the public url for the file. If the file has not been published to the CDN, this method will return nil as it is not publically accessible. This method will return the approprate url in the following order:

  1. If the service used to access this file was created with the option :rackspace_cdn_ssl => true, this method will return the SSL-secured URL.

  2. If the directory’s cdn_cname attribute is populated this method will return the cname.

  3. return the default CDN url.

Returns:

  • (String)

    public url for file

Raises:



210
211
212
# File 'lib/fog/rackspace/models/storage/file.rb', line 210

def public_url
  Files::file_url directory.public_url, key
end

#purge_from_cdnObject

Note:

You may only PURGE up to 25 objects per day. Any attempt to purge more than this will result in a 498 status code error (Rate Limited).

Immediately purge file from the CDN network



243
244
245
246
247
248
249
# File 'lib/fog/rackspace/models/storage/file.rb', line 243

def purge_from_cdn
  if public?
    service.cdn.purge(self)
  else
    false
  end
end

#save(options = {}) ⇒ Object

Create or updates file and associated metadata

Parameters:

  • options (Hash) (defaults to: {})

    additional parameters to pass to Cloud Files

Raises:

See Also:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/fog/rackspace/models/storage/file.rb', line 258

def save(options = {})
  requires :body, :directory, :key
  options['Content-Type'] = content_type if content_type
  options['Access-Control-Allow-Origin'] = access_control_allow_origin if access_control_allow_origin
  options['Origin'] = origin if origin
  options['Content-Disposition'] = content_disposition if content_disposition
  options['Etag'] = etag if etag
  options['Content-Encoding'] = content_encoding if content_encoding
  options['X-Delete-At'] = delete_at if delete_at
  options['X-Delete-After'] = delete_after if delete_after
  options.merge!(.to_headers)

  data = service.put_object(directory.key, key, body, options)
  update_attributes_from(data)

  self.content_length = Fog::Storage.get_body_size(body)
  self.content_type ||= Fog::Storage.get_content_type(body)
  true
end

#streaming_urlString

URL used to stream resources



232
233
234
# File 'lib/fog/rackspace/models/storage/file.rb', line 232

def streaming_url
  Files::file_url directory.streaming_url, key
end

#url(expires, options = {}) ⇒ String

Note:

This URL does not use the Rackspace CDN

Get a url for file.

required attributes: key

Parameters:

  • expires (String)

    number of seconds (since 1970-01-01 00:00) before url expires

  • options (Hash) (defaults to: {})

Returns:



188
189
190
191
192
193
194
195
# File 'lib/fog/rackspace/models/storage/file.rb', line 188

def url(expires, options = {})
  requires :key
  if service.ssl?
    service.get_object_https_url(directory.key, key, expires, options)
  else
    service.get_object_http_url(directory.key, key, expires, options)
  end
end