diff --git a/lib/fluent/plugin/out_s3.rb b/lib/fluent/plugin/out_s3.rb index 1dd1c6e..b405ed9 100644 --- a/lib/fluent/plugin/out_s3.rb +++ b/lib/fluent/plugin/out_s3.rb @@ -648,14 +648,29 @@ def content_type def compress(chunk, tmp) begin - data = chunk.open { |io| io.read } - compressed = Zstd.compress(data, level: @level) - tmp.write(compressed) - tmp.flush - tmp.close + data = '' + chunk.open { |io| data = io.read } + + compressed = nil + begin + compressed = Zstd.compress(data, level: @level) + rescue => e + log.warn "Failed to compress with zstd: #{e.message}" + raise e + end + + if compressed + tmp.binmode + tmp.write(compressed) + tmp.flush + else + raise "Failed to compress data with zstd" + end rescue => e log.warn "zstd compression failed: #{e.message}" raise e + ensure + tmp.close rescue nil end end end