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
2 changes: 2 additions & 0 deletions src/storage/examples/src/buckets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod add_bucket_conditional_iam_binding;
pub mod add_bucket_iam_member;
pub mod add_bucket_owner;
pub mod change_default_storage_class;
pub mod cors_configuration;
pub mod create_bucket;
pub mod create_bucket_class_location;
pub mod create_bucket_dual_region;
Expand All @@ -39,6 +40,7 @@ pub mod print_bucket_acl;
pub mod print_bucket_acl_for_user;
pub mod print_bucket_website_configuration;
pub mod remove_bucket_owner;
pub mod remove_cors_configuration;
pub mod remove_retention_policy;
pub mod set_autoclass;
#[allow(dead_code)]
Expand Down
46 changes: 46 additions & 0 deletions src/storage/examples/src/buckets/cors_configuration.rs
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_cors_configuration]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::Cors;
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 cors = bucket.cors.clone();
cors.push(
Cors::new()
.set_origin(["http://example.appspot.com".to_string()])
.set_method(["GET".to_string(), "HEAD".to_string(), "DELETE".to_string()])
.set_response_header(["Content-Type".to_string()])
.set_max_age_seconds(3600),
);

let bucket = client
.update_bucket()
.set_bucket(bucket.set_cors(cors))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["cors"]))
.send()
.await?;
println!("successfully updated bucket CORS for {bucket_id}: {bucket:?}");
Ok(())
}
// [END storage_cors_configuration]
41 changes: 41 additions & 0 deletions src/storage/examples/src/buckets/remove_cors_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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_remove_cors_configuration]
use google_cloud_storage::client::StorageControl;
use google_cloud_storage::model::bucket::Cors;
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_cors(Vec::<Cors>::new()))
.set_if_metageneration_match(metageneration)
.set_update_mask(FieldMask::default().set_paths(["cors"]))
.send()
.await?;
println!(
"successfully removed CORS rule for bucket {bucket_id}: {:?}",
bucket.cors
);
Ok(())
}
// [END storage_remove_cors_configuration]
4 changes: 4 additions & 0 deletions src/storage/examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ pub async fn run_bucket_examples(buckets: &mut Vec<String>) -> anyhow::Result<()
tracing::info!("running define_bucket_website_configuration example");
buckets::define_bucket_website_configuration::sample(&client, &id, "index.html", "404.html")
.await?;
tracing::info!("running cors_configuiration example");
buckets::cors_configuration::sample(&client, &id).await?;
tracing::info!("running remove_cors_configuration example");
buckets::remove_cors_configuration::sample(&client, &id).await?;
tracing::info!("running remove_retention_policy example");
buckets::remove_retention_policy::sample(&client, &id).await?;
tracing::info!("running set_retention_policy example");
Expand Down
Loading