Class: Fluent::S3Output

Inherits:
TimeSlicedOutput
  • Object
show all
Includes:
Mixin::ConfigPlaceholders
Defined in:
lib/fluent/plugin/out_s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeS3Output

Returns a new instance of S3Output.



11
12
13
14
15
16
17
18
19
20
# File 'lib/fluent/plugin/out_s3.rb', line 11

def initialize
  super
  require 'aws-sdk'
  require 'zlib'
  require 'time'
  require 'tempfile'
  require 'open3'

  @use_ssl = true
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



38
39
40
# File 'lib/fluent/plugin/out_s3.rb', line 38

def bucket
  @bucket
end

Instance Method Details

#configure(conf) ⇒ Object



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
# File 'lib/fluent/plugin/out_s3.rb', line 46

def configure(conf)
  super

  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

  @ext, @mime_type = case @store_as
                     when 'gzip'
                       ['gz', 'application/x-gzip']
                     when 'lzo'
                       check_command('lzop', 'LZO')
                       @command_parameter = '-qf1' if @command_parameter.nil?
                       ['lzo', 'application/x-lzop']
                     when 'lzma2'
                       check_command('xz', 'LZMA2')
                       @command_parameter = '-qf0' if @command_parameter.nil?
                       ['xz', 'application/x-xz']
                     when 'json'
                       ['json', 'application/json']
                     else
                       ['txt', 'text/plain']
                     end

  if format_json = conf['format_json']
    $log.warn "format_json is deprecated. Use 'format json' instead"
    conf['format'] = 'json'
  else
    conf['format'] = @format
  end
  @formatter = TextFormatter.create(conf)

  if @localtime
    @path_slicer = Proc.new {|path|
      Time.now.strftime(path)
    }
  else
    @path_slicer = Proc.new {|path|
      Time.now.utc.strftime(path)
    }
  end
end

#format(tag, time, record) ⇒ Object



115
116
117
# File 'lib/fluent/plugin/out_s3.rb', line 115

def format(tag, time, record)
  @formatter.format(tag, time, record)
end

#placeholdersObject



42
43
44
# File 'lib/fluent/plugin/out_s3.rb', line 42

def placeholders
  [:percent]
end

#startObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fluent/plugin/out_s3.rb', line 96

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[:region] = @s3_region if @s3_region
  options[:endpoint] = @s3_endpoint if @s3_endpoint
  options[:proxy_uri] = @proxy_uri if @proxy_uri
  options[:use_ssl] = @use_ssl

  @s3 = AWS::S3.new(options)
  @bucket = @s3.buckets[@s3_bucket]

  check_apikeys if @check_apikey_on_start
  ensure_bucket
end

#write(chunk) ⇒ Object



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
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/plugin/out_s3.rb', line 119

def write(chunk)
  i = 0
  previous_path = nil

  begin
    s3path = Time.now.utc.strftime "#{@s3_object_key_format}.json.gz"

    if (i > 0) && (s3path == previous_path)
      raise "duplicated path is generated. use %{index} in s3_object_key_format: path = #{s3path}"
    end

    i += 1
    previous_path = s3path
  end while @bucket.objects[s3path].exists?

  tmp = Tempfile.new("s3-")
  begin
    if @store_as == "gzip"
      w = Zlib::GzipWriter.new(tmp)
      chunk.write_to(w)
      w.close
    elsif @store_as == "lzo"
      w = Tempfile.new("chunk-tmp")
      chunk.write_to(w)
      w.close
      tmp.close
      # We don't check the return code because we can't recover lzop failure.
      system "lzop #{@command_parameter} -o #{tmp.path} #{w.path}"
    elsif @store_as == "lzma2"
      w = Tempfile.new("chunk-xz-tmp")
      chunk.write_to(w)
      w.close
      tmp.close
      system "xz #{@command_parameter} -c #{w.path} > #{tmp.path}"
    else
      chunk.write_to(tmp)
      tmp.close
    end
    @bucket.objects[s3path].write(Pathname.new(tmp.path), {:content_type => @mime_type,
                                                           :reduced_redundancy => @reduced_redundancy})
  ensure
    tmp.close(true) rescue nil
    w.close rescue nil
    w.unlink rescue nil
  end
end