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
48 changes: 48 additions & 0 deletions src/storage/examples/src/define_bucket_website_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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_define_bucket_website_configuration]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::Website;
use google_cloud_wkt::FieldMask;

pub async fn sample(
client: &StorageControl,
bucket_id: &str,
main_page_suffix: &str,
not_found_page: &str,
) -> anyhow::Result<()> {
let bucket = client
.get_bucket()
.set_name(format!("projects/_/buckets/{bucket_id}"))
.send()
.await?;
let metageneration = bucket.metageneration;
let website = Website::new()
.set_main_page_suffix(main_page_suffix)
.set_not_found_page(not_found_page);
let bucket = client
.update_bucket()
.set_bucket(bucket.set_website(website))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["website"]))
.send()
.await?;
println!(
"Website configuration for bucket {} defined as: {:?}",
bucket_id, bucket.website
);
Ok(())
}
// [END storage_define_bucket_website_configuration]
7 changes: 6 additions & 1 deletion src/storage/examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod create_bucket;
mod create_bucket_class_location;
mod create_bucket_dual_region;
mod create_bucket_hierarchical_namespace;
mod define_bucket_website_configuration;
mod delete_bucket;
mod disable_bucket_lifecycle_management;
mod disable_default_event_based_hold;
Expand All @@ -31,6 +32,7 @@ mod list_buckets;
mod objects;
mod print_bucket_acl;
mod print_bucket_acl_for_user;
mod print_bucket_website_configuration;
mod quickstart;
mod remove_bucket_owner;
mod set_lifecycle_abort_multipart_upload;
Expand Down Expand Up @@ -137,7 +139,10 @@ pub async fn run_bucket_examples(buckets: &mut Vec<String>) -> anyhow::Result<()
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_website_configuration example");
print_bucket_website_configuration::sample(&client, &id).await?;
tracing::info!("running define_bucket_website_configuration example");
define_bucket_website_configuration::sample(&client, &id, "index.html", "404.html").await?;
tracing::info!("running print_bucket_acl example");
print_bucket_acl::sample(&client, &id).await?;
tracing::info!("running add_bucket_owner example");
Expand Down
30 changes: 30 additions & 0 deletions src/storage/examples/src/print_bucket_website_configuration.rs
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_print_bucket_website_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!(
"Website configuration for bucket {}: {:?}",
bucket_id, bucket.website
);
Ok(())
}
// [END storage_print_bucket_website_configuration]
Loading