Cloud Storage API - Class Google::Cloud::Storage::Bucket::Lifecycle (v1.50.0)

Reference documentation and code samples for the Cloud Storage API class Google::Cloud::Storage::Bucket::Lifecycle.

Bucket Lifecycle

A special-case Array for managing the Object Lifecycle Management rules for a bucket. Accessed via #lifecycle.

Inherits

  • Array

Examples

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

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"]
rule.matches_prefix #=> ["myprefix/foo"]
rule.matches_suffix #=> [".jpg", ".png"]

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

Methods

#add_abort_incomplete_multipart_upload_rule

def add_abort_incomplete_multipart_upload_rule(age: nil, matches_prefix: nil, matches_suffix: nil)

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

Parameters
  • age (Integer) (defaults to: nil) — The age of a file (in days). This condition is satisfied when a file reaches the specified age.
  • matches_prefix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of prefixs will be matched. Arguments will be converted from symbols to strings.
  • matches_suffix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of suffixes will be matched. Arguments will be converted from symbols to strings.
Example
require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.create_bucket "my-bucket" do |b|
  b.lifecycle.add_abort_incomplete_multipart_upload_rule age: 10,
                                                         matches_prefix: ["images/"],
                                                         matches_suffix: [".pdf"]
end

#add_delete_rule

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, matches_prefix: nil, matches_suffix: nil)

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

Parameters
  • age (Integer) (defaults to: nil) — The age of a file (in days). This condition is satisfied when a file reaches the specified age.
  • created_before (String, Date) (defaults to: nil) — 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.
  • custom_time_before (String, Date) (defaults to: nil) — 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.
  • days_since_custom_time (Integer) (defaults to: nil) — 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.
  • days_since_noncurrent_time (Integer) (defaults to: nil) — 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 #versioning?)
  • is_live (Boolean) (defaults to: nil) — Relevant only for versioned files. If the value is true, this condition matches live files; if the value is false, it matches archived files.
  • matches_storage_class (String, Symbol, Array<String,Symbol>) (defaults to: nil) — 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.
  • noncurrent_time_before (String, Date) (defaults to: nil) — 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.
  • num_newer_versions (Integer) (defaults to: nil) — 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.
  • matches_prefix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of prefixs will be matched. Arguments will be converted from symbols to strings.
  • matches_suffix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of suffixes will be matched. Arguments will be converted from symbols to strings.
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

#add_set_storage_class_rule

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, matches_prefix: nil, matches_suffix: nil)

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

Parameters
  • storage_class (String, Symbol) — 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.
  • age (Integer) (defaults to: nil) — The age of a file (in days). This condition is satisfied when a file reaches the specified age.
  • created_before (String, Date) (defaults to: nil) — 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.
  • custom_time_before (String, Date) (defaults to: nil) — 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.
  • days_since_custom_time (Integer) (defaults to: nil) — 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.
  • days_since_noncurrent_time (Integer) (defaults to: nil) — 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 #versioning?)
  • is_live (Boolean) (defaults to: nil) — Relevant only for versioned files. If the value is true, this condition matches live files; if the value is false, it matches archived files.
  • matches_storage_class (String, Symbol, Array<String,Symbol>) (defaults to: nil) — 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.
  • noncurrent_time_before (String, Date) (defaults to: nil) — 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.
  • num_newer_versions (Integer) (defaults to: nil) — 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.
  • matches_prefix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of prefixs will be matched. Arguments will be converted from symbols to strings.
  • matches_suffix (Array<String,Symbol>) (defaults to: nil) — Files having their name with the specified list of suffixes will be matched. Arguments will be converted from symbols to strings.
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