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_days = 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:
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/’, 10)
add_rule(‘backups/’, 30)
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/’, 365, :id => ‘backup-rule’, :disabled => true
bucket.lifecycle_configuration.update do
# add a rule that deletes backups after they are 1 year old
you can do this with {#add_rule}.
If you perfer to specify a rule’s ID or status (defaults to ‘Enabled’)
end
add_rule(‘backups/’, 365)
bucket.lifecycle_configuration.update do
# add a rule that deletes backups after they are 1 year old
You can add a rule to a bucket lifecycle configuration using {#add_rule}.
== Adding Rules
available for rules.
See {Rule} for more information on all of the attributes and methods
ID and a status (they can be disabled).
deleted after “expiration days” have passed. Rules also have an
Objects with keys that start with the given prefix will be automatically
A rule is comprised primarily of a prefix and number of expiration days.
#expiration_days have passed.
Objects with keys matching a rule prefix will be deleted after
* #id
* #status
* #expiration_days
* #prefix
following attributes:
Each lifecycle configuration has a list of rules. Each rule has the
== Rules
Amazon S3 to delete certain objects after a period of days.
bucket that instructs that instruct
A lifecycle configuration is collections of rules for a single
def add_rule prefix, expiration_days, options = {}
-
(Rule)
- Returns the rule that was added, as a {Rule} object.
Options Hash:
(**options)
-
:disabled
(Boolean
) -- By default, all rules -
:id
(String
) -- A unique ID for this rule. If an ID
Parameters:
-
options
(Hash
) -- -
expiration_days
(Integer
) -- Indicates the lifetime for objects -
prefix
(String
) --
def add_rule prefix, expiration_days, options = {} id = options[:id] || UUIDTools::UUID.random_create.to_s status = options[:disabled] == true ? 'Disabled' : 'Enabled' rule = Rule.new(self, id, prefix, expiration_days, status) self.rules << rule rule end
def clear
def clear @rules = [] bucket.lifecycle_configuration = nil end
def initialize bucket, options = {}
- 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| Rule.new(self, r.id, r.prefix, r.expiration.days, r.status) 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
-
(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
-
(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
-
(String)
- Returns an xml string representation of this
def to_xml xml = Builder::XmlMarkup.new(:indent => 2) 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 xml.Days rule.expiration_days end end end end.strip end
def update &block
-
(nil)
-
def update &block begin @batching = true instance_eval(&block) if block_given? persist(true) ensure @batching = false end nil end