Class: Fluent::S3Output
- Inherits:
-
TimeSlicedOutput
- Object
- TimeSlicedOutput
- Fluent::S3Output
- Defined in:
- lib/fluent/plugin/out_s3.rb,
lib/fluent/plugin/s3_compressor_lzo.rb,
lib/fluent/plugin/s3_compressor_lzma2.rb,
lib/fluent/plugin/s3_compressor_gzip_command.rb
Defined Under Namespace
Classes: Compressor, GzipCommandCompressor, GzipCompressor, JsonCompressor, LZMA2Compressor, LZOCompressor, TextCompressor
Constant Summary collapse
- MAX_HEX_RANDOM_LENGTH =
16
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #desc(description) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ S3Output
constructor
A new instance of S3Output.
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ S3Output
Returns a new instance of S3Output.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/fluent/plugin/out_s3.rb', line 8 def initialize super require 'aws-sdk-resources' require 'zlib' require 'time' require 'tempfile' @compressor = nil @uuid_flush_enabled = false end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
136 137 138 |
# File 'lib/fluent/plugin/out_s3.rb', line 136 def bucket @bucket end |
Instance Method Details
#configure(conf) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/fluent/plugin/out_s3.rb', line 140 def configure(conf) super if @s3_endpoint && @s3_endpoint.end_with?('amazonaws.com') raise ConfigError, "s3_endpoint parameter is not supported for S3, use s3_region instead. This parameter is for S3 compatible services" end begin @compressor = COMPRESSOR_REGISTRY.lookup(@store_as).new(:buffer_type => @buffer_type, :log => log) rescue $log.warn "#{@store_as} not found. Use 'text' instead" @compressor = TextCompressor.new end @compressor.configure(conf) @formatter = Plugin.new_formatter(@format) @formatter.configure(conf) if @localtime @path_slicer = Proc.new {|path| Time.now.strftime(path) } else @path_slicer = Proc.new {|path| Time.now.utc.strftime(path) } end if @hex_random_length > MAX_HEX_RANDOM_LENGTH raise ConfigError, "hex_random_length parameter must be less than or equal to #{MAX_HEX_RANDOM_LENGTH}" end if @reduced_redundancy $log.warn "reduced_redundancy parameter is deprecated. Use storage_class parameter instead" @storage_class = "REDUCED_REDUNDANCY" end @path = process_deprecated_placeholders(@path) @s3_object_key_format = process_deprecated_placeholders(@s3_object_key_format) if !@check_object if conf.has_key?('s3_object_key_format') log.warn "Set 'check_object false' and s3_object_key_format is specified. Check s3_object_key_format is unique in each write. If not, existing file will be overwritten." else log.warn "Set 'check_object false' and s3_object_key_format is not specified. Use '%{path}/%{date_slice}_%{hms_slice}.%{file_extension}' for s3_object_key_format" @s3_object_key_format = "%{path}/%{date_slice}_%{hms_slice}.%{file_extension}" end end @values_for_s3_object_chunk = {} end |
#desc(description) ⇒ Object
22 23 |
# File 'lib/fluent/plugin/out_s3.rb', line 22 def desc(description) end |
#format(tag, time, record) ⇒ Object
214 215 216 |
# File 'lib/fluent/plugin/out_s3.rb', line 214 def format(tag, time, record) @formatter.format(tag, time, record) end |
#start ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/fluent/plugin/out_s3.rb', line 190 def start = setup_credentials [:region] = @s3_region if @s3_region [:endpoint] = @s3_endpoint if @s3_endpoint [:http_proxy] = @proxy_uri if @proxy_uri [:force_path_style] = @force_path_style [:compute_checksums] = @compute_checksums unless @compute_checksums.nil? [:signature_version] = @signature_version unless @signature_version.nil? [:ssl_verify_peer] = @ssl_verify_peer log.on_trace do [:http_wire_trace] = true [:logger] = log end s3_client = Aws::S3::Client.new() @s3 = Aws::S3::Resource.new(:client => s3_client) @bucket = @s3.bucket(@s3_bucket) check_apikeys if @check_apikey_on_start ensure_bucket if @check_bucket super end |
#write(chunk) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/fluent/plugin/out_s3.rb', line 218 def write(chunk) i = 0 previous_path = nil if @check_object begin path = @path_slicer.call(@path) @values_for_s3_object_chunk[chunk.unique_id] ||= { "hex_random" => hex_random(chunk), } values_for_s3_object_key = { "path" => path, "time_slice" => chunk.key, "file_extension" => @compressor.ext, "index" => i, }.merge!(@values_for_s3_object_chunk[chunk.unique_id]) values_for_s3_object_key['uuid_flush'.freeze] = uuid_random if @uuid_flush_enabled s3path = @s3_object_key_format.gsub(%r(%{[^}]+})) { |expr| values_for_s3_object_key[expr[2...expr.size-1]] } if (i > 0) && (s3path == previous_path) if @overwrite log.warn "#{s3path} already exists, but will overwrite" break else raise "duplicated path is generated. use %{index} in s3_object_key_format: path = #{s3path}" end end i += 1 previous_path = s3path end while @bucket.object(s3path).exists? else if @localtime hms_slicer = Time.now.strftime("%H%M%S") else hms_slicer = Time.now.utc.strftime("%H%M%S") end values_for_s3_object_key = { "path" => @path_slicer.call(@path), "time_slice" => chunk.key, "date_slice" => chunk.key, "file_extension" => @compressor.ext, "hms_slice" => hms_slicer, } s3path = @s3_object_key_format.gsub(%r(%{[^}]+})) { |expr| values_for_s3_object_key[expr[2...expr.size-1]] } end tmp = Tempfile.new("s3-", @tmp_dir) tmp.binmode begin @compressor.compress(chunk, tmp) tmp.rewind log.debug { "out_s3: write chunk: {key:#{chunk.key},unique_id:#{unique_hex(chunk)}} to s3://#{@s3_bucket}/#{s3path}" } = { :body => tmp, :content_type => @compressor.content_type, :storage_class => @storage_class, } [:server_side_encryption] = @use_server_side_encryption if @use_server_side_encryption [:ssekms_key_id] = @ssekms_key_id if @ssekms_key_id [:sse_customer_algorithm] = @sse_customer_algorithm if @sse_customer_algorithm [:sse_customer_key] = @sse_customer_key if @sse_customer_key [:sse_customer_key_md5] = @sse_customer_key_md5 if @sse_customer_key_md5 [:acl] = @acl if @acl [:grant_full_control] = @grant_full_control if @grant_full_control [:grant_read] = @grant_read if @grant_read [:grant_read_acp] = @grant_read_acp if @grant_read_acp [:grant_write_acp] = @grant_write_acp if @grant_write_acp @bucket.object(s3path).put() @values_for_s3_object_chunk.delete(chunk.unique_id) if @warn_for_delay if Time.strptime(chunk.key, @time_slice_format) < Time.now - @warn_for_delay log.warn { "out_s3: delayed events were put to s3://#{@s3_bucket}/#{s3path}" } end end ensure tmp.close(true) rescue nil end end |