Class: Fog::Storage::OpenStack::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/storage.rb,
lib/fog/openstack/requests/storage/get_object.rb,
lib/fog/openstack/requests/storage/put_object.rb,
lib/fog/openstack/requests/storage/copy_object.rb,
lib/fog/openstack/requests/storage/head_object.rb,
lib/fog/openstack/requests/storage/delete_object.rb,
lib/fog/openstack/requests/storage/get_container.rb,
lib/fog/openstack/requests/storage/put_container.rb,
lib/fog/openstack/requests/storage/get_containers.rb,
lib/fog/openstack/requests/storage/head_container.rb,
lib/fog/openstack/requests/storage/head_containers.rb,
lib/fog/openstack/requests/storage/delete_container.rb,
lib/fog/openstack/requests/storage/put_object_manifest.rb,
lib/fog/openstack/requests/storage/get_object_https_url.rb,
lib/fog/openstack/requests/storage/post_set_meta_temp_url_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fog/openstack/storage.rb', line 76

def initialize(options={})
  require 'mime/types'
  @openstack_api_key = options[:openstack_api_key]
  @openstack_username = options[:openstack_username]
  @openstack_auth_url = options[:openstack_auth_url]
  @openstack_auth_token = options[:openstack_auth_token]
  @openstack_storage_url = options[:openstack_storage_url]
  @openstack_must_reauthenticate = false
  @openstack_service_type = options[:openstack_service_type] || 'object-store'
  @openstack_service_name = options[:openstack_service_name]
  @openstack_region       = options[:openstack_region]
  @openstack_tenant       = options[:openstack_tenant]
  @connection_options     = options[:connection_options] || {}
  @openstack_temp_url_key = options[:openstack_temp_url_key]
  authenticate
  @persistent = options[:persistent] || false
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Method Details

#change_account(account) ⇒ Object

Change the current account while re-using the auth token.

This is usefull when you have an admin role and you’re able to HEAD other user accounts, set quotas, list files, etc.

For example:

# List current user account details
service = Fog::Storage[:openstack]
service.request :method => 'HEAD'

Would return something like:

Account:                      AUTH_1234
Date:                         Tue, 05 Mar 2013 16:50:52 GMT
X-Account-Bytes-Used:         0 (0.00 Bytes)
X-Account-Container-Count:    0
X-Account-Object-Count:       0

Now let’s change the account

service.('AUTH_3333')
service.request :method => 'HEAD'

Would return something like:

Account:                      AUTH_3333
Date:                         Tue, 05 Mar 2013 16:51:53 GMT
X-Account-Bytes-Used:         23423433
X-Account-Container-Count:    2
X-Account-Object-Count:       10

If we wan’t to go back to our original admin account:

service.


135
136
137
138
139
# File 'lib/fog/openstack/storage.rb', line 135

def ()
  @original_path ||= @path
  version_string = @path.split('/')[1]
  @path = "/#{version_string}/#{}"
end

#copy_object(source_container_name, source_object_name, target_container_name, target_object_name, options = {}) ⇒ Object

Copy object

Parameters

  • source_container_name<~String> - Name of source bucket

  • source_object_name<~String> - Name of source object

  • target_container_name<~String> - Name of bucket to create copy in

  • target_object_name<~String> - Name for new copy of object

  • options<~Hash> - Additional headers



14
15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/storage/copy_object.rb', line 14

def copy_object(source_container_name, source_object_name, target_container_name, target_object_name, options={})
  headers = { 'X-Copy-From' => "/#{source_container_name}/#{source_object_name}" }.merge(options)
  request({
    :expects  => 201,
    :headers  => headers,
    :method   => 'PUT',
    :path     => "#{Fog::OpenStack.escape(target_container_name)}/#{Fog::OpenStack.escape(target_object_name)}"
  })
end

#delete_container(name) ⇒ Object

Delete an existing container

Parameters

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



11
12
13
14
15
16
17
# File 'lib/fog/openstack/requests/storage/delete_container.rb', line 11

def delete_container(name)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => Fog::OpenStack.escape(name)
  )
end

#delete_object(container, object) ⇒ Object

Delete an existing object

Parameters

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

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



12
13
14
15
16
17
18
# File 'lib/fog/openstack/requests/storage/delete_object.rb', line 12

def delete_object(container, object)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}"
  )
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
# File 'lib/fog/openstack/requests/storage/get_container.rb', line 31

def get_container(container, options = {})
  options = options.reject {|key, value| value.nil?}
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => Fog::OpenStack.escape(container),
    :query    => {'format' => 'json'}.merge!(options)
  )
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
# File 'lib/fog/openstack/requests/storage/get_containers.rb', line 20

def get_containers(options = {})
  options = options.reject {|key, value| value.nil?}
  request(
    :expects  => [200, 204],
    :method   => 'GET',
    :path     => '',
    :query    => {'format' => 'json'}.merge!(options)
  )
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
21
22
23
24
# File 'lib/fog/openstack/requests/storage/get_object.rb', line 12

def get_object(container, object, &block)
  params = {}

  if block_given?
    params[:response_block] = Proc.new
  end

  request(params.merge!({
    :expects  => 200,
    :method   => 'GET',
    :path     => "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}"
  }), false)
end

#get_object_https_url(container, object, expires, options = {}) ⇒ Object

Get an expiring object https url from Cloud Files

Parameters

  • container<~String> - Name of container containing object

  • object<~String> - Name of object to get expiring url for

  • expires<~Time> - An expiry time for this url

Returns

  • response<~Excon::Response>:

    • body<~String> - url for object

See Also

docs.rackspace.com/files/api/v1/cf-devguide/content/Create_TempURL-d1a444.html



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/openstack/requests/storage/get_object_https_url.rb', line 20

def get_object_https_url(container, object, expires, options = {})
  if @openstack_temp_url_key.nil?
    raise ArgumentError, "Storage must my instantiated with the :openstack_temp_url_key option"
  end

  method         = 'GET'
  expires        = expires.to_i
  object_path_escaped   = "#{@path}/#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object,"/")}"
  object_path_unescaped = "#{@path}/#{Fog::OpenStack.escape(container)}/#{object}"
  string_to_sign = "#{method}\n#{expires}\n#{object_path_unescaped}"

  hmac = Fog::HMAC.new('sha1', @openstack_temp_url_key)
  sig  = sig_to_hex(hmac.sign(string_to_sign))

  "https://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
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
# File 'lib/fog/openstack/requests/storage/head_container.rb', line 16

def head_container(container)
  request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => Fog::OpenStack.escape(container),
    :query    => {'format' => 'json'}
  )
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
# File 'lib/fog/openstack/requests/storage/head_containers.rb', line 13

def head_containers
  request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => '',
    :query    => {'format' => 'json'}
  )
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
# File 'lib/fog/openstack/requests/storage/head_object.rb', line 12

def head_object(container, object)
  request({
    :expects  => 200,
    :method   => 'HEAD',
    :path     => "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}"
  }, false)
end

#post_set_meta_temp_url_key(key) ⇒ Object

Set the account wide Temp URL Key. This is a secret key that’s used to generate signed expiring URLs.

Once the key has been set with this request you should create new Storage objects with the :openstack_temp_url_key option then use the get_object_https_url method to generate expiring URLs.

*** CAUTION *** changing this secret key will invalidate any expiring URLS generated with old keys.

Parameters

  • key<~String> - The new Temp URL Key

Returns

  • response<~Excon::Response>

See Also

docs.rackspace.com/files/api/v1/cf-devguide/content/Set_Account_Metadata-d1a4460.html



25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/storage/post_set_meta_temp_url_key.rb', line 25

def post_set_meta_temp_url_key(key)
  request(
    :expects  => [201, 202, 204],
    :method   => 'POST',
    :headers  => {'X-Account-Meta-Temp-Url-Key' => key}
  )
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
# File 'lib/fog/openstack/requests/storage/put_container.rb', line 11

def put_container(name)
  request(
    :expects  => [201, 202],
    :method   => 'PUT',
    :path     => Fog::OpenStack.escape(name)
  )
end

#put_object(container, object, data, options = {}, &block) ⇒ Object

Create a new object

Parameters

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

  • object<~String> - Name for object

  • data<~String|File> - data to upload

  • options<~Hash> - config headers for object. Defaults to {}.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/openstack/requests/storage/put_object.rb', line 14

def put_object(container, object, data, options = {}, &block)
  data = Fog::Storage.parse_data(data)
  headers = data[:headers].merge!(options)

  params = block_given? ? { :request_block => block } : { :body => data[:body] }

  params.merge!(
    :expects    => 201,
    :idempotent => !params[:request_block],
    :headers    => headers,
    :method     => 'PUT',
    :path       => "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}"
  )

  request(params)
end

#put_object_manifest(container, object, options = {}) ⇒ Object

Create a new manifest object

Creates an object with a X-Object-Manifest header that specifies the common prefix (“<container>/<prefix>”) for all uploaded segments. Retrieving the manifest object streams all segments matching this prefix. Segments must sort in the order they should be concatenated. Note that any future objects stored in the container along with the segments that match the prefix will be included when retrieving the manifest object.

All segments must be stored in the same container, but may be in a different container than the manifest object. The default X-Object-Manifest header is set to “container/object”, but may be overridden in options to specify the prefix and/or the container where segments were stored. If overridden, names should be CGI escaped (excluding spaces) if needed (see Rackspace.escape).

Parameters:

  • container (String)

    Name for container where object will be stored. Should be < 256 bytes and must not contain ‘/’

  • object (String)

    Name for manifest object.

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

    Config headers for object.

Options Hash (options):

  • 'X-Object-Manifest' (String) — default: "container/object"

    “<container>/<prefix>” for segment objects.

See Also:



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/storage/put_object_manifest.rb', line 24

def put_object_manifest(container, object, options = {})
  path = "#{Fog::OpenStack.escape(container)}/#{Fog::OpenStack.escape(object)}"
  headers = {'X-Object-Manifest' => path}.merge(options)
  request(
    :expects  => 201,
    :headers  => headers,
    :method   => 'PUT',
    :path     => path
  )
end

#reloadObject



95
96
97
# File 'lib/fog/openstack/storage.rb', line 95

def reload
  @connection.reset
end

#request(params, parse_json = true, &block) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fog/openstack/storage.rb', line 145

def request(params, parse_json = true, &block)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}",
    }), &block)
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Storage::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

#reset_account_nameObject



141
142
143
# File 'lib/fog/openstack/storage.rb', line 141

def 
  @path = @original_path
end