Class: Openstack::Swift::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack-swift/client.rb

Constant Summary collapse

SWIFT_API =
Openstack::Swift::Api
MAX_SIZE =
4 * 1024 ** 3

Instance Method Summary collapse

Constructor Details

#initialize(proxy, user, password) ⇒ Client

Initialize method It uses the authenticate method to store the tokens for future requests



10
11
12
13
# File 'lib/openstack-swift/client.rb', line 10

def initialize(proxy, user, password)
  @proxy, @user, @password = proxy, user, password
  authenticate!
end

Instance Method Details

#account_infoObject

Returns the following informations about the object:

bytes_used: Number of bytes used by this account
object_count: Number of objects that this account have allocated
container_count: Number of container


33
34
35
36
37
38
39
40
# File 'lib/openstack-swift/client.rb', line 33

def 
  headers = SWIFT_API.(@url, @token)
  {
    "bytes_used" => headers["x-account-bytes-used"],
    "object_count" => headers["x-account-object-count"],
    "container_count" => headers["x-account-container-count"]
  }
end

#authenticate!Object

Authentication method It stores the authentication url and token for future commands avoiding to request a new token for each request It should be used to force a new token



19
20
21
22
23
24
25
26
27
# File 'lib/openstack-swift/client.rb', line 19

def authenticate!
  @url, _, @token = SWIFT_API.auth(@proxy, @user, @password)

  if @url.blank? or @token.blank?
    raise AuthenticationError
  else
    true
  end
end

#download(container, object) ⇒ Object

This method downloads a object from a given container



96
97
98
# File 'lib/openstack-swift/client.rb', line 96

def download(container, object)
  SWIFT_API.download_object(@url, @token, container, object)
end

#object_info(container, object) ⇒ Object

Returns the following informations about the account:

last_modified
md5
content_type
manifest
content_length


48
49
50
51
52
53
54
55
56
57
# File 'lib/openstack-swift/client.rb', line 48

def object_info(container, object)
  headers = SWIFT_API.object_stat(@url, @token, container, object)
  {
    "last_modified" => headers["last-modified"],
    "md5" => headers["etag"],
    "content_type" => headers["content-type"],
    "manifest" => headers["x-object-manifest"],
    "content_length" => headers["content-length"]
  }
end

#upload(container, file_path, options = {}) ⇒ Object

This method uploads a file from a given to a given container



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openstack-swift/client.rb', line 60

def upload(container, file_path, options={})
  options[:segments_size] ||= MAX_SIZE

  SWIFT_API.create_container(@url, @token, container) rescue nil

  file_name, file_mtime, file_size  = file_info(file_path)

  if file_size > options[:segments_size]
    SWIFT_API.create_container(@url, @token, "#{container}_segments") rescue nil

    segments_minus_one = file_size / options[:segments_size]
    last_piece = file_size - segments_minus_one * options[:segments_size]
    segments_minus_one.times do |segment|
      upload_path_for(file_path, segment)
      SWIFT_API.upload_object(
        @url, @token, "#{container}_segments", file_path,
        :size => options[:segments_size],
        :position => options[:segments_size] * segment,
        :object_name => segment_path
      )
    end

    SWIFT_API.upload_object(
      @url, @token, "#{container}_segments", file_path,
      :size => last_piece,
      :position => options[:segments_size] * segments_minus_one,
      :object_name => upload_path_for(file_path, segments_minus_one)
    )

    SWIFT_API.create_manifest(@url, @token, container, file_path)
  else
    SWIFT_API.upload_object(@url, @token, container, file_path)
  end
end