Class: BucketClient::AzureClient

Inherits:
Client
  • Object
show all
Defined in:
lib/bucket_client/azure/azure_client.rb

Instance Method Summary collapse

Methods included from BucketMethods

#create_bucket!, #delete_bucket!, #delete_bucket_if_exist!, #get_bucket, #put_bucket!, #set_get_cors!, #set_read_policy!

Methods included from BlobMethods

#delete_blob!, #delete_blob_if_exist!, #get_blob!, #put_blob!, #update_blob!

Constructor Details

#initialize(account, secret) ⇒ AzureClient

Returns a new instance of AzureClient.



11
12
13
14
15
16
17
# File 'lib/bucket_client/azure/azure_client.rb', line 11

def initialize(, secret)
	@client = Azure::Storage::Blob::BlobService.create(
		storage_account_name: ,
		storage_access_key: secret
	)
	@acc = 
end

Instance Method Details

#create_bucket(key) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bucket_client/azure/azure_client.rb', line 46

def create_bucket(key)
	exist = exist_bucket key
	return OperationResult.new(false, "Bucket already exist", nil, 409) if exist
	begin
		@client.create_container key
		value = get_bucket! key
		OperationResult.new(true, "Azure Container created", value, 200)
	rescue Azure::Core::Http::HTTPError => e
		if e.status_code === 409
			sleep(3)
			create_bucket key
		else
			OperationResult.new(false, e.description, nil, e.status_code)
		end

	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#delete_blob(uri) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/bucket_client/azure/azure_client.rb', line 173

def delete_blob(uri)
	data = break_uri uri
	begin
		@client.delete_blob(data[:bucket], data[:blob])
		OperationResult.new(true, "Deleted Blob", nil, 204)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#delete_blob_if_exist(uri) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/bucket_client/azure/azure_client.rb', line 164

def delete_blob_if_exist(uri)
	exist = exist_blob uri
	if exist
		delete_blob uri
	else
		OperationResult.new(true, "Blob already deleted", nil, 204)
	end
end

#delete_bucket(key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/bucket_client/azure/azure_client.rb', line 66

def delete_bucket(key)
	begin
		@client.delete_container key
		OperationResult.new(true, "Container deleted", nil, 200)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#delete_bucket_if_exist(key) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/bucket_client/azure/azure_client.rb', line 77

def delete_bucket_if_exist(key)
	exist = exist_bucket key
	if exist
		delete_bucket key
	else
		OperationResult.new(true, "Bucket already deleted", nil, 200)
	end
end

#exist_blob(uri) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bucket_client/azure/azure_client.rb', line 129

def exist_blob(uri)
	begin
		data = break_uri uri
		@client. data[:bucket], data[:blob]
		true
	rescue Azure::Core::Http::HTTPError => e
		if e.status_code === 404
			false
		end
	end
end

#exist_bucket(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bucket_client/azure/azure_client.rb', line 19

def exist_bucket(key)
	begin
		@client. key
		true
	rescue Azure::Core::Http::HTTPError => e
		if e.status_code === 404
			false
		else
			raise e
		end
	end
end

#get_blob(uri) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bucket_client/azure/azure_client.rb', line 117

def get_blob(uri)
	data = break_uri uri
	begin
		_, content = @client.get_blob data[:bucket], data[:blob]
		OperationResult.new(true, "", content, 200)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#get_bucket!(key) ⇒ Object



32
33
34
# File 'lib/bucket_client/azure/azure_client.rb', line 32

def get_bucket!(key)
	AzureBucket.new(self, key, @client, @acc)
end

#put_blob(payload, uri) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/bucket_client/azure/azure_client.rb', line 141

def put_blob(payload, uri)
	mime = MimeMagic.by_magic payload
	data = break_uri uri
	begin
		type = mime&.type || "text/plain"
		@client.create_block_blob data[:bucket], data[:blob], payload, {:content_type => type}
		OperationResult.new(true, "OK", uri, 204)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#put_bucket(key) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/bucket_client/azure/azure_client.rb', line 36

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bucket_client/azure/azure_client.rb', line 99

def set_get_cors(key, cors)
	begin
		cors_rule = Azure::Storage::BlobService::CorsRule.new
		cors_rule.allowed_origins = cors
		cors_rule.allowed_methods = ["GET"]
		cors_rule.allowed_headers = ['*']
		cors_rule.exposed_headers = ['*']
		service_properties = Azure::Storage::Blob::StorageServiceProperties.new
		service_properties.cors.cors_rules = [cors_rule]
		@client.set_service_properties(service_properties)
		OperationResult.new(true, "OK", nil, 200)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#set_read_policy(key, access) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bucket_client/azure/azure_client.rb', line 86

def set_read_policy(key, access)
	raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
	begin
		level = access === :public ? "blob" : ""
		@client.set_container_acl key, level
		OperationResult.new(true, "OK", nil, 200)
	rescue Azure::Core::Http::HTTPError => e
		OperationResult.new(false, e.description, nil, e.status_code)
	rescue StandardError => e
		OperationResult.new(false, e.message, nil, 400)
	end
end

#update_blob(payload, uri) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/bucket_client/azure/azure_client.rb', line 155

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