forked from Git-Host/amazon_s3_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon_s3_integration.rb
57 lines (46 loc) · 1.25 KB
/
amazon_s3_integration.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
53
54
55
56
57
require_relative 'lib/amazon_s3/amazon_s3'
require 'active_support/inflector'
class AmazonS3Integration < EndpointBase::Sinatra::Base
set :logging, true
error SocketError do
result 500, "Unable to reach Amazon S3. Please make sure '#{@config[:region]}' is a valid region"
end
post '/export_file' do
summary = AmazonS3.new(
s3_client: s3_client,
bucket_name: @config[:bucket_name],
).export(
file_name: @config[:file_name],
objects: objects
)
if batch?
add_value "success", { summary => objects.collect {|o| o["id"]} }
end
result 200, summary
end
post '/import_file' do
summary, objects = AmazonS3.new(
s3_client: s3_client,
bucket_name: @config[:bucket_name],
).import(
file_name: @config[:file_name]
)
objects.each do |object|
add_object @config[:object_type], object
end if objects
result 200, summary
end
def s3_client
AWS::S3.new(
access_key_id: @config[:access_key_id],
secret_access_key: @config[:secret_access_key],
region: @config[:region].to_s.downcase
)
end
def objects
Array.wrap(@payload.except(:parameters, :request_id).values.first)
end
def batch?
objects.size > 1
end
end