Class: Fog::Rackspace::Files::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/files.rb,
lib/fog/rackspace/models/files/directories.rb,
lib/fog/rackspace/requests/files/get_object.rb,
lib/fog/rackspace/requests/files/put_object.rb,
lib/fog/rackspace/requests/files/head_object.rb,
lib/fog/rackspace/requests/files/delete_object.rb,
lib/fog/rackspace/requests/files/get_container.rb,
lib/fog/rackspace/requests/files/put_container.rb,
lib/fog/rackspace/requests/files/get_containers.rb,
lib/fog/rackspace/requests/files/head_container.rb,
lib/fog/rackspace/requests/files/head_containers.rb,
lib/fog/rackspace/requests/files/delete_container.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fog/rackspace/files.rb', line 77

def initialize(options={})
  credentials = Fog::Rackspace.authenticate(options)
  @auth_token = credentials['X-Auth-Token']
  cdn_uri = URI.parse(credentials['X-CDN-Management-Url'])
  @cdn_host   = cdn_uri.host
  @cdn_path   = cdn_uri.path
  @cdn_port   = cdn_uri.port
  @cdn_scheme = cdn_uri.scheme
  storage_uri = URI.parse(credentials['X-Storage-Url'])
  @storage_host   = storage_uri.host
  @storage_path   = storage_uri.path
  @storage_port   = storage_uri.port
  @storage_scheme = storage_uri.scheme
end

Instance Method Details

#cdn_request(params) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/rackspace/files.rb', line 92

def cdn_request(params)
  @cdn_connection = Fog::Connection.new("#{@cdn_scheme}://#{@cdn_host}:#{@cdn_port}")
  response = @cdn_connection.request({
    :body     => params[:body],
    :expects  => params[:expects],
    :headers  => {
      'Content-Type' => 'application/json',
      'X-Auth-Token' => @auth_token
    }.merge!(params[:headers] || {}),
    :host     => @cdn_host,
    :method   => params[:method],
    :path     => "#{@cdn_path}/#{params[:path]}",
    :query    => params[:query]
  })
  unless response.body.empty?
    response.body = JSON.parse(response.body)
  end
  response
end

#delete_container(name) ⇒ Object

Delete an existing container

Parameters

  • name<~String> - Name of container to delete


11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/files/delete_container.rb', line 11

def delete_container(name)
  response = storage_request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => CGI.escape(name)
  )
  response
end

#delete_object(container, object) ⇒ Object

Delete an existing container

Parameters

  • container<~String> - Name of container to delete

  • object<~String> - Name of object to delete


12
13
14
15
16
17
18
19
# File 'lib/fog/rackspace/requests/files/delete_object.rb', line 12

def delete_object(container, object)
  response = storage_request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "#{CGI.escape(container)}/#{CGI.escape(object)}"
  )
  response
end

#directoriesObject


9
10
11
# File 'lib/fog/rackspace/models/files/directories.rb', line 9

def directories
  Fog::Rackspace::Files::Directories.new(:connection => self)
end

#get_container(container, options = {}) ⇒ Object

Get details for container and total bytes stored

Parameters

  • container<~String> - Name of container to retrieve info for

  • options<~String>:

    • 'limit'<~String> - Maximum number of objects to return

    • 'marker'<~String> - Only return objects whose name is greater than marker

    • 'prefix'<~String> - Limits results to those starting with prefix

    • 'path'<~String> - Return objects nested in the pseudo path

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • 'X-Account-Container-Count'<~String> - Count of containers

      • 'X-Account-Bytes-Used'<~String> - Bytes used

    • body<~Array>:

      • 'bytes'<~Integer> - Number of bytes used by container

      • 'count'<~Integer> - Number of items in container

      • 'name'<~String> - Name of container

      • item<~Hash>:

        • 'bytes'<~String> - Size of object

        • 'content_type'<~String> Content-Type of object

        • 'hash'<~String> - Hash of object (etag?)

        • 'last_modified'<~String> - Last modified timestamp

        • 'name'<~String> - Name of object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/rackspace/requests/files/get_container.rb', line 31

def get_container(container, options = {})
  query = ''
  for key, value in options.merge!({ 'format' => 'json' })
    query << "#{key}=#{value}&"
  end
  query.chop!
  response = storage_request(
    :expects  => 200,
    :method   => 'GET',
    :path     => container,
    :query    => query
  )
  response
end

#get_containers(options = {}) ⇒ Object

List existing storage containers

Parameters

  • options<~Hash>:

    • 'limit'<~Integer> - Upper limit to number of results returned

    • 'marker'<~String> - Only return objects with name greater than this value

Returns

  • response<~Excon::Response>:

    • body<~Array>:

      • container<~Hash>:

        • 'bytes'<~Integer>: - Number of bytes used by container

        • 'count'<~Integer>: - Number of items in container

        • 'name'<~String>: - Name of container


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/rackspace/requests/files/get_containers.rb', line 20

def get_containers(options = {})
  options = { 'format' => 'json' }.merge!(options)
  query = ''
  for key, value in options
    query << "#{key}=#{CGI.escape(value)}&"
  end
  query.chop!
  response = storage_request(
    :expects  => [200, 204],
    :method   => 'GET',
    :path     => '',
    :query    => query
  )
  response
end

#get_object(container, object, &block) ⇒ Object

Get details for object

Parameters

  • container<~String> - Name of container to look in

  • object<~String> - Name of object to look for


12
13
14
15
16
17
18
19
20
# File 'lib/fog/rackspace/requests/files/get_object.rb', line 12

def get_object(container, object, &block)
  response = storage_request({
    :block    => block,
    :expects  => 200,
    :method   => 'GET',
    :path     => "#{CGI.escape(container)}/#{CGI.escape(object)}"
  }, false)
  response
end

#head_container(container) ⇒ Object

List number of objects and total bytes stored

Parameters

  • container<~String> - Name of container to retrieve info for

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • 'X-Container-Object-Count'<~String> - Count of containers

      • 'X-Container-Bytes-Used'<~String> - Bytes used


16
17
18
19
20
21
22
23
24
# File 'lib/fog/rackspace/requests/files/head_container.rb', line 16

def head_container(container)
  response = storage_request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => container,
    :query    => 'format=json'
  )
  response
end

#head_containersObject

List number of containers and total bytes stored

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • 'X-Account-Container-Count'<~String> - Count of containers

      • 'X-Account-Bytes-Used'<~String> - Bytes used


13
14
15
16
17
18
19
20
21
# File 'lib/fog/rackspace/requests/files/head_containers.rb', line 13

def head_containers
  response = storage_request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => '',
    :query    => 'format=json'
  )
  response
end

#head_object(container, object) ⇒ Object

Get headers for object

Parameters

  • container<~String> - Name of container to look in

  • object<~String> - Name of object to look for


12
13
14
15
16
17
18
19
# File 'lib/fog/rackspace/requests/files/head_object.rb', line 12

def head_object(container, object)
  response = storage_request({
    :expects  => 200,
    :method   => 'GET',
    :path     => "#{CGI.escape(container)}/#{CGI.escape(object)}"
  }, false)
  response
end

#put_container(name) ⇒ Object

Create a new container

Parameters

  • name<~String> - Name for container, should be < 256 bytes and must not contain '/'


11
12
13
14
15
16
17
18
# File 'lib/fog/rackspace/requests/files/put_container.rb', line 11

def put_container(name)
  response = storage_request(
    :expects  => [201, 202],
    :method   => 'PUT',
    :path     => CGI.escape(name)
  )
  response
end

#put_object(container, object, data) ⇒ Object

Create a new object

Parameters

  • container<~String> - Name for container, should be < 256 bytes and must not contain '/'


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/rackspace/requests/files/put_object.rb', line 11

def put_object(container, object, data)
  data = Fog::Rackspace::Files.parse_data(data)
  response = storage_request(
    :body     => data[:body],
    :expects  => 201,
    :headers  => data[:headers],
    :method   => 'PUT',
    :path     => "#{CGI.escape(container)}/#{CGI.escape(object)}"
  )
  response
end

#storage_request(params, parse_json = true) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fog/rackspace/files.rb', line 112

def storage_request(params, parse_json = true)
  @storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}")
  response = @storage_connection.request({
    :body     => params[:body],
    :expects  => params[:expects],
    :headers  => {
      'Content-Type' => 'application/json',
      'X-Auth-Token' => @auth_token
    }.merge!(params[:headers] || {}),
    :host     => @storage_host,
    :method   => params[:method],
    :path     => "#{@storage_path}/#{params[:path]}",
    :query    => params[:query]
  })
  if !response.body.empty? && parse_json
    response.body = JSON.parse(response.body)
  end
  response
end