Class: BucketClient::AWSClient

Inherits:
Client
  • Object
show all
Defined in:
lib/bucket_client/aws/aws_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(client, id, secret, region) ⇒ AWSClient

Returns a new instance of AWSClient.

Parameters:

  • client (KirinHttp::Client)

    Basic http client

  • id (String)

    AWS id

  • secret (String)

    AWS secret

  • region (String)

    Region for bucket



18
19
20
21
22
23
# File 'lib/bucket_client/aws/aws_client.rb', line 18

def initialize(client, id, secret, region)
	signer = AWS4RequestSigner.new(id, secret)
	@region = region
	@http = AWSHttpClient.new(signer, region, client)
	@policy_factory = AWSPolicyFactory.new
end

Instance Method Details

#create_bucket(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bucket_client/aws/aws_client.rb', line 44

def create_bucket(key)
	content = "<CreateBucketConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
                                  <LocationConstraint>#{@region}</LocationConstraint>
                         </CreateBucketConfiguration>"
	endpoint = "https://s3-#{@region}.amazonaws.com/#{key}"
	resp = @http.query(:put, endpoint, content)
	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



135
136
137
138
139
140
141
142
# File 'lib/bucket_client/aws/aws_client.rb', line 135

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



125
126
127
128
129
130
131
132
133
# File 'lib/bucket_client/aws/aws_client.rb', line 125

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/bucket_client/aws/aws_client.rb', line 59

def delete_bucket(key)
	endpoint = "https://s3-#{@region}.amazonaws.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



70
71
72
73
74
75
76
77
# File 'lib/bucket_client/aws/aws_client.rb', line 70

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



100
101
102
# File 'lib/bucket_client/aws/aws_client.rb', line 100

def exist_blob(uri)
	@http.query(:head, uri).code === 200
end

#exist_bucket(key) ⇒ Object



25
26
27
28
# File 'lib/bucket_client/aws/aws_client.rb', line 25

def exist_bucket(key)
	resp = @http.query(:head, "https://s3-#{@region}.amazonaws.com/#{key}")
	resp.code == 200
end

#get_blob(uri) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/bucket_client/aws/aws_client.rb', line 90

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



30
31
32
# File 'lib/bucket_client/aws/aws_client.rb', line 30

def get_bucket!(key)
	AWSBucket.new(@region, @http, self, key)
end

#put_blob(payload, uri) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bucket_client/aws/aws_client.rb', line 104

def put_blob(payload, uri)
	mime = MimeMagic.by_magic payload
	type = mime&.type || "text/plain"
	resp = @http.query(:put, uri, payload, type, "text/plain")
	success = resp.code === 200
	if success
		OperationResult.new(success, "Blob put", uri, resp.code)
	else
		OperationResult.new(success, resp.content, nil, resp.code)
	end
end

#put_bucket(key) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/bucket_client/aws/aws_client.rb', line 34

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



85
86
87
88
# File 'lib/bucket_client/aws/aws_client.rb', line 85

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

Raises:

  • (ArgumentError)


79
80
81
82
83
# File 'lib/bucket_client/aws/aws_client.rb', line 79

def set_read_policy(key, access)
	raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
	resp = set_policy key, access, 10
	OperationResult.new(resp.code === 204, resp.content, nil, resp.code)
end

#update_blob(payload, uri) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/bucket_client/aws/aws_client.rb', line 116

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