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

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

Overview

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 Google::Cloud::Storage::Bucket#lifecycle.

Examples:

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"]

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

See Also:

Instance Attribute Summary collapse

Instance Attribute Details

#actionString

The type of action taken when the rule's conditions are met. Currently, only Delete and SetStorageClass are supported.

Returns:

  • (String)

    the current value of action



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def action
  @action
end

#ageInteger

The age of a file (in days). This condition is satisfied when a file reaches the specified age.

Returns:

  • (Integer)

    the current value of age



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def age
  @age
end

#created_beforeString, Date

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.

Returns:

  • (String, Date)

    the current value of created_before



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def created_before
  @created_before
end

#is_liveBoolean

Relevant only for versioned files. If the value is true, this condition matches live files; if the value is false, it matches archived files.

Returns:

  • (Boolean)

    the current value of is_live



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def is_live
  @is_live
end

#matches_storage_classArray<String>

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.

Returns:

  • (Array<String>)

    the current value of matches_storage_class



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def matches_storage_class
  @matches_storage_class
end

#num_newer_versionsInteger

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.

Returns:

  • (Integer)

    the current value of num_newer_versions



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def num_newer_versions
  @num_newer_versions
end

#storage_classString

The target storage class for the action. Required only if the action is SetStorageClass.

Returns:

  • (String)

    the current value of storage_class



287
288
289
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 287

def storage_class
  @storage_class
end