Class: BucketClient::DigitalOceanClient
- Inherits:
-
Client
- Object
- Client
- BucketClient::DigitalOceanClient
show all
- Defined in:
- lib/bucket_client/digital_ocean/digital_ocean_client.rb
Instance Method Summary
collapse
#create_bucket!, #delete_bucket!, #delete_bucket_if_exist!, #get_bucket, #put_bucket!, #set_get_cors!, #set_read_policy!
#delete_blob!, #delete_blob_if_exist!, #get_blob!, #put_blob!, #update_blob!
Constructor Details
#initialize(client, id, secret, region) ⇒ DigitalOceanClient
Returns a new instance of DigitalOceanClient.
Instance Method Details
#create_bucket(key) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 60
def create_bucket(key)
endpoint = "https://#{@region}.digitaloceanspaces.com/#{key}"
resp = @http.query(:put, endpoint)
success = resp.code === 200
if success
bucket = get_bucket! key
OperationResult.new(success, "Bucket created", bucket, 200)
else
OperationResult.new(success, resp.content, nil, resp.code)
end
end
|
#delete_blob(uri) ⇒ Object
164
165
166
167
168
169
170
171
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 164
def delete_blob(uri)
exist = exist_blob uri
if exist
delete_blob_if_exist uri
else
OperationResult.new(false, "Blob does not exist", nil, 404)
end
end
|
#delete_blob_if_exist(uri) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 154
def delete_blob_if_exist(uri)
resp = @http.query :delete, uri
success = resp.code === 204
if success
OperationResult.new(success, "Blob deleted", nil, resp.code)
else
OperationResult.new(success, resp.content, nil, resp.code)
end
end
|
#delete_bucket(key) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 81
def delete_bucket(key)
endpoint = "https://#{@region}.digitaloceanspaces.com/#{key}"
resp = @http.query(:delete, endpoint)
success = resp.code === 204
if success
OperationResult.new(success, "Bucket deleted", nil, resp.code)
else
OperationResult.new(success, resp.content, nil, resp.code)
end
end
|
#delete_bucket_if_exist(key) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 72
def delete_bucket_if_exist(key)
exist = exist_bucket key
if exist
delete_bucket key
else
OperationResult.new(true, "Bucket already deleted", nil, 204)
end
end
|
#exist_blob(uri) ⇒ Object
124
125
126
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 124
def exist_blob(uri)
@http.query(:head, uri).code === 200
end
|
#exist_bucket(key) ⇒ Object
45
46
47
48
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 45
def exist_bucket(key)
resp = @http.query(:head, "https://#{@region}.digitaloceanspaces.com/#{key}")
resp.code == 200
end
|
#get_blob(uri) ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 114
def get_blob(uri)
resp = @http.query :get, uri
success = resp.code === 200
if success
OperationResult.new(success, "OK", resp.content, resp.code)
else
OperationResult.new(success, resp.content, nil, resp.code)
end
end
|
#get_bucket!(key) ⇒ Object
92
93
94
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 92
def get_bucket!(key)
DigitalOceanBucket.new(@region, @http, self, key)
end
|
#put_blob(payload, uri) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 128
def put_blob(payload, uri)
mime = MimeMagic.by_magic payload
type = mime&.type || "text/plain"
resp = @http.query :put, uri, payload, type, "application/xml"
return OperationResult.new(false, resp.content, nil, 400) unless resp.code === 200
url = Addressable::URI.parse uri
bucket = url.path.split("/").find {|x| !x.nil? && !x.empty?}
domain = url.host.split(".").first
bucket = domain unless domain == @region
is_public = bucket_public bucket
return is_public unless is_public.success
access = is_public.value ? :public : :private
resp = set_blob_acl uri, access, 10
return resp unless resp.code === 200
OperationResult.new(true, "OK", uri, resp.code)
end
|
#put_bucket(key) ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 50
def put_bucket(key)
exist = exist_bucket key
if exist
bucket = get_bucket! key
OperationResult.new(true, "OK", bucket, 200)
else
create_bucket key
end
end
|
#set_get_cors(key, cors) ⇒ Object
96
97
98
99
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 96
def set_get_cors(key, cors)
resp = set_cors key, cors, 10
OperationResult.new(resp.code === 200, resp.content, nil, resp.code)
end
|
#set_read_policy(key, access) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 101
def set_read_policy(key, access)
raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
resp = set_bucket_acl key, access, 10
return resp unless resp.success
object_uri_resp = get_all_object_uri key
return object_uri_resp unless object_uri_resp.success
object_uri_resp.value.each do |uri|
acl_resp = set_blob_acl uri, access, 10
return acl_resp unless acl_resp.success
end
resp
end
|
#update_blob(payload, uri) ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/bucket_client/digital_ocean/digital_ocean_client.rb', line 145
def update_blob(payload, uri)
exist = exist_blob uri
if exist
put_blob payload, uri
else
OperationResult.new(false, "Blob does not exist", nil, 404)
end
end
|