class AWS::S3::BucketLifecycleConfiguration


or the changes will not be persisted.
{#update} or {#replace} block, then you must call ‘#update` yourself
Please be aware, if you add, remove or edit rules outside of an
end
end
rule.expiration_time = 10
rules.each do |rule|
bucket.lifecycle_configuration.update do
# change the expiration days to 10 for EVERY rule
You can also make changes to existing rules.
## Editing Existing Rules
bucket.lifecycle_configuration.clear
# remove all rules from this lifecycle configuration
You can also remove all rules in a single call with {#clear}:
end
end
remove_rule(rule) if rule.disabled?
rules.each do |rule|
bucket.lifecycle_configuration.update do
# delete all disabled rules
You can delete specific rules with {#remove_rule}.
## Removing Rules
end
add_rule(’temp/‘, :expiration_time => 30)
add_rule(’backups/‘, :glacier_transition_time => 10)
bucket.lifecycle_configuration.replace do
# replace all existing rules with the following
{#add_rule} inside a {#replace} block instead of an `#update` block:
If you prefer to completely replace a lifecycle configuration, call
## Replacing Rules
end
add_rule(’backups/‘, :glacier_transition_time => date)
date = Date.new(Time.now.year + 1, 01, 01)
bucket.lifecycle_configuration.update do
# transition all backups on January 1st of next year
You can also define the rule to transition objects at a specific date:
end
add_rule(’backups/‘, :glacier_transition_time => 3)
bucket.lifecycle_configuration.update do
# move backups to Glacier after 3 days
after a given number of days:
inside of an {#update} block that will transition objects to Glacier
You can add a rule to a bucket lifecycle configuration using {#add_rule}
## Transitioning Objects to Glacier
end
add_rule(’backups/‘, :expiration_time => date)
date = Date.new(Time.now.year + 1, 01, 01)
bucket.lifecycle_configuration.update do
# delete backups on January 1st of next year
You can also define the rule to expire objects at a specific date:
end
add_rule(’backups/‘, :expiration_time => 365)
bucket.lifecycle_configuration.update do
# delete backups after they are 1 year old
number of days:
inside of an {#update} block that will expire an object after a given
You can add a rule to a bucket lifecycle configuration using {#add_rule}
## Expiring Objects
available for rules.
See {Rule} for more information on all of the attributes and methods
* Whether the rule is enabled or disabled
* When to transition an object to Glacier
* When to expire an object
configuration options. Configuration options on the rules can specify:
A rule is comprised primarily of an id, prefix and set of
## Rules
the rule’s prefix.
Amazon S3 stores objects. The rules apply to objects whose keys match
A lifecycle configuration specify {Rule rules} that manage the way

def add_rule prefix, expiration_time = nil, options = {}

Returns:
  • (Rule) - Returns the rule that was added, as a {Rule} object.

Options Hash: (**options)
  • :glacier_transition_time (Date, Integer) --
  • :expiration_time (Date, Integer) -- Indicates
  • :disabled (Boolean) -- By default, all rules
  • :id (String) -- A unique ID for this rule. If an ID

Parameters:
  • prefix (String) -- objects whose keys begin with this prefix

Overloads:
  • add_rule(prefix, options = {})
def add_rule prefix, expiration_time = nil, options = {}
  if Hash === expiration_time
    options = expiration_time
  else
    options[:expiration_time] = expiration_time
  end
  id = options[:id] || SecureRandom.uuid
  opts = {
    :status => options[:disabled] == true ? 'Disabled' : 'Enabled',
    :expiration_time => options[:expiration_time],
    :glacier_transition_time => options[:glacier_transition_time]
  }
  rule = Rule.new(self, id, prefix, opts)
  self.rules << rule
  rule
end

def clear

def clear
  @rules = []
  bucket.lifecycle_configuration = nil
end

def initialize bucket, options = {}

Other tags:
    Api: - private
def initialize bucket, options = {}
  @bucket = bucket
  @rules = parse_xml(options[:xml]) if options[:xml]
  @rules = [] if options[:empty] == true
end

def parse_xml xml

def parse_xml xml
  Client::XML::GetBucketLifecycleConfiguration.parse(xml).rules.map do |r|
    opts = { :status => r[:status] }
    if r[:expiration]
      opts[:expiration_time] =
        r[:expiration][:days] || r[:expiration][:date]
    end
    if r[:transition]
      opts[:glacier_transition_time] =
        r[:transition][:days] || r[:transition][:date]
    end
    Rule.new(self, r[:id], r[:prefix], opts)
  end
end

def persist force = false

def persist force = false
  unless @batching and force == false
    if rules.empty?
      bucket.lifecycle_configuration = nil
    else
      bucket.lifecycle_configuration = self
    end
  end
end

def remove_rule rule_or_rule_id

Returns:
  • (nil) -

Parameters:
  • rule_or_rule_id (Rule, String) --
def remove_rule rule_or_rule_id
  rule_id = rule_or_rule_id
  if rule_id.nil?
    raise ArgumentError, "expected a rule or rule id, got nil"
  end
  rule_id = rule_id.id unless rule_id.is_a?(String)
  @rules = rules.select{|r| r.id != rule_id }
  nil
end

def replace &block

bucket.lifecycle_configuration.rules.size #=> 1

end
add_rule 'temp/', 10
bucket.lifecycle_configuration.replace
# replace the existing 3 rules with a single rule

bucket.lifecycle_configuration.rules.size #=> 3

the new rules.
When the block is complete, a single call will be made to save

new rules.
rules will be blanked out. This allows you to provide all
Yields to the given block. Before yielding, the current
def replace &block
  @rules = []
  update(&block)
end

def rules

Returns:
  • (Array) - Returns an array of rules.
def rules
  @rules ||= begin
    begin
      opts = { :bucket_name => bucket.name }
      response = bucket.client.get_bucket_lifecycle_configuration(opts)
      parse_xml(response.http_response.body)
    rescue Errors::NoSuchLifecycleConfiguration
      []
    end
  end
end

def to_xml

Returns:
  • (String) - Returns an xml string representation of this
def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.LifecycleConfiguration do
      rules.each do |rule|
        xml.Rule do
          xml.ID rule.id
          xml.Prefix rule.prefix
          xml.Status rule.status
          xml.Expiration do
            if Integer === rule.expiration_time
              xml.Days rule.expiration_time
            else
              date = rule.expiration_time.to_s
              xml.Date "#{date}T00:00:00Z"
            end
          end if rule.expiration_time
          xml.Transition do
            xml.StorageClass 'GLACIER'
            if Integer === rule.glacier_transition_time
              xml.Days rule.glacier_transition_time
            else
              date = rule.glacier_transition_time.to_s
              xml.Date "#{date}T00:00:00Z"
            end
          end if rule.glacier_transition_time
        end
      end
    end
  end.doc.root.to_xml
end

def update &block

Returns:
  • (nil) -
def update &block
  begin
    @batching = true
    instance_eval(&block) if block_given?
    persist(true)
  ensure
    @batching = false
  end
  nil
end