-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat(storagecontrol): Added samples for Anywhere Cache #14021
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Copyright 2026 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. | ||
|
|
||
| import sys | ||
|
|
||
| # [START storage_control_create_anywhere_cache] | ||
| from google.cloud import storage_control_v2 | ||
|
|
||
|
|
||
| def create_anywhere_cache( | ||
| bucket_name: str, zone: str, admission_policy: str = "ADMIT_ON_FIRST_MISS" | ||
| ) -> None: | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "your-unique-bucket-name" | ||
|
|
||
| # The zone where the Anywhere Cache will be created | ||
| # zone = "us-central1-a" | ||
|
|
||
| # The admission policy for the Anywhere Cache | ||
| # admission_policy = "ADMIT_ON_FIRST_MISS" | ||
|
|
||
| client = storage_control_v2.StorageControlClient() | ||
|
|
||
| # The storage bucket path uses the global access pattern, in which the "_" | ||
| # denotes this bucket exists in the global namespace. | ||
| project_path = client.common_project_path("_") | ||
| bucket_path = f"{project_path}/buckets/{bucket_name}" | ||
|
|
||
| anywhere_cache = storage_control_v2.AnywhereCache( | ||
| admission_policy=admission_policy, | ||
| ) | ||
|
|
||
| request = storage_control_v2.CreateAnywhereCacheRequest( | ||
| parent=bucket_path, | ||
| anywhere_cache=anywhere_cache, | ||
| anywhere_cache_id=zone, | ||
| ) | ||
|
|
||
| # create_anywhere_cache is a long-running operation. | ||
| # Real applications may want to setup a callback or poll for the operation to complete. | ||
| operation = client.create_anywhere_cache(request=request) | ||
| response = operation.result() | ||
|
|
||
| print(f"Created Anywhere Cache: {response.name}") | ||
|
|
||
|
|
||
| # [END storage_control_create_anywhere_cache] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| create_anywhere_cache( | ||
| bucket_name=sys.argv[1], zone=sys.argv[2], admission_policy=sys.argv[3] | ||
| ) | ||
|
Comment on lines
+62
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||
| # Copyright 2026 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. | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| # [START storage_control_disable_anywhere_cache] | ||||||||
| from google.cloud import storage_control_v2 | ||||||||
|
|
||||||||
|
|
||||||||
| def disable_anywhere_cache(bucket_name: str, zone: str) -> None: | ||||||||
| # The ID of your GCS bucket | ||||||||
| # bucket_name = "your-unique-bucket-name" | ||||||||
|
|
||||||||
| # The zone of the Anywhere Cache | ||||||||
| # zone = "us-central1-a" | ||||||||
|
|
||||||||
| client = storage_control_v2.StorageControlClient() | ||||||||
|
|
||||||||
| # The storage bucket path uses the global access pattern, in which the "_" | ||||||||
| # denotes this bucket exists in the global namespace. | ||||||||
| project_path = client.common_project_path("_") | ||||||||
| name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}" | ||||||||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the client's path helper methods is preferred over manual string concatenation for resource names. This improves readability and reduces the risk of errors in the resource path structure.
Suggested change
|
||||||||
|
|
||||||||
| request = storage_control_v2.DisableAnywhereCacheRequest( | ||||||||
| name=name, | ||||||||
| ) | ||||||||
| response = client.disable_anywhere_cache(request=request) | ||||||||
|
|
||||||||
| print(f"Disabled Anywhere Cache: {response.name}") | ||||||||
|
|
||||||||
|
|
||||||||
| # [END storage_control_disable_anywhere_cache] | ||||||||
|
|
||||||||
|
|
||||||||
| if __name__ == "__main__": | ||||||||
| disable_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||
| # Copyright 2026 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. | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| # [START storage_control_get_anywhere_cache] | ||||||||
| from google.cloud import storage_control_v2 | ||||||||
|
|
||||||||
|
|
||||||||
| def get_anywhere_cache(bucket_name: str, zone: str) -> None: | ||||||||
| # The ID of your GCS bucket | ||||||||
| # bucket_name = "your-unique-bucket-name" | ||||||||
|
|
||||||||
| # The zone of the Anywhere Cache | ||||||||
| # zone = "us-central1-a" | ||||||||
|
|
||||||||
| client = storage_control_v2.StorageControlClient() | ||||||||
|
|
||||||||
| # The storage bucket path uses the global access pattern, in which the "_" | ||||||||
| # denotes this bucket exists in the global namespace. | ||||||||
| project_path = client.common_project_path("_") | ||||||||
| name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}" | ||||||||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the
Suggested change
|
||||||||
|
|
||||||||
| request = storage_control_v2.GetAnywhereCacheRequest( | ||||||||
| name=name, | ||||||||
| ) | ||||||||
| response = client.get_anywhere_cache(request=request) | ||||||||
|
|
||||||||
| print(f"Anywhere Cache: {response.name}") | ||||||||
| print(f"Admission Policy: {response.admission_policy}") | ||||||||
| print(f"State: {response.state}") | ||||||||
|
|
||||||||
|
|
||||||||
| # [END storage_control_get_anywhere_cache] | ||||||||
|
|
||||||||
|
|
||||||||
| if __name__ == "__main__": | ||||||||
| get_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||||
| # Copyright 2026 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. | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| # [START storage_control_list_anywhere_caches] | ||||||||
| from google.cloud import storage_control_v2 | ||||||||
|
|
||||||||
|
|
||||||||
| def list_anywhere_caches(bucket_name: str) -> None: | ||||||||
| # The ID of your GCS bucket | ||||||||
| # bucket_name = "your-unique-bucket-name" | ||||||||
|
|
||||||||
| client = storage_control_v2.StorageControlClient() | ||||||||
|
|
||||||||
| # The storage bucket path uses the global access pattern, in which the "_" | ||||||||
| # denotes this bucket exists in the global namespace. | ||||||||
| project_path = client.common_project_path("_") | ||||||||
| bucket_path = f"{project_path}/buckets/{bucket_name}" | ||||||||
|
Comment on lines
+29
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The client provides a
Suggested change
|
||||||||
|
|
||||||||
| request = storage_control_v2.ListAnywhereCachesRequest( | ||||||||
| parent=bucket_path, | ||||||||
| ) | ||||||||
| page_result = client.list_anywhere_caches(request=request) | ||||||||
|
|
||||||||
| for response in page_result: | ||||||||
| print(f"Anywhere Cache: {response.name}") | ||||||||
|
|
||||||||
|
|
||||||||
| # [END storage_control_list_anywhere_caches] | ||||||||
|
|
||||||||
|
|
||||||||
| if __name__ == "__main__": | ||||||||
| list_anywhere_caches(bucket_name=sys.argv[1]) | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||
| # Copyright 2026 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. | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| # [START storage_control_pause_anywhere_cache] | ||||||||
| from google.cloud import storage_control_v2 | ||||||||
|
|
||||||||
|
|
||||||||
| def pause_anywhere_cache(bucket_name: str, zone: str) -> None: | ||||||||
| # The ID of your GCS bucket | ||||||||
| # bucket_name = "your-unique-bucket-name" | ||||||||
|
|
||||||||
| # The zone of the Anywhere Cache | ||||||||
| # zone = "us-central1-a" | ||||||||
|
|
||||||||
| client = storage_control_v2.StorageControlClient() | ||||||||
|
|
||||||||
| # The storage bucket path uses the global access pattern, in which the "_" | ||||||||
| # denotes this bucket exists in the global namespace. | ||||||||
| project_path = client.common_project_path("_") | ||||||||
| name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}" | ||||||||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the
Suggested change
|
||||||||
|
|
||||||||
| request = storage_control_v2.PauseAnywhereCacheRequest( | ||||||||
| name=name, | ||||||||
| ) | ||||||||
| response = client.pause_anywhere_cache(request=request) | ||||||||
|
|
||||||||
| print(f"Paused Anywhere Cache: {response.name}") | ||||||||
|
|
||||||||
|
|
||||||||
| # [END storage_control_pause_anywhere_cache] | ||||||||
|
|
||||||||
|
|
||||||||
| if __name__ == "__main__": | ||||||||
| pause_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||
| # Copyright 2026 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. | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| # [START storage_control_resume_anywhere_cache] | ||||||||
| from google.cloud import storage_control_v2 | ||||||||
|
|
||||||||
|
|
||||||||
| def resume_anywhere_cache(bucket_name: str, zone: str) -> None: | ||||||||
| # The ID of your GCS bucket | ||||||||
| # bucket_name = "your-unique-bucket-name" | ||||||||
|
|
||||||||
| # The zone of the Anywhere Cache | ||||||||
| # zone = "us-central1-a" | ||||||||
|
|
||||||||
| client = storage_control_v2.StorageControlClient() | ||||||||
|
|
||||||||
| # The storage bucket path uses the global access pattern, in which the "_" | ||||||||
| # denotes this bucket exists in the global namespace. | ||||||||
| project_path = client.common_project_path("_") | ||||||||
| name = f"{project_path}/buckets/{bucket_name}/anywhereCaches/{zone}" | ||||||||
|
Comment on lines
+32
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use the
Suggested change
|
||||||||
|
|
||||||||
| request = storage_control_v2.ResumeAnywhereCacheRequest( | ||||||||
| name=name, | ||||||||
| ) | ||||||||
| response = client.resume_anywhere_cache(request=request) | ||||||||
|
|
||||||||
| print(f"Resumed Anywhere Cache: {response.name}") | ||||||||
|
|
||||||||
|
|
||||||||
| # [END storage_control_resume_anywhere_cache] | ||||||||
|
|
||||||||
|
|
||||||||
| if __name__ == "__main__": | ||||||||
| resume_anywhere_cache(bucket_name=sys.argv[1], zone=sys.argv[2]) | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to use the client's built-in path helper methods instead of manually constructing resource names with f-strings. This ensures the resource name follows the correct pattern and is more maintainable.