Class: BucketClient::GCPClient
- Inherits:
-
Client
- Object
- Client
- BucketClient::GCPClient
show all
- Defined in:
- lib/bucket_client/gcp/gcp_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(project_id, secret) ⇒ GCPClient
Returns a new instance of GCPClient.
8
9
10
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 8
def initialize(project_id, secret)
@client = Google::Cloud::Storage.new(project_id: project_id, keyfile: secret)
end
|
Instance Method Details
#create_bucket(key) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 30
def create_bucket(key)
begin
@client.create_bucket key
bucket = get_bucket! key
OperationResult.new(true, "OK", bucket, 200)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#delete_blob(uri) ⇒ Object
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 140
def delete_blob(uri)
data = break_uri uri
begin
bucket = @client.bucket data[:bucket]
bucket.file(data[:blob], skip_lookup: true).delete
OperationResult.new(true, "Deleted Blob", nil, 204)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#delete_blob_if_exist(uri) ⇒ Object
131
132
133
134
135
136
137
138
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 131
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
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 40
def delete_bucket(key)
exist = exist_bucket key
if exist
bucket = @client.bucket(key)
success = bucket.delete
message = success ? "Deleted" : "Failed to delete"
code = success ? 204 : 400
OperationResult.new(success, message, nil, code)
else
OperationResult.new(false, "Bucket does not exist", nil, 404)
end
end
|
#delete_bucket_if_exist(key) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 53
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
100
101
102
103
104
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 100
def exist_blob(uri)
data = break_uri uri
bucket = @client.bucket data[:bucket]
bucket.file(data[:blob], skip_lookup: true).exists?
end
|
#exist_bucket(key) ⇒ Object
12
13
14
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 12
def exist_bucket(key)
!@client.bucket(key).nil?
end
|
#get_blob(uri) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 88
def get_blob(uri)
data = break_uri uri
begin
bucket = @client.bucket data[:bucket]
blob = bucket.file data[:blob]
bin = blob.download.read
OperationResult.new(true, "OK", bin, 200)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#get_bucket!(key) ⇒ Object
16
17
18
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 16
def get_bucket!(key)
GCPBucket.new(self, key)
end
|
#put_blob(payload, uri) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 106
def put_blob(payload, uri)
mime = MimeMagic.by_magic payload
data = break_uri uri
begin
bucket = @client.bucket data[:bucket]
f = StringIO.new
f << payload
type = mime&.type || "text/plain"
file = bucket.create_file f, data[:blob], cache_control: "no-cache", content_type: type
OperationResult.new(true, "OK", file.public_url, 200)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#put_bucket(key) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 20
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
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 76
def set_get_cors(key, cors)
begin
bucket = @client.bucket key
bucket.cors(&:clear)
bucket.cors {|rules| rules.add_rule cors, "GET"}
OperationResult.new(true, "OK", nil, 200)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#set_read_policy(key, access) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 62
def set_read_policy(key, access)
raise ArgumentError.new("Read Policy not accepted") if access != :public && access != :private
begin
bucket = @client.bucket key
modify_acl bucket, access
bucket.files.each do |file|
modify_acl file, access
end
OperationResult.new(true, "OK", nil, 200)
rescue StandardError => e
OperationResult.new(false, e.message, nil, 400)
end
end
|
#update_blob(payload, uri) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/bucket_client/gcp/gcp_client.rb', line 122
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
|