Class: SwiftClient

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_client.rb,
lib/swift_client/version.rb

Defined Under Namespace

Classes: AuthenticationError, EmptyNameError, NullCache, OptionError, ResponseError, TempUrlKeyMissing

Constant Summary collapse

VERSION =
"0.1.6"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SwiftClient

Returns a new instance of SwiftClient.

Raises:



31
32
33
34
35
36
37
38
# File 'lib/swift_client.rb', line 31

def initialize(options = {})
  raise(OptionError, "Setting expires_in connection wide is deprecated") if options[:expires_in]

  self.options = options
  self.cache_store = options[:cache_store] || SwiftClient::NullCache.new

  authenticate
end

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



29
30
31
# File 'lib/swift_client.rb', line 29

def auth_token
  @auth_token
end

#cache_storeObject

Returns the value of attribute cache_store.



29
30
31
# File 'lib/swift_client.rb', line 29

def cache_store
  @cache_store
end

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/swift_client.rb', line 29

def options
  @options
end

#storage_urlObject

Returns the value of attribute storage_url.



29
30
31
# File 'lib/swift_client.rb', line 29

def storage_url
  @storage_url
end

Instance Method Details

#bulk_delete(items, options = {}) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/swift_client.rb', line 163

def bulk_delete(items, options = {})
  items.each_slice(1_000) do |slice|
    request :delete, "/?bulk-delete", options.merge(:body => slice.join("\n"), :headers => { "Content-Type" => "text/plain" })
  end

  items
end

#delete_container(container_name, options = {}) ⇒ Object

Raises:



88
89
90
91
92
# File 'lib/swift_client.rb', line 88

def delete_container(container_name, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :delete, "/#{container_name}", options
end

#delete_object(object_name, container_name, options = {}) ⇒ Object

Raises:



129
130
131
132
133
# File 'lib/swift_client.rb', line 129

def delete_object(object_name, container_name, options = {})
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  request :delete, "/#{container_name}/#{object_name}", options
end

#get_container(container_name, query = {}, options = {}) ⇒ Object

Raises:



60
61
62
63
64
# File 'lib/swift_client.rb', line 60

def get_container(container_name, query = {}, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :get, "/#{container_name}", options.merge(:query => query)
end

#get_containers(query = {}, options = {}) ⇒ Object



52
53
54
# File 'lib/swift_client.rb', line 52

def get_containers(query = {}, options = {})
  request :get, "/", options.merge(:query => query)
end

#get_object(object_name, container_name, options = {}, &block) ⇒ Object

Raises:



117
118
119
120
121
# File 'lib/swift_client.rb', line 117

def get_object(object_name, container_name, options = {}, &block)
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  request(:get, "/#{container_name}/#{object_name}", options.merge(block ? { :stream_body => true } : {}), &block)
end

#get_objects(container_name, query = {}, options = {}) ⇒ Object

Raises:



135
136
137
138
139
# File 'lib/swift_client.rb', line 135

def get_objects(container_name, query = {}, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :get, "/#{container_name}", options.merge(:query => query)
end

#head_account(options = {}) ⇒ Object



40
41
42
# File 'lib/swift_client.rb', line 40

def (options = {})
  request :head, "/", options
end

#head_container(container_name, options = {}) ⇒ Object

Raises:



70
71
72
73
74
# File 'lib/swift_client.rb', line 70

def head_container(container_name, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :head, "/#{container_name}", options
end

#head_containers(options = {}) ⇒ Object



48
49
50
# File 'lib/swift_client.rb', line 48

def head_containers(options = {})
  request :head, "/", options
end

#head_object(object_name, container_name, options = {}) ⇒ Object

Raises:



123
124
125
126
127
# File 'lib/swift_client.rb', line 123

def head_object(object_name, container_name, options = {})
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  request :head, "/#{container_name}/#{object_name}", options
end

#paginate_container(container_name, query = {}, options = {}, &block) ⇒ Object



66
67
68
# File 'lib/swift_client.rb', line 66

def paginate_container(container_name, query = {}, options = {}, &block)
  paginate(:get_container, container_name, query, options, &block)
end

#paginate_containers(query = {}, options = {}, &block) ⇒ Object



56
57
58
# File 'lib/swift_client.rb', line 56

def paginate_containers(query = {}, options = {}, &block)
  paginate(:get_containers, query, options, &block)
end

#paginate_objects(container_name, query = {}, options = {}, &block) ⇒ Object



141
142
143
# File 'lib/swift_client.rb', line 141

def paginate_objects(container_name, query = {}, options = {}, &block)
  paginate(:get_objects, container_name, query, options, &block)
end

#post_account(headers = {}, options = {}) ⇒ Object



44
45
46
# File 'lib/swift_client.rb', line 44

def (headers = {}, options = {})
  request :post, "/", options.merge(:headers => headers)
end

#post_container(container_name, headers = {}, options = {}) ⇒ Object

Raises:



82
83
84
85
86
# File 'lib/swift_client.rb', line 82

def post_container(container_name, headers = {}, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :post, "/#{container_name}", options.merge(:headers => headers)
end

#post_object(object_name, container_name, headers = {}, options = {}) ⇒ Object

Raises:



111
112
113
114
115
# File 'lib/swift_client.rb', line 111

def post_object(object_name, container_name, headers = {}, options = {})
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  request :post, "/#{container_name}/#{object_name}", options.merge(:headers => headers)
end

#public_url(object_name, container_name) ⇒ Object

Raises:



145
146
147
148
149
# File 'lib/swift_client.rb', line 145

def public_url(object_name, container_name)
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  "#{storage_url}/#{container_name}/#{object_name}"
end

#put_container(container_name, headers = {}, options = {}) ⇒ Object

Raises:



76
77
78
79
80
# File 'lib/swift_client.rb', line 76

def put_container(container_name, headers = {}, options = {})
  raise(EmptyNameError) if container_name.empty?

  request :put, "/#{container_name}", options.merge(:headers => headers)
end

#put_object(object_name, data_or_io, container_name, headers = {}, options = {}) ⇒ Object

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/swift_client.rb', line 94

def put_object(object_name, data_or_io, container_name, headers = {}, options = {})
  raise(EmptyNameError) if object_name.empty? || container_name.empty?

  mime_type = MIME::Types.of(object_name).first

  extended_headers = (headers || {}).dup

  unless find_header_key(extended_headers, "Content-Type")
    extended_headers["Content-Type"] = mime_type.content_type if mime_type
    extended_headers["Content-Type"] ||= "application/octet-stream"
  end

  extended_headers["Transfer-Encoding"] = "chunked"

  request :put, "/#{container_name}/#{object_name}", options.merge(:body_stream => data_or_io.respond_to?(:read) ? data_or_io : StringIO.new(data_or_io), :headers => extended_headers)
end

#temp_url(object_name, container_name, opts = {}) ⇒ Object

Raises:



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/swift_client.rb', line 151

def temp_url(object_name, container_name, opts = {})
  raise(EmptyNameError) if object_name.empty? || container_name.empty?
  raise(TempUrlKeyMissing) unless options[:temp_url_key]

  expires = (Time.now + (opts[:expires_in] || 3600).to_i).to_i
  path = URI.parse("#{storage_url}/#{container_name}/#{object_name}").path

  signature = OpenSSL::HMAC.hexdigest("sha1", options[:temp_url_key], "GET\n#{expires}\n#{path}")

  "#{storage_url}/#{container_name}/#{object_name}?temp_url_sig=#{signature}&temp_url_expires=#{expires}"
end