forked from fluent/fluent-plugin-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout_s3.rb
152 lines (124 loc) · 3.48 KB
/
out_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module Fluent
require 'fluent/mixin/config_placeholders'
class S3Output < Fluent::TimeSlicedOutput
Fluent::Plugin.register_output('s3', self)
def initialize
super
require 'aws-sdk'
require 'zlib'
require 'time'
require 'tempfile'
@use_ssl = true
end
config_param :path, :string, :default => ""
config_param :time_format, :string, :default => nil
include SetTagKeyMixin
config_set_default :include_tag_key, false
include SetTimeKeyMixin
config_set_default :include_time_key, false
config_param :aws_key_id, :string, :default => nil
config_param :aws_sec_key, :string, :default => nil
config_param :s3_bucket, :string
config_param :s3_endpoint, :string, :default => nil
config_param :s3_object_key_format, :string, :default => "%{path}%{time_slice}_%{index}.%{file_extension}"
config_param :auto_create_bucket, :bool, :default => true
config_param :proxy_uri, :string, :default => nil
attr_reader :bucket
include Fluent::Mixin::ConfigPlaceholders
def placeholders
[:percent]
end
def configure(conf)
super
if format_json = conf['format_json']
@format_json = true
else
@format_json = false
end
if use_ssl = conf['use_ssl']
if use_ssl.empty?
@use_ssl = true
else
@use_ssl = Config.bool_value(use_ssl)
if @use_ssl.nil?
raise ConfigError, "'true' or 'false' is required for use_ssl option on s3 output"
end
end
end
@timef = TimeFormatter.new(@time_format, @localtime)
end
def start
super
options = {}
if @aws_key_id && @aws_sec_key
options[:access_key_id] = @aws_key_id
options[:secret_access_key] = @aws_sec_key
end
options[:s3_endpoint] = @s3_endpoint if @s3_endpoint
options[:use_ssl] = @use_ssl
options[:proxy_uri] = @proxy_uri
@s3 = AWS::S3.new(options)
@bucket = @s3.buckets[@s3_bucket]
ensure_bucket
check_apikeys
end
def format(tag, time, record)
if @include_time_key || !@format_json
time_str = @timef.format(time)
end
# copied from each mixin because current TimeSlicedOutput can't support mixins.
if @include_tag_key
record[@tag_key] = tag
end
if @include_time_key
record[@time_key] = time_str
end
if @format_json
Yajl.dump(record) + "\n"
else
"#{time_str}\t#{tag}\t#{Yajl.dump(record)}\n"
end
end
def write(chunk)
i = 0
begin
values_for_s3_object_key = {
"path" => @path,
"time_slice" => chunk.key,
"file_extension" => "gz",
"index" => i
}
s3path = @s3_object_key_format.gsub(%r(%{[^}]+})) { |expr|
values_for_s3_object_key[expr[2...expr.size-1]]
}
i += 1
end while @bucket.objects[s3path].exists?
tmp = Tempfile.new("s3-")
w = Zlib::GzipWriter.new(tmp)
begin
chunk.write_to(w)
w.close
@bucket.objects[s3path].write(Pathname.new(tmp.path), :content_type => 'application/x-gzip')
ensure
tmp.close(true) rescue nil
w.close rescue nil
end
end
private
def ensure_bucket
if @auto_create_bucket
$log.info "Creating bucket #{@s3_bucket} on #{@s3_endpoint}"
@s3.buckets.create(@s3_bucket)
else
raise "The specified bucket does not exist: bucket = #{@s3_bucket}"
end
end
end
def check_apikeys
@bucket.empty?
rescue
raise "aws_key_id or aws_sec_key is invalid. Please check your configuration"
end
end
end