Skip to content

Commit 5ffc1f2

Browse files
docs: Managed Folders samples (#2562)
* docs: Managed Folders samples * linter update without managed folder name populated lint format bucketname enable uniform bucket level access moving config update enabling uniform bucket level access figure out why tests are failing use format instead of linter fixing tests finally fix test and add in other tests linter adding copyrights to tests removing unnecessary changed to downloadbytesrangetest * shutdown storage control instances * Update samples/snippets/src/main/java/com/example/storage/managedfolders/CreateManagedFolder.java Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com> * pr comments * linter --------- Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com>
1 parent 129f188 commit 5ffc1f2

File tree

8 files changed

+528
-0
lines changed

8 files changed

+528
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.managedfolders;
18+
19+
// [START storage_control_managed_folder_create]
20+
21+
import com.google.storage.control.v2.BucketName;
22+
import com.google.storage.control.v2.CreateManagedFolderRequest;
23+
import com.google.storage.control.v2.ManagedFolder;
24+
import com.google.storage.control.v2.StorageControlClient;
25+
26+
public class CreateManagedFolder {
27+
public static void managedFolderCreate(String bucketName, String managedFolderId)
28+
throws Exception {
29+
30+
// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
31+
try (StorageControlClient storageControlClient = StorageControlClient.create()) {
32+
CreateManagedFolderRequest request =
33+
CreateManagedFolderRequest.newBuilder()
34+
// Set project to "_" to signify global bucket
35+
.setParent(BucketName.format("_", bucketName))
36+
.setManagedFolder(ManagedFolder.newBuilder().build())
37+
.setManagedFolderId(managedFolderId).build();
38+
String response = storageControlClient.createManagedFolder(request).getName();
39+
System.out.printf("Performed createManagedFolder request for %s%n", response);
40+
}
41+
}
42+
}
43+
// [END storage_control_managed_folder_create]
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.managedfolders;
18+
19+
// [START storage_control_managed_folder_delete]
20+
import com.google.storage.control.v2.BucketName;
21+
import com.google.storage.control.v2.DeleteManagedFolderRequest;
22+
import com.google.storage.control.v2.ManagedFolderName;
23+
import com.google.storage.control.v2.StorageControlClient;
24+
25+
class DeleteManagedFolder {
26+
public static void managedFolderDelete(String bucketName, String managedFolderId)
27+
throws Exception {
28+
// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
29+
try (StorageControlClient storageControlClient = StorageControlClient.create()) {
30+
// Set project to "_" to signify global bucket
31+
BucketName resourceBucketName = BucketName.of("_", bucketName);
32+
DeleteManagedFolderRequest deleteManagedFolderRequest =
33+
DeleteManagedFolderRequest.newBuilder()
34+
.setName(
35+
ManagedFolderName.format(
36+
resourceBucketName.getProject(),
37+
resourceBucketName.getBucket(),
38+
managedFolderId)
39+
)
40+
.build();
41+
storageControlClient.deleteManagedFolder(deleteManagedFolderRequest);
42+
System.out.printf("Deleted Managed Folder %s%n", managedFolderId);
43+
}
44+
}
45+
46+
}
47+
48+
// [END storage_control_managed_folder_delete]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.managedfolders;
18+
19+
// [START storage_control_managed_folder_get]
20+
21+
import com.google.storage.control.v2.BucketName;
22+
import com.google.storage.control.v2.GetManagedFolderRequest;
23+
import com.google.storage.control.v2.ManagedFolder;
24+
import com.google.storage.control.v2.ManagedFolderName;
25+
import com.google.storage.control.v2.StorageControlClient;
26+
27+
class GetManagedFolder {
28+
29+
public static void managedFolderGet(String bucketName, String managedFolderId) throws Exception {
30+
// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
31+
try (StorageControlClient storageControlClient = StorageControlClient.create()) {
32+
// Set project to "_" to signify global bucket
33+
BucketName resourceBucketName = BucketName.of("_", bucketName);
34+
GetManagedFolderRequest getManagedFolderRequest =
35+
GetManagedFolderRequest.newBuilder()
36+
.setName(
37+
ManagedFolderName.format(
38+
resourceBucketName.getProject(),
39+
resourceBucketName.getBucket(),
40+
managedFolderId))
41+
.build();
42+
ManagedFolder managedFolder = storageControlClient.getManagedFolder(getManagedFolderRequest);
43+
System.out.printf("Got Managed Folder %s%n", managedFolder.getName());
44+
}
45+
}
46+
47+
}
48+
49+
// [END storage_control_managed_folder_get]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.managedfolders;
18+
19+
// [START storage_control_managed_folder_list]
20+
21+
import com.google.storage.control.v2.BucketName;
22+
import com.google.storage.control.v2.ListManagedFoldersRequest;
23+
import com.google.storage.control.v2.ManagedFolder;
24+
import com.google.storage.control.v2.StorageControlClient;
25+
26+
class ListManagedFolders {
27+
28+
public static void managedFolderList(String bucketName) throws Exception {
29+
// Instantiates a client in a try-with-resource to automatically cleanup underlying resources
30+
try (StorageControlClient storageControlClient = StorageControlClient.create()) {
31+
ListManagedFoldersRequest listManagedFoldersRequest =
32+
ListManagedFoldersRequest.newBuilder()
33+
// Set project to "_" to signify global bucket
34+
.setParent(BucketName.format("_", bucketName))
35+
.build();
36+
Iterable<ManagedFolder> managedFolders =
37+
storageControlClient.listManagedFolders(listManagedFoldersRequest).iterateAll();
38+
for (ManagedFolder folder : managedFolders) {
39+
System.out.printf("%s bucket has managed folder %s%n", bucketName, folder.getName());
40+
}
41+
}
42+
}
43+
}
44+
45+
// [END storage_control_managed_folder_list]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.managedfolders;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.storage.Bucket;
22+
import com.google.cloud.storage.BucketInfo;
23+
import com.google.cloud.storage.BucketInfo.IamConfiguration;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import com.google.cloud.storage.testing.RemoteStorageHelper;
27+
import com.google.cloud.testing.junit4.StdOutCaptureRule;
28+
import com.google.storage.control.v2.DeleteManagedFolderRequest;
29+
import com.google.storage.control.v2.ManagedFolderName;
30+
import com.google.storage.control.v2.StorageControlClient;
31+
import java.io.IOException;
32+
import java.util.UUID;
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.Rule;
36+
import org.junit.Test;
37+
38+
public class CreateManagedFolderTest {
39+
40+
@Rule
41+
public StdOutCaptureRule stdOut = new StdOutCaptureRule();
42+
43+
protected String bucketName;
44+
protected Storage storage;
45+
protected Bucket bucket;
46+
protected String managedFolderId;
47+
protected StorageControlClient storageControl;
48+
49+
@Before
50+
public void setUp() throws IOException {
51+
bucketName = RemoteStorageHelper.generateBucketName();
52+
storageControl = StorageControlClient.create();
53+
storage = StorageOptions.getDefaultInstance().getService();
54+
managedFolderId = "new-managed-folder-" + UUID.randomUUID();
55+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
56+
.setIamConfiguration(
57+
IamConfiguration
58+
.newBuilder()
59+
.setIsUniformBucketLevelAccessEnabled(true)
60+
.build()).build();
61+
bucket = storage.create(bucketInfo);
62+
}
63+
64+
@After
65+
public void tearDown() {
66+
storageControl.deleteManagedFolder(
67+
DeleteManagedFolderRequest.newBuilder().setName(
68+
ManagedFolderName.format("_", bucketName, managedFolderId)
69+
).build());
70+
storage.delete(bucketName);
71+
storageControl.shutdown();
72+
}
73+
74+
@Test
75+
public void testCreateManagedFolder() throws Exception {
76+
CreateManagedFolder.managedFolderCreate(bucketName, managedFolderId);
77+
String got = stdOut.getCapturedOutputAsUtf8String();
78+
assertThat(got).contains(String.format(managedFolderId));
79+
}
80+
}
81+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.managedfolders;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.storage.Bucket;
22+
import com.google.cloud.storage.BucketInfo;
23+
import com.google.cloud.storage.BucketInfo.IamConfiguration;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import com.google.cloud.storage.testing.RemoteStorageHelper;
27+
import com.google.cloud.testing.junit4.StdOutCaptureRule;
28+
import com.google.storage.control.v2.BucketName;
29+
import com.google.storage.control.v2.CreateManagedFolderRequest;
30+
import com.google.storage.control.v2.ManagedFolder;
31+
import com.google.storage.control.v2.StorageControlClient;
32+
import java.io.IOException;
33+
import java.util.UUID;
34+
import org.junit.After;
35+
import org.junit.Before;
36+
import org.junit.Rule;
37+
import org.junit.Test;
38+
39+
public class DeleteManagedFolderTest {
40+
41+
@Rule
42+
public StdOutCaptureRule stdOut = new StdOutCaptureRule();
43+
44+
protected String bucketName;
45+
protected Storage storage;
46+
protected Bucket bucket;
47+
protected String managedFolderId;
48+
protected StorageControlClient storageControl;
49+
50+
@Before
51+
public void setUp() throws IOException {
52+
bucketName = RemoteStorageHelper.generateBucketName();
53+
storageControl = StorageControlClient.create();
54+
storage = StorageOptions.getDefaultInstance().getService();
55+
managedFolderId = "new-managed-folder-" + UUID.randomUUID();
56+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
57+
.setIamConfiguration(
58+
IamConfiguration
59+
.newBuilder()
60+
.setIsUniformBucketLevelAccessEnabled(true)
61+
.build()).build();
62+
bucket = storage.create(bucketInfo);
63+
storageControl.createManagedFolder(
64+
CreateManagedFolderRequest.newBuilder()
65+
// Set project to "_" to signify global bucket
66+
.setParent(BucketName.format("_", bucketName))
67+
.setManagedFolder(ManagedFolder.newBuilder().build())
68+
.setManagedFolderId(managedFolderId).build());
69+
}
70+
71+
@After
72+
public void tearDown() {
73+
storage.delete(bucketName);
74+
storageControl.shutdown();
75+
}
76+
77+
@Test
78+
public void testDeleteManagedFolder() throws Exception {
79+
DeleteManagedFolder.managedFolderDelete(bucketName, managedFolderId);
80+
String got = stdOut.getCapturedOutputAsUtf8String();
81+
assertThat(got).contains(String.format(managedFolderId));
82+
}
83+
}

0 commit comments

Comments
 (0)