-
Notifications
You must be signed in to change notification settings - Fork 103
docs(storage): more examples for buckets #2880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
coryan
merged 4 commits into
googleapis:main
from
coryan:docs-storage-more-bucket-examples
Aug 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // 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_change_default_storage_class] | ||
| use google_cloud_storage::client::StorageControl; | ||
| use google_cloud_wkt::FieldMask; | ||
|
|
||
| pub async fn change_default_storage_class( | ||
| 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_storage_class("NEARLINE")) | ||
| .set_if_metageneration_match(metageneration) | ||
| .set_update_mask(FieldMask::default().set_paths(["storage_class"])) | ||
| .send() | ||
| .await?; | ||
| println!("successfully updated bucket {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_change_default_storage_class] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // 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_create_bucket] | ||
| use google_cloud_storage::{client::StorageControl, model::Bucket}; | ||
|
|
||
| pub async fn create_bucket( | ||
| client: &StorageControl, | ||
| project_id: &str, | ||
| bucket_id: &str, | ||
| ) -> anyhow::Result<()> { | ||
| let bucket = client | ||
| .create_bucket() | ||
| .set_parent("projects/_") | ||
| .set_bucket_id(bucket_id) | ||
| .set_bucket(Bucket::new().set_project(format!("projects/{project_id}"))) | ||
| .send() | ||
| .await?; | ||
| println!("successfully created bucket {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_create_bucket] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // 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_create_bucket_class_and_location] | ||
| use google_cloud_storage::{client::StorageControl, model::Bucket}; | ||
|
|
||
| pub async fn create_bucket_class_and_location( | ||
| client: &StorageControl, | ||
| project_id: &str, | ||
| bucket_id: &str, | ||
| ) -> anyhow::Result<()> { | ||
| let bucket = client | ||
| .create_bucket() | ||
| .set_parent("projects/_") | ||
| .set_bucket_id(bucket_id) | ||
| .set_bucket( | ||
| Bucket::new() | ||
| .set_project(format!("projects/{project_id}")) | ||
| .set_storage_class("NEARLINE") | ||
| .set_location("US-CENTRAL1"), | ||
| ) | ||
| .send() | ||
| .await?; | ||
| println!("successfully created bucket {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_create_bucket_class_and_location] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_create_bucket_dual_region] | ||
| use google_cloud_storage::{ | ||
| client::StorageControl, model::Bucket, model::bucket::CustomPlacementConfig, | ||
| }; | ||
|
|
||
| pub async fn create_bucket_dual_region( | ||
| client: &StorageControl, | ||
| project_id: &str, | ||
| bucket_id: &str, | ||
| ) -> anyhow::Result<()> { | ||
| let bucket = client | ||
| .create_bucket() | ||
| .set_parent("projects/_") | ||
| .set_bucket_id(bucket_id) | ||
| .set_bucket( | ||
| Bucket::new() | ||
| .set_project(format!("projects/{project_id}")) | ||
| .set_custom_placement_config( | ||
| CustomPlacementConfig::new().set_data_locations(["US-EAST4", "US-CENTRAL1"]), | ||
| ), | ||
| ) | ||
| .send() | ||
| .await?; | ||
| println!("successfully created bucket {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_create_bucket_dual_region] |
45 changes: 45 additions & 0 deletions
45
src/storage/examples/src/create_bucket_hierarchical_namespace.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_create_bucket_hierarchical_namespace] | ||
| use google_cloud_storage::{ | ||
| client::StorageControl, | ||
| model::Bucket, | ||
| model::bucket::iam_config::UniformBucketLevelAccess, | ||
| model::bucket::{HierarchicalNamespace, IamConfig}, | ||
| }; | ||
|
|
||
| pub async fn create_bucket_hierarchical_namespace( | ||
| client: &StorageControl, | ||
| project_id: &str, | ||
| bucket_id: &str, | ||
| ) -> anyhow::Result<()> { | ||
| let bucket = client | ||
| .create_bucket() | ||
| .set_parent("projects/_") | ||
| .set_bucket_id(bucket_id) | ||
| .set_bucket( | ||
| Bucket::new() | ||
| .set_project(format!("projects/{project_id}")) | ||
| .set_hierarchical_namespace(HierarchicalNamespace::new().set_enabled(true)) | ||
| .set_iam_config(IamConfig::new().set_uniform_bucket_level_access( | ||
| UniformBucketLevelAccess::new().set_enabled(true), | ||
| )), | ||
| ) | ||
| .send() | ||
| .await?; | ||
| println!("successfully created bucket {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_create_bucket_hierarchical_namespace] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // 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_delete_bucket] | ||
| use google_cloud_storage::client::StorageControl; | ||
|
|
||
| pub async fn delete_bucket(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> { | ||
| client | ||
| .delete_bucket() | ||
| .set_name(format!("projects/_/buckets/{bucket_id}")) | ||
| .send() | ||
| .await?; | ||
| println!("successfully deleted bucket {bucket_id}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_delete_bucket] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_get_bucket_class_and_location] | ||
| // [START storage_get_bucket_labels] | ||
| // [START storage_get_bucket_metadata] | ||
| // [START storage_get_public_access_prevention] | ||
| // [START storage_get_rpo] | ||
| // [START storage_get_uniform_bucket_level_access] | ||
| use google_cloud_storage::client::StorageControl; | ||
|
|
||
| pub async fn get_bucket_metadata(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> { | ||
| let bucket = client | ||
| .get_bucket() | ||
| .set_name(format!("projects/_/buckets/{bucket_id}")) | ||
| .send() | ||
| .await?; | ||
| println!("successfully obtained bucket metadata {bucket:?}"); | ||
| Ok(()) | ||
| } | ||
| // [END storage_get_uniform_bucket_level_access] | ||
| // [END storage_get_rpo] | ||
| // [END storage_get_public_access_prevention] | ||
| // [END storage_get_bucket_metadata] | ||
| // [END storage_get_bucket_labels] | ||
| // [END storage_get_bucket_class_and_location] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.