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 #=> ["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

See Also:

Defined Under Namespace

Classes: Rule

Instance Method Summary collapse

Instance Method Details

#add_abort_incomplete_multipart_upload_rule(age: nil, matches_prefix: nil, matches_suffix: nil) ⇒ Object

Adds a AbortIncompleteMultipartUpload 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_abort_incomplete_multipart_upload_rule age: 10,
                                                         matches_prefix: ["images/"],
                                                         matches_suffix: [".pdf"]
end

See Also:



308
309
310
311
312
313
314
315
316
317
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 308

def add_abort_incomplete_multipart_upload_rule age: nil,
                                               matches_prefix: nil,
                                               matches_suffix: nil
  push Rule.new(
    "AbortIncompleteMultipartUpload",
    age: age,
    matches_prefix: Array(matches_prefix),
    matches_suffix: Array(matches_suffix)
  )
end

#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) ⇒ Object

Adds a Delete 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

See Also:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 252

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

See Also:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/google/cloud/storage/bucket/lifecycle.rb', line 159

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
  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,
    matches_prefix: Array(matches_prefix),
    matches_suffix: Array(matches_suffix)
  )
end