-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.rb
52 lines (43 loc) · 1.35 KB
/
s3.rb
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
48
49
50
51
52
require "grid/editor_integration/storages/abstract"
require "aws-sdk-v1"
module Grid
module EditorIntegration
module Storages
class S3 < AbstractStorage
attr_accessor :bucket, :content_encoding
attr_reader :storage
def initialize(path = nil, cdn_url = "", options = {})
super
self.bucket = options[:bucket]
self.content_encoding = options[:content_encoding]
@storage = AWS::S3.new(options)
end
def file_exists?(file_name)
storage.buckets[bucket].objects[full_file_name(file_name)].exists?
end
def save_content_in_file(current_content, file_name, options = {})
o = storage.buckets[bucket].objects.create(full_file_name(file_name),
current_content,
acl: :public_read,
content_type: options[:content_type])
end
def full_file_name(file_name)
::File.join(path, file_name)
end
def get_content_by_file_name(file_name)
res = storage.
buckets[bucket].
objects[full_file_name(file_name)].
read.
force_encoding(Encoding::UTF_8)
end
def clear
storage.buckets[bucket].
objects.
with_prefix(path).
each { |o| o.delete }
end
end
end
end
end