Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/storage/examples/src/disable_bucket_lifecycle_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_disable_lifecycle_management]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::Lifecycle;
use google_cloud_wkt::FieldMask;

pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
let bucket = client
.get_bucket()
.set_name(format!("projects/_/buckets/{bucket_id}"))
.send()
.await?;
let metageneration = bucket.metageneration;
let bucket = client
.update_bucket()
.set_bucket(bucket.set_lifecycle(Lifecycle::new()))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["lifecycle"]))
.send()
.await?;
println!("Lifecycle management disabled for bucket {bucket_id}: {bucket:?}");
Ok(())
}
// [END storage_disable_lifecycle_management]
45 changes: 45 additions & 0 deletions src/storage/examples/src/enable_bucket_lifecycle_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_enable_lifecycle_management]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::Lifecycle;
use google_cloud_storage::model::bucket::lifecycle::{
Rule,
rule::{Action, Condition},
};
use google_cloud_wkt::FieldMask;

pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
let bucket = client
.get_bucket()
.set_name(format!("projects/_/buckets/{bucket_id}"))
.send()
.await?;
let metageneration = bucket.metageneration;
let rule = Rule::new()
.set_action(Action::new().set_type("Delete"))
.set_condition(Condition::new().set_age_days(30));
let lifecycle = Lifecycle::new().set_rule(vec![rule]);
let bucket = client
.update_bucket()
.set_bucket(bucket.set_lifecycle(lifecycle))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["lifecycle"]))
.send()
.await?;
println!("Lifecycle management enabled for bucket {bucket_id}: {bucket:?}");
Ok(())
}
// [END storage_enable_lifecycle_management]
12 changes: 12 additions & 0 deletions src/storage/examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ mod create_bucket_class_location;
mod create_bucket_dual_region;
mod create_bucket_hierarchical_namespace;
mod delete_bucket;
mod disable_bucket_lifecycle_management;
mod disable_default_event_based_hold;
mod enable_bucket_lifecycle_management;
mod enable_default_event_based_hold;
mod get_bucket_metadata;
mod get_default_event_based_hold;
Expand All @@ -31,9 +33,11 @@ mod print_bucket_acl;
mod print_bucket_acl_for_user;
mod quickstart;
mod remove_bucket_owner;
mod set_lifecycle_abort_multipart_upload;
mod set_public_access_prevention_enforced;
mod set_public_access_prevention_inherited;
mod set_public_access_prevention_unspecified;
mod view_lifecycle_management_configuration;

use google_cloud_storage::client::{Storage, StorageControl};
use google_cloud_storage::model::Object;
Expand Down Expand Up @@ -81,6 +85,14 @@ pub async fn run_bucket_examples(buckets: &mut Vec<String>) -> anyhow::Result<()
set_public_access_prevention_enforced::sample(&client, &id).await?;
tracing::info!("running get_public_access_prevention example");
get_public_access_prevention::sample(&client, &id).await?;
tracing::info!("running view_lifecycle_management_configuration example");
view_lifecycle_management_configuration::sample(&client, &id).await?;
tracing::info!("running enable_bucket_lifecycle_management example");
enable_bucket_lifecycle_management::sample(&client, &id).await?;
tracing::info!("running set_lifecycle_abort_multipart_upload example");
set_lifecycle_abort_multipart_upload::sample(&client, &id).await?;
tracing::info!("running disable_bucket_lifecycle_management example");
disable_bucket_lifecycle_management::sample(&client, &id).await?;
tracing::info!("running print_bucket_acl example");
print_bucket_acl::sample(&client, &id).await?;
tracing::info!("running add_bucket_owner example");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_add_lifecycle_rule]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::lifecycle::{
Rule,
rule::{Action, Condition},
};

use google_cloud_wkt::FieldMask;

pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
let bucket = client
.get_bucket()
.set_name(format!("projects/_/buckets/{bucket_id}"))
.send()
.await?;
let metageneration = bucket.metageneration;
let mut lifecycle = bucket.lifecycle.clone().unwrap_or_default();
let rule = Rule::new()
.set_action(Action::new().set_type("AbortIncompleteMultipartUpload"))
.set_condition(Condition::new().set_age_days(7));
lifecycle.rule.push(rule);
let bucket = client
.update_bucket()
.set_bucket(bucket.set_lifecycle(lifecycle))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["lifecycle"]))
.send()
.await?;
println!("Lifecycle rule added for bucket {bucket_id}: {bucket:?}");
Ok(())
}
// [END storage_add_lifecycle_rule]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_view_lifecycle_management_configuration]
use google_cloud_storage::client::StorageControl;

pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
let bucket = client
.get_bucket()
.set_name(format!("projects/_/buckets/{bucket_id}"))
.send()
.await?;
println!(
"Lifecycle rules for bucket {}: {:?}",
bucket_id, bucket.lifecycle
);
Ok(())
}
// [END storage_view_lifecycle_management_configuration]
Loading