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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



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

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:



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

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:



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

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.



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

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.



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

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



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

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



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

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



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

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

Yield Parameters:

Returns:

Raises:



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

alias_method :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:

  • key (String)

    of file

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:



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

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:

  • (String)

    url



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

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:

  • (String)

    url



159
160
161
# File 'lib/fog/rackspace/models/storage/files.rb', line 159

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:

  • (String)

    url for object

Raises:

See Also:



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

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:



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

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:



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

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