Class: TOS::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/tos/bucket.rb

Overview

All operations are bucket-scoped: get a bucket via ‘client.bucket(name)` then call put_object / get_object / etc. on it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, name:) ⇒ Bucket

Returns a new instance of Bucket.

Raises:



11
12
13
14
15
16
# File 'lib/tos/bucket.rb', line 11

def initialize(client:, name:)
  raise ConfigError, "bucket name is required" if name.to_s.empty?

  @client = client
  @name = name.to_s
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/tos/bucket.rb', line 9

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/tos/bucket.rb', line 9

def name
  @name
end

Instance Method Details

#delete_multiple_objects(keys, quiet: true) ⇒ Object

Deletes up to 1000 objects in a single request.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tos/bucket.rb', line 65

def delete_multiple_objects(keys, quiet: true)
  keys = Array(keys).reject { |k| k.to_s.empty? }
  return [] if keys.empty?

  body = build_delete_payload(keys, quiet)
  content_md5 = OpenSSL::Digest::MD5.base64digest(body)

  client.request(
    method: :post,
    host: host,
    path: "/",
    query: { "delete" => "" },
    headers: { "Content-Type" => "application/xml", "Content-MD5" => content_md5 },
    body: body,
  )
end

#delete_object(key) ⇒ Object



41
42
43
# File 'lib/tos/bucket.rb', line 41

def delete_object(key)
  client.request(method: :delete, host: host, path: path_for(key))
end

#get_object(key, range: nil, headers: {}, &block) ⇒ Object

Returns the response with the body loaded. For large objects, pass a block which receives streamed chunks.



30
31
32
33
34
35
# File 'lib/tos/bucket.rb', line 30

def get_object(key, range: nil, headers: {}, &block)
  h = headers.dup
  h["Range"] = format_range(range) if range

  client.request(method: :get, host: host, path: path_for(key), headers: h, stream: block)
end

#head_object(key, headers: {}) ⇒ Object



37
38
39
# File 'lib/tos/bucket.rb', line 37

def head_object(key, headers: {})
  client.request(method: :head, host: host, path: path_for(key), headers: headers)
end

#hostObject



82
83
84
# File 'lib/tos/bucket.rb', line 82

def host
  client.host_for(name)
end

#list_objects(prefix: nil, max_keys: 1000, continuation_token: nil) ⇒ Object

Lists up to ‘max_keys` objects under `prefix`. Pass the previous response’s ‘next_continuation_token` to page through results.

Returns a Hash:

{
  keys: ["foo/bar.txt", ...],
  is_truncated: true|false,
  next_continuation_token: "...",
  raw: TOS::Response
}


55
56
57
58
59
60
61
62
# File 'lib/tos/bucket.rb', line 55

def list_objects(prefix: nil, max_keys: 1000, continuation_token: nil)
  query = { "list-type" => "2", "max-keys" => max_keys.to_s }
  query["prefix"] = prefix if prefix
  query["continuation-token"] = continuation_token if continuation_token

  res = client.request(method: :get, host: host, path: "/", query: query)
  parse_list_response(res)
end

#path_for(key) ⇒ Object



86
87
88
# File 'lib/tos/bucket.rb', line 86

def path_for(key)
  "/#{client.escape_key(key)}"
end

#put_object(key, body, content_type: nil, content_md5: nil, metadata: {}, headers: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tos/bucket.rb', line 18

def put_object(key, body, content_type: nil, content_md5: nil, metadata: {}, headers: {})
  h = headers.dup
  h["Content-Type"] = content_type if content_type
  h["Content-MD5"] = content_md5 if content_md5
  .each { |k, v| h["x-tos-meta-#{k}"] = v.to_s }

  body_str = body.respond_to?(:read) ? body.read : body.to_s
  client.request(method: :put, host: host, path: path_for(key), headers: h, body: body_str)
end