Class: Fog::Storage::Rackspace::Files

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

Instance Attribute Summary collapse

Attributes inherited from Collection

#service

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#clear, #create, #destroy, #initialize, #inspect, #load, model, #model, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Collection

Instance Attribute Details

#directoryString

Note:

Methods in this class require this attribute to be set

Returns The name of the directory.

Returns:

  • (String)

    The name of the directory



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

attribute :directory

#limitInteger

Returns For an integer value n, limits the number of results to at most n values.

Returns:

  • (Integer)

    For an integer value n, limits the number of results to at most n values.

See Also:



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

attribute :limit

#markerString

Returns Given a string value x, return object names greater in value than the specified marker.

Returns:

  • (String)

    Given a string value x, return object names greater in value than the specified marker.

See Also:



23
# File 'lib/fog/rackspace/models/storage/files.rb', line 23

attribute :marker

#pathString

Equivalent to setting delimiter to ‘/’ and prefix to the path with a ‘/’ on the end.

Returns:

  • (String)

    For a string value x, return the object names nested in the pseudo path.



28
# File 'lib/fog/rackspace/models/storage/files.rb', line 28

attribute :path

#prefixString

Returns For a string value x, causes the results to be limited to object names beginning with the substring x.

Returns:

  • (String)

    For a string value x, causes the results to be limited to object names beginning with the substring x.



32
# File 'lib/fog/rackspace/models/storage/files.rb', line 32

attribute :prefix

Class Method Details

.file_url(path, key) ⇒ String

Returns an escaped object url

Parameters:

  • path (String)

    of the url

  • key (String)

    of the object

Returns:

  • (String)

    escaped file url



196
197
198
199
# File 'lib/fog/rackspace/models/storage/files.rb', line 196

def self.file_url(path, key)
  return nil unless path
  "#{path}/#{Fog::Rackspace.escape(key, '/')}"
end

Instance Method Details

#all(options = {}) ⇒ Fog::Storage::Rackspace::Files

Returns list of files



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/rackspace/models/storage/files.rb', line 43

def all(options = {})
  requires :directory
  options = {
    'limit'   => limit,
    'marker'  => marker,
    'path'    => path,
    'prefix'  => prefix
  }.merge!(options)
  merge_attributes(options)
  parent = directory.collection.get(
    directory.key,
    options
  )
  if parent
    load(parent.files.map {|file| file.attributes})
  else
    nil
  end
end

#eachObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/rackspace/models/storage/files.rb', line 72

def each
  if !block_given?
    self
  else
    subset = dup.all

    subset.each_file_this_page {|f| yield f}
    while subset.length == (subset.limit || 10000)
      subset = subset.all(:marker => subset.last.key)
      subset.each_file_this_page {|f| yield f}
    end

    self
  end
end

#each_file_this_page {|| ... } ⇒ Fog::Storage::Rackspace::Directory

Note:

This method retrieves files in pages. Page size is defined by the limit attribute

Calls block for each file in the directory



71
# File 'lib/fog/rackspace/models/storage/files.rb', line 71

alias :each_file_this_page :each

#get(key) {|data, remaining, content_length| ... } ⇒ Fog::Storage::Rackspace:File

Note:

If a block is provided, the body attribute will be empty. By default chunk size is 1 MB. This value can be changed by passing the parameter :chunk_size into the :connection_options hash in the service constructor.

Retrieves file

Examples:

Download an image from Cloud Files and save it to file


File.open('download.jpg', 'w') do | f |
  my_directory.files.get("europe.jpg") do |data, remaing, content_length|
    f.syswrite data
  end
end

Parameters:

Yields:

  • get yields to block after chunk of data has been received (Optional)

Yield Parameters:

  • data (String)
  • remaining (Integer)
  • content_length (Integer)

Returns:

  • (Fog::Storage::Rackspace:File)

Raises:

See Also:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fog/rackspace/models/storage/files.rb', line 110

def get(key, &block)
  requires :directory
  data = service.get_object(directory.key, key, &block)
   = Metadata.from_headers(self, data.headers)  
  file_data = data.headers.merge({
    :body => data.body,
    :key  => key,
    :metadata => 
  })
  
  new(file_data)
rescue Fog::Storage::Rackspace::NotFound
  nil
end

#get_http_url(key, expires, options = {}) ⇒ String

Note:

This URL does not use the Rackspace CDN

Get a temporary http url for a file.

required attributes: key

Parameters:

  • key (String)

    the key of the file within the directory

  • expires (String)

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

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

Returns:



148
149
150
151
# File 'lib/fog/rackspace/models/storage/files.rb', line 148

def get_http_url(key, expires, options = {})
  requires :directory
  service.get_object_http_url(directory.key, key, expires, options)
end

#get_https_url(key, expires, options = {}) ⇒ String

Note:

This URL does not use the Rackspace CDN

Get a temporary https url for a file.

required attributes: key

Parameters:

  • key (String)

    the key of the file within the directory

  • expires (String)

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

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

Returns:



161
162
163
# File 'lib/fog/rackspace/models/storage/files.rb', line 161

def get_https_url(key, expires, options = {})
  service.get_object_https_url(directory.key, key, expires, options)
end

#get_url(key) ⇒ String

Returns the public_url for the given object key

Parameters:

  • key

    of the object

Returns:

Raises:

See Also:



133
134
135
136
137
138
# File 'lib/fog/rackspace/models/storage/files.rb', line 133

def get_url(key)
  requires :directory
  if self.directory.public_url
    Files::file_url directory.public_url, key
  end
end

#head(key, options = {}) ⇒ Fog::Storage::Rackspace::File

View directory detail without loading file contents

Parameters:

  • key

    of the object

  • options (defaults to: {})

    Required for compatibility with other Fog providers. Not Used.

Returns:

Raises:



173
174
175
176
177
178
179
180
181
182
# File 'lib/fog/rackspace/models/storage/files.rb', line 173

def head(key, options = {})
  requires :directory
  data = service.head_object(directory.key, key)
  file_data = data.headers.merge({
    :key => key
  })
  new(file_data)
rescue Fog::Storage::Rackspace::NotFound
  nil
end

#new(attributes = {}) ⇒ Fog::Storage::Rackspace::File

Create a new file object

Parameters:

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

    of object

Returns:



187
188
189
190
# File 'lib/fog/rackspace/models/storage/files.rb', line 187

def new(attributes = {})
  requires :directory
  super({ :directory => directory }.merge!(attributes))
end