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



151
152
153
154
# File 'lib/fog/rackspace/models/storage/files.rb', line 151

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

Instance Method Details

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

Returns list of files



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/rackspace/models/storage/files.rb', line 39

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/rackspace/models/storage/files.rb', line 64

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::File]| ... } ⇒ 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

Yield Parameters:

Returns:



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

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)

See Also:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fog/rackspace/models/storage/files.rb', line 98

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_url(key) ⇒ String

Returns the public_url for the given object key

Parameters:

  • key

    of the object

Returns:

See Also:



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

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:



128
129
130
131
132
133
134
135
136
137
# File 'lib/fog/rackspace/models/storage/files.rb', line 128

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:



142
143
144
145
# File 'lib/fog/rackspace/models/storage/files.rb', line 142

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