Skip to content

Commit 3030081

Browse files
authored
docs: add Hierarchical Namespace Bucket and Folders samples (#2583)
New samples * `storage_create_bucket_hierarchical_namespace` * `storage_control_create_folder` * `storage_control_delete_folder` * `storage_control_get_folder` * `storage_control_list_folders` * `storage_control_rename_folder` Fixes #2569
1 parent 65c8808 commit 3030081

File tree

8 files changed

+516
-0
lines changed

8 files changed

+516
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_create_folder]
20+
import com.google.storage.control.v2.BucketName;
21+
import com.google.storage.control.v2.CreateFolderRequest;
22+
import com.google.storage.control.v2.Folder;
23+
import com.google.storage.control.v2.StorageControlClient;
24+
import java.io.IOException;
25+
26+
public final class CreateFolder {
27+
28+
public static void createFolder(String bucketName, String folderName) throws IOException {
29+
// The name of the bucket
30+
// String bucketName = "your-unique-bucket-name";
31+
32+
// The name of the folder within the bucket
33+
// String folderName = "your-unique-folder-name";
34+
35+
try (StorageControlClient storageControl = StorageControlClient.create()) {
36+
37+
CreateFolderRequest request =
38+
CreateFolderRequest.newBuilder()
39+
// Set project to "_" to signify globally scoped bucket
40+
.setParent(BucketName.format("_", bucketName))
41+
.setFolderId(folderName)
42+
.build();
43+
44+
Folder newFolder = storageControl.createFolder(request);
45+
46+
System.out.printf("Created folder: %s%n", newFolder.getName());
47+
}
48+
}
49+
}
50+
// [END storage_control_create_folder]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_create_bucket_hierarchical_namespace]
20+
import com.google.cloud.storage.Bucket;
21+
import com.google.cloud.storage.BucketInfo;
22+
import com.google.cloud.storage.BucketInfo.HierarchicalNamespace;
23+
import com.google.cloud.storage.BucketInfo.IamConfiguration;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
27+
public final class CreateHierarchicalNamespaceBucket {
28+
29+
public static void createHierarchicalNamespaceBucket(String projectId, String bucketName)
30+
throws Exception {
31+
// The ID of your GCP project
32+
// String projectId = "your-project-id";
33+
34+
// The ID to give your GCS bucket
35+
// String bucketName = "your-unique-bucket-name";
36+
StorageOptions storageOptions = StorageOptions.newBuilder().setProjectId(projectId).build();
37+
try (Storage storage = storageOptions.getService()) {
38+
39+
BucketInfo bucketInfo =
40+
BucketInfo.newBuilder(bucketName)
41+
.setIamConfiguration(
42+
// Hierarchical namespace buckets must use uniform bucket-level access.
43+
IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build())
44+
.setHierarchicalNamespace(HierarchicalNamespace.newBuilder().setEnabled(true).build())
45+
.build();
46+
47+
Bucket bucket = storage.create(bucketInfo);
48+
49+
System.out.printf(
50+
"Created bucket %s with Hierarchical Namespace enabled.%n", bucket.getName());
51+
}
52+
}
53+
}
54+
// [END storage_create_bucket_hierarchical_namespace]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_delete_folder]
20+
21+
import com.google.storage.control.v2.DeleteFolderRequest;
22+
import com.google.storage.control.v2.FolderName;
23+
import com.google.storage.control.v2.StorageControlClient;
24+
import java.io.IOException;
25+
26+
public final class DeleteFolder {
27+
28+
public static void deleteFolder(String bucketName, String folderName) throws IOException {
29+
// The name of the bucket
30+
// String bucketName = "your-unique-bucket-name";
31+
32+
// The name of the folder within the bucket
33+
// String folderName = "your-unique-folder-name";
34+
35+
try (StorageControlClient storageControl = StorageControlClient.create()) {
36+
37+
// Set project to "_" to signify globally scoped bucket
38+
String folderResourceName = FolderName.format("_", bucketName, folderName);
39+
DeleteFolderRequest request =
40+
DeleteFolderRequest.newBuilder().setName(folderResourceName).build();
41+
42+
storageControl.deleteFolder(request);
43+
44+
System.out.printf("Deleted folder: %s%n", folderResourceName);
45+
}
46+
}
47+
}
48+
// [END storage_control_delete_folder]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_get_folder]
20+
21+
import com.google.storage.control.v2.Folder;
22+
import com.google.storage.control.v2.FolderName;
23+
import com.google.storage.control.v2.GetFolderRequest;
24+
import com.google.storage.control.v2.StorageControlClient;
25+
import java.io.IOException;
26+
27+
public final class GetFolder {
28+
29+
public static void getFolder(String bucketName, String folderName) throws IOException {
30+
// The name of the bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
// The name of the folder within the bucket
34+
// String folderName = "your-unique-folder-name";
35+
36+
try (StorageControlClient storageControl = StorageControlClient.create()) {
37+
38+
GetFolderRequest request =
39+
GetFolderRequest.newBuilder()
40+
// Set project to "_" to signify globally scoped bucket
41+
.setName(FolderName.format("_", bucketName, folderName))
42+
.build();
43+
44+
Folder newFolder = storageControl.getFolder(request);
45+
46+
System.out.printf("Got folder: %s%n", newFolder.getName());
47+
}
48+
}
49+
}
50+
// [END storage_control_get_folder]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_list_folders]
20+
21+
import com.google.storage.control.v2.BucketName;
22+
import com.google.storage.control.v2.Folder;
23+
import com.google.storage.control.v2.ListFoldersRequest;
24+
import com.google.storage.control.v2.StorageControlClient;
25+
import java.io.IOException;
26+
27+
public final class ListFolders {
28+
29+
public static void listFolders(String bucketName) throws IOException {
30+
// The name of the bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
try (StorageControlClient storageControl = StorageControlClient.create()) {
34+
35+
ListFoldersRequest request =
36+
ListFoldersRequest.newBuilder()
37+
// Set project to "_" to signify globally scoped bucket
38+
.setParent(BucketName.format("_", bucketName))
39+
.build();
40+
41+
Iterable<Folder> folders = storageControl.listFolders(request).iterateAll();
42+
for (Folder folder : folders) {
43+
System.out.printf("Found folder: %s%n", folder.getName());
44+
}
45+
}
46+
}
47+
}
48+
// [END storage_control_list_folders]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.storage.control.v2;
18+
19+
// [START storage_control_rename_folder]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.storage.control.v2.Folder;
23+
import com.google.storage.control.v2.FolderName;
24+
import com.google.storage.control.v2.RenameFolderMetadata;
25+
import com.google.storage.control.v2.RenameFolderRequest;
26+
import com.google.storage.control.v2.StorageControlClient;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public final class RenameFolder {
33+
34+
public static void renameFolder(
35+
String bucketName, String sourceFolderName, String destinationFolderName)
36+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
37+
// The name of the bucket
38+
// String bucketName = "your-unique-bucket-name";
39+
40+
// The name of the folder within the bucket
41+
// String sourceFolderName = "your-unique-source-folder-name";
42+
43+
// The new name of the folder within the bucket
44+
// String destinationFolderName = "your-unique-destination-folder-name";
45+
46+
try (StorageControlClient storageControl = StorageControlClient.create()) {
47+
48+
// Set project to "_" to signify globally scoped bucket
49+
String sourceFolderResourceName = FolderName.format("_", bucketName, sourceFolderName);
50+
RenameFolderRequest request =
51+
RenameFolderRequest.newBuilder()
52+
.setName(sourceFolderResourceName)
53+
.setDestinationFolderId(destinationFolderName)
54+
.build();
55+
56+
OperationFuture<Folder, RenameFolderMetadata> renameOperation =
57+
storageControl.renameFolderAsync(request);
58+
59+
Folder destinationFolder = renameOperation.get(30, TimeUnit.SECONDS);
60+
61+
System.out.printf(
62+
"Renamed folder from %s to %s%n", sourceFolderResourceName, destinationFolder.getName());
63+
}
64+
}
65+
}
66+
// [END storage_control_rename_folder]

0 commit comments

Comments
 (0)