Class: Canistor::Storage::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/storage/bucket.rb,
lib/canistor/storage/bucket/settings.rb

Overview

Holds information about a bucket and implements interaction with it.

Defined Under Namespace

Classes: Settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Bucket

Returns a new instance of Bucket.



22
23
24
25
26
27
28
# File 'lib/canistor/storage/bucket.rb', line 22

def initialize(**attributes)
  @settings = Settings.new
  clear
  attributes.each do |name, value|
    public_send("#{name}=", value)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/canistor/storage/bucket.rb', line 16

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



19
20
21
# File 'lib/canistor/storage/bucket.rb', line 19

def objects
  @objects
end

#regionObject

Returns the value of attribute region.



15
16
17
# File 'lib/canistor/storage/bucket.rb', line 15

def region
  @region
end

#settingsObject (readonly)

Returns the value of attribute settings.



18
19
20
# File 'lib/canistor/storage/bucket.rb', line 18

def settings
  @settings
end

#uploadsObject (readonly)

Returns the value of attribute uploads.



20
21
22
# File 'lib/canistor/storage/bucket.rb', line 20

def uploads
  @uploads
end

Instance Method Details

#[](name) ⇒ Object



36
37
38
# File 'lib/canistor/storage/bucket.rb', line 36

def [](name)
  @objects[name]
end

#[]=(name, value) ⇒ Object



40
41
42
# File 'lib/canistor/storage/bucket.rb', line 40

def []=(name, value)
  @objects[name] = value
end

#allow_access_to(access_key_ids) ⇒ Object



142
143
144
# File 'lib/canistor/storage/bucket.rb', line 142

def allow_access_to(access_key_ids)
  settings.allow_access_keys(access_key_ids)
end

#clearObject



126
127
128
129
130
131
# File 'lib/canistor/storage/bucket.rb', line 126

def clear
  @objects = Canistor::Storage::Objects.new(
    versioned: settings.versioned?
  )
  @uploads = {}
end

#delete(context, access_key_id, subject) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/canistor/storage/bucket.rb', line 115

def delete(context, access_key_id, subject)
  if !settings.access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif objects[subject.key]
    object = @objects.delete(subject.key)
    object.delete(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#dig(*segments) ⇒ Object



44
45
46
# File 'lib/canistor/storage/bucket.rb', line 44

def dig(*segments)
  @objects.dig(*segments)
end

#get(context, access_key_id, subject) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/canistor/storage/bucket.rb', line 58

def get(context, access_key_id, subject)
  params = CGI::parse(context.http_request.endpoint.query.to_s)
  catch(:rendered_error) do
    if !settings.access_keys.include?(access_key_id)
      Canistor::ErrorHandler.serve_access_denied(context, subject)
    elsif params.has_key?('uploads')
      list_bucket_uploads(context)
    elsif params.has_key?('uploadId')
      list_bucket_upload_parts(context, subject, params)
    elsif subject.key.nil? || subject.key == ''
      list_bucket(context)
    elsif object = objects[subject.key]
      object.get(context, subject)
    else
      Canistor::ErrorHandler.serve_no_such_key(context, subject)
    end
  end
end

#head(context, access_key_id, subject) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/canistor/storage/bucket.rb', line 48

def head(context, access_key_id, subject)
  if !settings.access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key]
    object.head(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#post(context, access_key_id, subject) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/canistor/storage/bucket.rb', line 95

def post(context, access_key_id, subject)
  if settings.access_keys.include?(access_key_id)
    Canistor.take_fail(:store) { return }
    params = CGI::parse(context.http_request.endpoint.query.to_s)
    catch(:rendered_error) do
      if params.has_key?('uploads')
        # Client wants to create a new upload when uploads is present in
        # the query.
        post_upload(context, subject)
      elsif params.has_key?('uploadId')
        # Client wants to complete the upload when uploadId is present in
        # the query.
        complete_upload(context, subject, params)
      end
    end
  else
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  end
end

#put(context, access_key_id, subject) ⇒ Object



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

def put(context, access_key_id, subject)
  if settings.access_keys.include?(access_key_id)
    Canistor.take_fail(:store) { return }
    params = CGI::parse(context.http_request.endpoint.query.to_s)
    catch(:rendered_error) do
      if params.has_key?('uploadId')
        # Client wants to create a new upload part when uploadId is
        # present in the query.
        put_upload_part(context, subject, params)
      else
        put_object(context, subject)
      end
    end
  else
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  end
end

#store_replica(object) ⇒ Object



146
147
148
149
150
# File 'lib/canistor/storage/bucket.rb', line 146

def store_replica(object)
  replica = object.copy
  replica.versioned = settings.versioned?
  self[object.key] = replica
end

#to_sObject



133
134
135
136
137
138
139
140
# File 'lib/canistor/storage/bucket.rb', line 133

def to_s
  @objects.values.map do |object|
    ' * ' + object.label
  end.join("\n") +
  @uploads.keys.each do |upload_id|
    ' - ' + upload_id
  end.join("\n")
end

#update_settings(settings) ⇒ Object

Update bucket settings, see Canistor::Storage::Bucket::Settings for supported configuration.



32
33
34
# File 'lib/canistor/storage/bucket.rb', line 32

def update_settings(settings)
  @settings.update(settings)
end