Class: Google::Cloud::Storage::Bucket::Lifecycle

Inherits:
Array
  • Object
show all
Defined in:
lib/google/cloud/storage/bucket/lifecycle.rb

Overview

Bucket Lifecycle

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

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 #=> ["MULTI_REGIONAL", "REGIONAL"]

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

See Also:

Defined Under Namespace

Classes: Rule

Instance Method Summary collapse

Instance Method Details

#add_delete_rule(age: nil, created_before: nil, is_live: nil, matches_storage_class: nil, num_newer_versions: nil) ⇒ Object

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

Examples:

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

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.

  • 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 MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, and DURABLE_REDUCED_AVAILABILITY. Arguments will be converted from symbols and lower-case to upper-case strings.

  • 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.

See Also:



181
182
183
184
185
186
187
188
189
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 181

def add_delete_rule age: nil, created_before: nil, is_live: nil,
                    matches_storage_class: nil,
                    num_newer_versions: nil
  push Rule.new \
    "Delete",
    age: age, created_before: created_before, is_live: is_live,
    matches_storage_class: storage_class_for(matches_storage_class),
    num_newer_versions: num_newer_versions
end

#add_set_storage_class_rule(storage_class, age: nil, created_before: nil, is_live: nil, matches_storage_class: nil, num_newer_versions: nil) ⇒ Object

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

Examples:

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

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.

  • 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 MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, and DURABLE_REDUCED_AVAILABILITY. Arguments will be converted from symbols and lower-case to upper-case strings.

  • 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.

See Also:



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 131

def add_set_storage_class_rule storage_class, age: nil,
                               created_before: nil, is_live: nil,
                               matches_storage_class: nil,
                               num_newer_versions: nil
  push Rule.new \
    "SetStorageClass",
    storage_class: storage_class_for(storage_class),
    age: age, created_before: created_before, is_live: is_live,
    matches_storage_class: storage_class_for(matches_storage_class),
    num_newer_versions: num_newer_versions
end