blob: 71f065c6087c824a3e123d090215123f9aa5d554 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
require "google/cloud/storage"
class ActiveFile::Sites::GCSSite < ActiveFile::Site
attr_reader :client, :bucket
def initialize(project:, keyfile:, bucket:)
@client = Google::Cloud::Storage.new(project: project, keyfile: keyfile)
@bucket = @client.bucket(bucket)
end
def upload(key, data)
bucket.create_file(data, key)
end
def download(key)
io = file_for(key).download
io.rewind
io.read
end
def delete(key)
file_for(key).try(:delete)
end
def exist?(key)
file_for(key).present?
end
def byte_size(key)
file_for(key).size
end
def checksum(key)
convert_to_hex base64: file_for(key).md5
end
private
def file_for(key)
bucket.file(key)
end
def convert_to_hex(base64:)
base64.unpack("m0").first.unpack("H*").first
end
end
|