Class: SwiftClient
- Inherits:
-
Object
- Object
- SwiftClient
- Defined in:
- lib/client.rb
Class Method Summary collapse
- .delete_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
- .delete_object(url, token = nil, container = nil, name = nil, http_conn = nil, headers = {}, proxy = nil) ⇒ Object
- .get_account(url, token, marker = nil, limit = nil, prefix = nil, full_listing = false, http_conn = nil) ⇒ Object
- .get_auth(url, user, key, snet = false) ⇒ Object
- .get_container(url, token, container, marker = nil, limit = nil, prefix = nil, delimiter = nil, full_listing = false, http_conn = nil) ⇒ Object
- .get_object(url, token, container, name, http_conn = nil, resp_chunk_size = nil, &block) ⇒ Object
- .head_account(url, token, http_conn = nil) ⇒ Object
- .head_container(url, token, container, http_conn = nil) ⇒ Object
- .head_object(url, token, container, name, http_conn = nil) ⇒ Object
- .http_connection(url, proxy_host = nil, proxy_port = nil) ⇒ Object
- .post_account(url, token, headers, http_conn = nil) ⇒ Object
- .post_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
- .post_object(url, token = nil, container = nil, name = nil, headers = {}, http_conn = nil) ⇒ Object
- .put_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
- .put_object(url, token = nil, container = nil, name = nil, contents = nil, content_length = nil, etag = nil, chunk_size = nil, content_type = nil, headers = {}, http_conn = nil, proxy = nil) ⇒ Object
Instance Method Summary collapse
- #delete_container(container) ⇒ Object
- #delete_object(container, name, headers = {}) ⇒ Object
- #get_account(marker = nil, limit = nil, prefix = nil, full_listing = false) ⇒ Object
- #get_auth ⇒ Object
- #get_container(container, marker = nil, limit = nil, prefix = nil, delimiter = nil, full_listing = nil) ⇒ Object
- #get_object(container, name, resp_chunk_size = nil) ⇒ Object
- #head_account ⇒ Object
- #head_container(container) ⇒ Object
- #head_object(container, name) ⇒ Object
- #http_connection ⇒ Object
-
#initialize(authurl, user, key, retries = 5, preauthurl = nil, preauthtoken = nil, snet = false, starting_backoff = 1) ⇒ SwiftClient
constructor
A new instance of SwiftClient.
- #post_account(headers = nil) ⇒ Object
- #post_container(container, headers = {}) ⇒ Object
- #post_object(container, name, headers = {}) ⇒ Object
- #put_container(container, headers = {}) ⇒ Object
- #put_object(container, obj, contents, content_length = nil, etag = nil, chunk_size = 65536, content_type = nil, headers = {}) ⇒ Object
Constructor Details
#initialize(authurl, user, key, retries = 5, preauthurl = nil, preauthtoken = nil, snet = false, starting_backoff = 1) ⇒ SwiftClient
Returns a new instance of SwiftClient.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/client.rb', line 92 def initialize(authurl, user, key, retries=5, preauthurl=nil, preauthtoken=nil, snet=false, starting_backoff=1) @authurl = authurl @user = user @key = key @retries = retries @http_conn = nil @url = preauthurl @token = preauthtoken @attempts = 0 @snet = snet @starting_backoff = starting_backoff end |
Class Method Details
.delete_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/client.rb', line 418 def self.delete_container(url, token, container, headers={}, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? parsed.path += "/#{quote(container)}" headers['x-auth-token'] = token resp = conn.delete(parsed.request_uri, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Container DELETE failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end end |
.delete_object(url, token = nil, container = nil, name = nil, http_conn = nil, headers = {}, proxy = nil) ⇒ Object
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/client.rb', line 595 def self.delete_object(url, token=nil, container=nil, name=nil, http_conn=nil, headers={}, proxy=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? parsed.path += "/#{quote(container)}" if container parsed.path += "/#{quote(name)}" if name headers['x-auth-token'] = token if token resp = conn.delete(parsed.request_uri, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Object DELETE failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end end |
.get_account(url, token, marker = nil, limit = nil, prefix = nil, full_listing = false, http_conn = nil) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/client.rb', line 198 def self.get_account(url, token, marker=nil, limit=nil, prefix=nil, full_listing=false, http_conn=nil) #todo: add in rest of functionality if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] if full_listing rv = get_account(url, token, marker, limit, prefix, false, http_conn) listing = rv[1] while listing.length > 0 marker = listing[-1]['name'] listing = get_account(url, token, marker, limit, prefix, false, http_conn)[1] if listing.length > 0 rv[1] += listing end end return rv end query = Query.new(parsed.query) query.add('format', 'json') query.add('marker', quote(marker.to_s)) if marker query.add('limit', quote(limit.to_s)) if limit query.add('prefix', quote(prefix.to_s)) if prefix parsed.query = query.to_url_params conn.start if !conn.started? resp = conn.get(parsed.request_uri, {'x-auth-token' => token}) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end if resp.code.to_i == 204 [resp_headers, []] else [resp_headers, JSON.parse(resp.body)] end end |
.get_auth(url, user, key, snet = false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/client.rb', line 177 def self.get_auth(url, user, key, snet=false) parsed, conn = http_connection(url) conn.start if not conn.started? resp = conn.get(URI.encode(parsed.request_uri), {"x-auth-user" => user, "x-auth-key" => key }) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Account GET failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code, :http_reason=>resp.) end url = URI::parse(resp.header['x-storage-url']) if snet url.host = "snet-#{url.host}" end [url.to_s, resp.header['x-auth-token'], resp.header] end |
.get_container(url, token, container, marker = nil, limit = nil, prefix = nil, delimiter = nil, full_listing = false, http_conn = nil) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/client.rb', line 293 def self.get_container(url, token, container, marker=nil, limit=nil, prefix=nil, delimiter=nil, full_listing=false, http_conn=nil) #todo: add in rest of functionality if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] if full_listing rv = get_container(url, token, container, marker, limit, prefix, delimiter, false, http_conn) listing = rv[1] while listing.length > 0 marker = listing[-1]['name'] listing = get_container(url, token, container, marker, limit, prefix, delimiter, false, http_conn)[1] if listing.length > 0 rv[1] += listing end end return rv end query = Query.new(parsed.query) query.add('format', 'json') query.add('marker', quote(marker.to_s)) if marker query.add('limit', quote(limit.to_s)) if limit query.add('prefix', quote(prefix.to_s)) if prefix parsed.query = query.to_url_params conn.start if !conn.started? parsed.path += "/#{quote(container)}" resp = conn.get(parsed.request_uri, {'x-auth-token' => token}) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Container GET failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_query=>parsed.query, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end if resp.code.to_i == 204 [resp_headers, []] else [resp_headers, JSON.parse(resp.body())] end end |
.get_object(url, token, container, name, http_conn = nil, resp_chunk_size = nil, &block) ⇒ Object
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/client.rb', line 441 def self.get_object(url, token, container, name, http_conn=nil, resp_chunk_size=nil, &block) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] parsed.path += "/#{quote(container)}/#{quote(name)}" conn.start if not conn.started? headers = {'x-auth-token' => token} if block_given? resp = conn.request_get(parsed.request_uri, headers) do |r| r.read_body do |b| yield b end end object_body = nil else resp = conn.request_get(parsed.request_uri, headers) object_body = resp.body end if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Object GET failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end [resp_headers, object_body] end |
.head_account(url, token, http_conn = nil) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/client.rb', line 247 def self.head_account(url, token, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? resp = conn.head(parsed.request_uri, {'x-auth-token' => token}) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Account HEAD failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end resp_headers end |
.head_container(url, token, container, http_conn = nil) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/client.rb', line 344 def self.head_container(url, token, container, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? parsed.path += "/#{quote(container)}" resp = conn.head(parsed.request_uri, {'x-auth-token' => token}) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Container HEAD failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end resp_headers end |
.head_object(url, token, container, name, http_conn = nil) ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/client.rb', line 480 def self.head_object(url, token, container, name, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] parsed.path += "/#{quote(container)}/#{quote(name)}" conn.start if not conn.started? resp = conn.head(parsed.request_uri, {'x-auth-token' => token}) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Object HEAD failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp_headers = {} resp.header.each do |k,v| resp_headers[k.downcase] = v end resp_headers end |
.http_connection(url, proxy_host = nil, proxy_port = nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/client.rb', line 150 def self.http_connection(url, proxy_host=nil, proxy_port=nil) parsed = URI::parse(url) if parsed.scheme == 'http' require 'net/http' conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port) [parsed, conn] elsif parsed.scheme == 'https' require 'net/https' conn = Net::HTTP::Proxy(proxy_host, proxy_port).new(parsed.host, parsed.port) conn.use_ssl = true conn.verify_mode = OpenSSL::SSL::VERIFY_NONE [parsed, conn] else raise ClientException.new( "Cannot handle protocol scheme #{parsed.scheme} for #{url} %s") end end |
.post_account(url, token, headers, http_conn = nil) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/client.rb', line 272 def self.post_account(url, token, headers, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] headers['x-auth-token'] = token conn.start if !conn.started? resp = conn.post(parsed.request_uri, nil, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Account POST failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp.body end |
.post_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/client.rb', line 395 def self.post_container(url, token, container, headers={}, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? parsed.path += "/#{quote(container)}" headers['x-auth-token'] = token resp = conn.post(parsed.request_uri, nil, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Container POST failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end end |
.post_object(url, token = nil, container = nil, name = nil, headers = {}, http_conn = nil) ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/client.rb', line 572 def self.post_object(url, token=nil, container=nil, name=nil, headers={}, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] parsed.path += "/#{quote(container)}" if container parsed.path += "/#{quote(name)}" if name headers['x-auth-token'] = token if token resp = conn.post(parsed.request_uri, nil, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Object POST failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end end |
.put_container(url, token, container, headers = {}, http_conn = nil) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/client.rb', line 371 def self.put_container(url, token, container, headers={}, http_conn=nil) if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] conn.start if !conn.started? parsed.path += "/#{quote(container)}" headers['x-auth-token'] = token # headers['content-length'] = 0 resp = conn.put(parsed.request_uri, nil, headers) if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Container PUT failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end end |
.put_object(url, token = nil, container = nil, name = nil, contents = nil, content_length = nil, etag = nil, chunk_size = nil, content_type = nil, headers = {}, http_conn = nil, proxy = nil) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/client.rb', line 508 def self.put_object(url, token=nil, container=nil, name=nil, contents=nil, content_length=nil, etag=nil, chunk_size=nil, content_type=nil, headers={}, http_conn=nil, proxy=nil) chunk_size ||= 65536 if not http_conn http_conn = http_connection(url) end parsed = http_conn[0].clone conn = http_conn[1] parsed.path += "/#{quote(container)}" if container parsed.path += "/#{quote(name)}" if name headers['x-auth-token'] = token if token headers['etag'] = etag if etag if content_length != nil headers['content-length'] = content_length.to_s else headers.each do |k,v| if k.downcase == 'content-length' content_length = v.to_i end end end headers['content-type'] = content_type if content_type headers['content-length'] = '0' if not contents if contents.respond_to? :read request = Net::HTTP::Put.new(parsed.request_uri, headers) chunked = ChunkedConnectionWrapper.new(contents, chunk_size) if content_length == nil request['Transfer-Encoding'] = 'chunked' request.delete('content-length') end request.body_stream = chunked resp = conn.start do |http| http.request(request) end else conn.start if not conn.started? resp = conn.put(parsed.request_uri, contents, headers) end if resp.code.to_i < 200 or resp.code.to_i > 300 raise ClientException.new('Object PUT failed', :http_scheme=>parsed.scheme, :http_host=>conn.address, :http_port=>conn.port, :http_path=>parsed.path, :http_status=>resp.code, :http_reason=>resp.) end resp.header['etag'] end |
Instance Method Details
#delete_container(container) ⇒ Object
437 438 439 |
# File 'lib/client.rb', line 437 def delete_container(container) _retry(nil, :delete_container, [container]) end |
#delete_object(container, name, headers = {}) ⇒ Object
615 616 617 |
# File 'lib/client.rb', line 615 def delete_object(container, name, headers={}) _retry(nil, :delete_object, [container, name, headers]) end |
#get_account(marker = nil, limit = nil, prefix = nil, full_listing = false) ⇒ Object
243 244 245 |
# File 'lib/client.rb', line 243 def get_account(marker=nil, limit=nil, prefix=nil, full_listing=false) _retry(nil, :get_account, [marker, limit, prefix, @http_conn, full_listing]) end |
#get_auth ⇒ Object
194 195 196 |
# File 'lib/client.rb', line 194 def get_auth @url, @token = SwiftClient.get_auth(@authurl, @user, @key, @snet) end |
#get_container(container, marker = nil, limit = nil, prefix = nil, delimiter = nil, full_listing = nil) ⇒ Object
340 341 342 |
# File 'lib/client.rb', line 340 def get_container(container, marker=nil, limit=nil, prefix=nil, delimiter=nil, full_listing=nil) _retry(nil, :get_container, [container, marker, limit, prefix, delimiter, full_listing]) end |
#get_object(container, name, resp_chunk_size = nil) ⇒ Object
476 477 478 |
# File 'lib/client.rb', line 476 def get_object(container, name, resp_chunk_size=nil) _retry(nil, :get_object, [container, name, resp_chunk_size]) end |
#head_account ⇒ Object
268 269 270 |
# File 'lib/client.rb', line 268 def head_account _retry(nil, :head_account, [@http_conn]) end |
#head_container(container) ⇒ Object
367 368 369 |
# File 'lib/client.rb', line 367 def head_container(container) _retry(nil, :head_container, [container, @http_conn]) end |
#head_object(container, name) ⇒ Object
504 505 506 |
# File 'lib/client.rb', line 504 def head_object(container, name) _retry(nil, :head_object, [container, name]) end |
#http_connection ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/client.rb', line 169 def http_connection if !@http_conn @http_conn = SwiftClient.http_connection(@url) else @http_conn end end |
#post_account(headers = nil) ⇒ Object
289 290 291 |
# File 'lib/client.rb', line 289 def post_account(headers=nil) _retry(nil, :post_account, [headers, @http_conn]) end |
#post_container(container, headers = {}) ⇒ Object
414 415 416 |
# File 'lib/client.rb', line 414 def post_container(container, headers={}) _retry(nil, :post_container, [container, headers]) end |
#post_object(container, name, headers = {}) ⇒ Object
591 592 593 |
# File 'lib/client.rb', line 591 def post_object(container, name, headers={}) _retry(nil, :post_object, [container, name, headers]) end |
#put_container(container, headers = {}) ⇒ Object
391 392 393 |
# File 'lib/client.rb', line 391 def put_container(container, headers={}) _retry(nil, :put_container, [container, headers, @http_conn]) end |
#put_object(container, obj, contents, content_length = nil, etag = nil, chunk_size = 65536, content_type = nil, headers = {}) ⇒ Object
557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/client.rb', line 557 def put_object(container, obj, contents, content_length=nil, etag=nil, chunk_size=65536, content_type=nil, headers={}) _default_reset = Proc.new do |args| raise ClientException("put_object(#{container}, #{obj}, ...) failure and no ability to reset contents for reupload.") end reset_func = _default_reset if (contents.respond_to? :seek) and (contents.respond_to? :tell) orig_pos = contents.tell reset_func = Proc.new {|a| contents.seek(orig_pos)} elsif !contents reset_func = Proc.new {|a| nil } end _retry(reset_func, :put_object, [container, obj, contents, content_length, etag, chunk_size, content_type, headers]) end |