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

# Bucket Lifecycle

A special-case Array for managing the Object Lifecycle Management rules for a bucket. 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

@example Specifying the lifecycle management rules for a new bucket.

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.create_bucket "my-bucket" do |b|
  b.lifecycle.add_set_storage_class_rule "COLDLINE", age: 10
  b.lifecycle.add_delete_rule age: 30, is_live: false
end

@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 management rules in a block.

require "google/cloud/storage"

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

bucket.lifecycle do |l|
  # Remove the last rule from the array
  l.pop
  # Remove all rules with the given condition
  l.delete_if do |r|
    r.matches_storage_class.include? "NEARLINE"
  end
  l.add_set_storage_class_rule "COLDLINE", age: 10, is_live: true
  l.add_delete_rule age: 30, is_live: false
end

Public Class Methods

from_gapi(gapi) click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 265
def self.from_gapi gapi
  gapi_list = gapi.rule if gapi
  rules = Array(gapi_list).map do |rule_gapi|
    Rule.from_gapi rule_gapi
  end
  new rules
end
new(rules = []) click to toggle source

@private Initialize a new lifecycle rules builder with existing lifecycle rules, if any.

Calls superclass method
# File lib/google/cloud/storage/bucket/lifecycle.rb, line 79
def initialize rules = []
  super rules
  @original = to_gapi.rule.map(&:to_json)
end

Public Instance Methods

add_delete_rule(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

Adds a SetStorageClass lifecycle rule to the Object Lifecycle Management rules for a bucket.

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

Lifecycle Management

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

Managing Object Lifecycles

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

satisfied when a file reaches the specified age.

@param [String,Date] 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.

@param [String,Date] 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.

@param [Integer] 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.

@param [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?})

@param [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.

@param [String,Symbol,Array<String,Symbol>] 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. Arguments will be converted from symbols and lower-case
to upper-case strings.

@param [String,Date] 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.

@param [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

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.create_bucket "my-bucket" do |b|
  b.lifecycle.add_delete_rule age: 30, is_live: false
end
# File lib/google/cloud/storage/bucket/lifecycle.rb, line 234
def add_delete_rule 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
  push Rule.new(
    "Delete",
    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: storage_class_for(matches_storage_class),
    noncurrent_time_before: noncurrent_time_before,
    num_newer_versions: num_newer_versions
  )
end
add_set_storage_class_rule(storage_class, 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

Adds a SetStorageClass lifecycle rule to the Object Lifecycle Management rules for a bucket.

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

Lifecycle Management

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

Managing Object Lifecycles

@param [String,Symbol] storage_class The target storage class.

Required if the type of the action is `SetStorageClass`. The
argument will be converted from symbols and lower-case to
upper-case strings.

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

satisfied when a file reaches the specified age.

@param [String,Date] 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.

@param [String,Date] 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.

@param [Integer] 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.

@param [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?})

@param [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.

@param [String,Symbol,Array<String,Symbol>] 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. Arguments will be converted from symbols and lower-case
to upper-case strings.

@param [String,Date] 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.

@param [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

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.create_bucket "my-bucket" do |b|
  b.lifecycle.add_set_storage_class_rule "COLDLINE", age: 10
end
# File lib/google/cloud/storage/bucket/lifecycle.rb, line 151
def add_set_storage_class_rule storage_class,
                               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
  push Rule.new(
    "SetStorageClass",
    storage_class: storage_class_for(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: storage_class_for(matches_storage_class),
    noncurrent_time_before: noncurrent_time_before,
    num_newer_versions: num_newer_versions
  )
end
changed?() click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 85
def changed?
  @original != to_gapi.rule.map(&:to_json)
end
freeze() click to toggle source

@private

Calls superclass method
# File lib/google/cloud/storage/bucket/lifecycle.rb, line 274
def freeze
  each(&:freeze)
  super
end
to_gapi() click to toggle source

@private

# File lib/google/cloud/storage/bucket/lifecycle.rb, line 258
def to_gapi
  Google::Apis::StorageV1::Bucket::Lifecycle.new(
    rule: map(&:to_gapi)
  )
end