Skip to content

Commit 58f180e

Browse files
authored
Merge pull request #286 from coder-hugo/feature/bucket-lifecycle
Add possibility to configure bucket lifecycle policy rules
2 parents def20d6 + 2b5702c commit 58f180e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ It would be useful when you use S3 compatible storage that accepts only signatur
566566

567567
Given a threshold to treat events as delay, output warning logs if delayed events were put into s3.
568568

569+
**bucket_lifecycle_rule**
570+
571+
Specify one or more lifecycle rules for the bucket
572+
573+
<bucket_lifecycle_rule>
574+
id UNIQUE_ID_FOR_THE_RULE
575+
prefix OPTIONAL_PREFIX # Objects whose keys begin with this prefix will be affected by the rule. If not specified all objects of the bucket will be affected
576+
expiration_days NUMBER_OF_DAYS # The number of days before the object will expire
577+
</bucket_lifecycle_rule>
578+
569579
## Input: Setup
570580

571581
1. Create new [SQS](https://aws.amazon.com/documentation/sqs/) queue (use same region as S3)

lib/fluent/plugin/out_s3.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def initialize
124124
config_param :warn_for_delay, :time, default: nil
125125
desc "Arbitrary S3 metadata headers to set for the object"
126126
config_param :s3_metadata, :hash, default: nil
127+
config_section :bucket_lifecycle_rule, param_name: :bucket_lifecycle_rules, multi: true do
128+
desc "A unique ID for this rule"
129+
config_param :id, :string
130+
desc "Objects whose keys begin with this prefix will be affected by the rule. If not specified all objects of the bucket will be affected"
131+
config_param :prefix, :string, default: ''
132+
desc "The number of days before the object will expire"
133+
config_param :expiration_days, :integer
134+
end
127135

128136
DEFAULT_FORMAT_TYPE = "out_file"
129137

@@ -217,6 +225,7 @@ def start
217225

218226
check_apikeys if @check_apikey_on_start
219227
ensure_bucket if @check_bucket
228+
ensure_bucket_lifecycle
220229

221230
super
222231
end
@@ -373,6 +382,30 @@ def ensure_bucket
373382
end
374383
end
375384

385+
def ensure_bucket_lifecycle
386+
unless @bucket_lifecycle_rules.empty?
387+
old_rules = get_bucket_lifecycle_rules
388+
new_rules = @bucket_lifecycle_rules.sort_by { |rule| rule.id }.map do |rule|
389+
{ id: rule.id, expiration: { days: rule.expiration_days }, prefix: rule.prefix, status: "Enabled" }
390+
end
391+
392+
unless old_rules == new_rules
393+
log.info "Configuring bucket lifecycle rules for #{@s3_bucket} on #{@s3_endpoint}"
394+
@bucket.lifecycle_configuration.put({ lifecycle_configuration: { rules: new_rules } })
395+
end
396+
end
397+
end
398+
399+
def get_bucket_lifecycle_rules
400+
begin
401+
@bucket.lifecycle_configuration.rules.sort_by { |rule| rule[:id] }.map do |rule|
402+
{ id: rule[:id], expiration: { days: rule[:expiration][:days] }, prefix: rule[:prefix], status: rule[:status] }
403+
end
404+
rescue Aws::S3::Errors::NoSuchLifecycleConfiguration
405+
[]
406+
end
407+
end
408+
376409
def process_s3_object_key_format
377410
%W(%{uuid} %{uuid:random} %{uuid:hostname} %{uuid:timestamp}).each { |ph|
378411
if @s3_object_key_format.include?(ph)

0 commit comments

Comments
 (0)