class Google::Cloud::Storage::Bucket::Lifecycle::Rule

# Bucket Lifecycle Rule

Represents an Object Lifecycle Management rule for a bucket. The action for the rule will be taken when its conditions are met. Accessed via {Bucket#lifecycle}.

@see cloud.google.com/storage/docs/lifecycle Object

Lifecycle Management

@see cloud.google.com/storage/docs/managing-lifecycles

Managing Object Lifecycles

@attr [String] action The type of action taken when the rule's

conditions are met. Currently, only `Delete` and `SetStorageClass`
are supported.

@attr [String] storage_class The target storage class for the

action. Required only if the action is `SetStorageClass`.

@attr [Integer] age The age of a file (in days). This condition is

satisfied when a file reaches the specified age.

@attr [String,Date,nil] created_before A date in RFC 3339 format with

only the date part (for instance, "2013-01-15"). This condition is
satisfied when a file is created before midnight of the specified
date in UTC. When returned by the service, a non-empty value will
always be a Date object.

@attr [String,Date,nil] custom_time_before A date in RFC 3339 format with

only the date part (for instance, "2013-01-15"). This condition is
satisfied when the custom time on an object is before this date in UTC.

@attr [Integer,nil] days_since_custom_time Represents the number of

days elapsed since the user-specified timestamp set on an object.
The condition is satisfied if the days elapsed is at least this
number. If no custom timestamp is specified on an object, the
condition does not apply.

@attr [Integer] days_since_noncurrent_time Represents the number of

days elapsed since the noncurrent timestamp of an object. The
condition is satisfied if the days elapsed is at least this number.
The value of the field must be a nonnegative integer. If it's zero,
the object version will become eligible for Lifecycle action as
soon as it becomes noncurrent. Relevant only for versioning-enabled
buckets. (See {Bucket#versioning?})

@attr [Boolean] is_live Relevant only for versioned files. If the

value is `true`, this condition matches live files; if the value
is `false`, it matches archived files.

@attr [Array<String>] matches_storage_class Files having any of the

storage classes specified by this condition will be matched.
Values include `STANDARD`, `NEARLINE`, `COLDLINE`, and `ARCHIVE`.
`REGIONAL`, `MULTI_REGIONAL`, and `DURABLE_REDUCED_AVAILABILITY`
are supported as legacy storage classes.

@attr [String,Date,nil] noncurrent_time_before A date in RFC 3339 format

with only the date part (for instance, "2013-01-15"). This condition
is satisfied when the noncurrent time on an object is before this
date in UTC. This condition is relevant only for versioned objects.
When returned by the service, a non-empty value will always be a
Date object.

@attr [Integer] num_newer_versions Relevant only for versioned

files. If the value is N, this condition is satisfied when there
are at least N versions (including the live version) newer than
this version of the file.

@example Retrieving a bucket's lifecycle management rules.

require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket "my-bucket"

bucket.lifecycle.size #=> 2
rule = bucket.lifecycle.first
rule.action #=> "SetStorageClass"
rule.storage_class #=> "COLDLINE"
rule.age #=> 10
rule.matches_storage_class #=> ["STANDARD", "NEARLINE"]

@example Updating the bucket's lifecycle rules in a block.

require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket "my-bucket"

bucket.update do |b|
  b.lifecycle do |l|
    # Remove the last rule from the array
    l.pop
    # Remove rules with the given condition
    l.delete_if do |r|
      r.matches_storage_class.include? "NEARLINE"
    end
    # Update rules
    l.each do |r|
      r.age = 90 if r.action == "Delete"
    end
    # Add a rule
    l.add_set_storage_class_rule "COLDLINE", age: 10
  end
end

Attributes

action[RW]
age[RW]
created_before[RW]
custom_time_before[RW]
days_since_custom_time[RW]
days_since_noncurrent_time[RW]
is_live[RW]
matches_storage_class[RW]
noncurrent_time_before[RW]
num_newer_versions[RW]
storage_class[RW]

Public Class Methods

from_gapi(gapi) click to toggle source

@private @param [Google::Apis::StorageV1::Bucket::Rule] gapi

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 464
def self.from_gapi gapi
  action = gapi.action
  c = gapi.condition
  new(
    action.type,
    storage_class: action.storage_class,
    age: c.age,
    created_before: c.created_before,
    custom_time_before: c.custom_time_before,
    days_since_custom_time: c.days_since_custom_time,
    days_since_noncurrent_time: c.days_since_noncurrent_time,
    is_live: c.is_live,
    matches_storage_class: c.matches_storage_class,
    noncurrent_time_before: c.noncurrent_time_before,
    num_newer_versions: c.num_newer_versions
  )
end
new(action, storage_class: nil, age: nil, created_before: nil, custom_time_before: nil, days_since_custom_time: nil, days_since_noncurrent_time: nil, is_live: nil, matches_storage_class: nil, noncurrent_time_before: nil, num_newer_versions: nil) click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 387
def initialize action,
               storage_class: nil,
               age: nil,
               created_before: nil,
               custom_time_before: nil,
               days_since_custom_time: nil,
               days_since_noncurrent_time: nil,
               is_live: nil,
               matches_storage_class: nil,
               noncurrent_time_before: nil,
               num_newer_versions: nil
  @action = action
  @storage_class = storage_class
  @age = age
  @created_before = created_before
  @custom_time_before = custom_time_before
  @days_since_custom_time = days_since_custom_time
  @days_since_noncurrent_time = days_since_noncurrent_time
  @is_live = is_live
  @matches_storage_class = Array(matches_storage_class)
  @noncurrent_time_before = noncurrent_time_before
  @num_newer_versions = num_newer_versions
end

Public Instance Methods

action_gapi(action, storage_class) click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 432
def action_gapi action, storage_class
  Google::Apis::StorageV1::Bucket::Lifecycle::Rule::Action.new(
    type: action,
    storage_class: storage_class
  )
end
condition_gapi(age, created_before, custom_time_before, days_since_custom_time, days_since_noncurrent_time, is_live, matches_storage_class, noncurrent_time_before, num_newer_versions) click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 440
def condition_gapi age,
                   created_before,
                   custom_time_before,
                   days_since_custom_time,
                   days_since_noncurrent_time,
                   is_live,
                   matches_storage_class,
                   noncurrent_time_before,
                   num_newer_versions
  Google::Apis::StorageV1::Bucket::Lifecycle::Rule::Condition.new(
    age: age,
    created_before: created_before,
    custom_time_before: custom_time_before,
    days_since_custom_time: days_since_custom_time,
    days_since_noncurrent_time: days_since_noncurrent_time,
    is_live: is_live,
    matches_storage_class: Array(matches_storage_class),
    noncurrent_time_before: noncurrent_time_before,
    num_newer_versions: num_newer_versions
  )
end
freeze() click to toggle source

@private

Calls superclass method
# File lib/google/cloud/storage/bucket/lifecycle.rb, line 483
def freeze
  @matches_storage_class.freeze
  super
end
to_gapi() click to toggle source

@private @return [Google::Apis::StorageV1::Bucket::Lifecycle]

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 413
def to_gapi
  condition = condition_gapi(
    age,
    created_before,
    custom_time_before,
    days_since_custom_time,
    days_since_noncurrent_time,
    is_live,
    matches_storage_class,
    noncurrent_time_before,
    num_newer_versions
  )
  Google::Apis::StorageV1::Bucket::Lifecycle::Rule.new(
    action: action_gapi(action, storage_class),
    condition: condition
  )
end