|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import sys |
| 15 | +import argparse |
16 | 16 |
|
17 | 17 | # [START storage_control_managed_folder_list]
|
18 | 18 | from google.cloud import storage_control_v2
|
19 | 19 |
|
20 | 20 |
|
21 |
| -def list_managed_folders(bucket_name: str) -> None: |
22 |
| - # The ID of your GCS bucket |
23 |
| - # bucket_name = "your-unique-bucket-name" |
| 21 | +def list_managed_folders(bucket_name: str = "your-bucket-name") -> None: |
| 22 | + """Lists all managed folders in a Google Cloud Storage bucket. |
24 | 23 |
|
25 |
| - storage_control_client = storage_control_v2.StorageControlClient() |
26 |
| - # The storage bucket path uses the global access pattern, in which the "_" |
27 |
| - # denotes this bucket exists in the global namespace. |
28 |
| - project_path = storage_control_client.common_project_path("_") |
29 |
| - bucket_path = f"{project_path}/buckets/{bucket_name}" |
| 24 | + Args: |
| 25 | + bucket_name: The name of the Google Cloud Storage bucket. |
30 | 26 |
|
31 |
| - request = storage_control_v2.ListManagedFoldersRequest( |
32 |
| - parent=bucket_path, |
33 |
| - ) |
| 27 | + Returns: |
| 28 | + None. The function prints the name of each managed folder to the |
| 29 | + console. |
34 | 30 |
|
35 |
| - page_result = storage_control_client.list_managed_folders(request=request) |
36 |
| - for managed_folder in page_result: |
37 |
| - print(managed_folder) |
| 31 | + Example: |
| 32 | + >>> list_managed_folders(bucket_name="my-test-bucket") |
| 33 | + Managed folders in bucket projects/_/buckets/my-test-bucket: |
| 34 | + projects/_/buckets/my-test-bucket/managedFolders/folder-one |
| 35 | + projects/_/buckets/my-test-bucket/managedFolders/folder-two |
| 36 | + """ |
| 37 | + client = storage_control_v2.StorageControlClient() |
38 | 38 |
|
39 |
| - print(f"Listed managed folders in bucket {bucket_name}") |
| 39 | + # The storage bucket path uses the global access pattern, |
| 40 | + # in which the "_" denotes this bucket exists in the global namespace. |
| 41 | + GLOBAL_NAMESPACE_PATTERN = "_" |
| 42 | + bucket_resource_name = f"projects/{GLOBAL_NAMESPACE_PATTERN}/buckets/{bucket_name}" |
40 | 43 |
|
| 44 | + managed_folders = client.list_managed_folders(parent=bucket_resource_name) |
41 | 45 |
|
| 46 | + print(f"Managed folders in bucket {bucket_resource_name}:") |
| 47 | + for managed_folder in managed_folders: |
| 48 | + print(f"\t{managed_folder.name}") |
42 | 49 | # [END storage_control_managed_folder_list]
|
43 | 50 |
|
44 | 51 |
|
45 | 52 | if __name__ == "__main__":
|
46 |
| - list_managed_folders(bucket_name=sys.argv[1]) |
| 53 | + parser = argparse.ArgumentParser() |
| 54 | + parser.add_argument( |
| 55 | + "bucket_name", |
| 56 | + help="The name of the GCS bucket.", |
| 57 | + ) |
| 58 | + |
| 59 | + args = parser.parse_args() |
| 60 | + |
| 61 | + list_managed_folders(args.bucket_name) |
0 commit comments