Skip to content

Commit cd43d0d

Browse files
authored
docs(samples): remove bucket CORS configuration (googleapis#7288)
* docs(samples): remove bucket CORS configuration * docs(samples): change the place of region tag * docs(samples): addresse additional comments * feat: use clear to clear the cors list
1 parent 50b1bdc commit cd43d0d

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020 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+
package com.google.cloud.examples.storage.buckets;
17+
18+
// [START storage_remove_cors_configuration]
19+
import com.google.cloud.storage.Bucket;
20+
import com.google.cloud.storage.Cors;
21+
import com.google.cloud.storage.Storage;
22+
import com.google.cloud.storage.StorageOptions;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
public class RemoveBucketCors {
27+
public static void removeBucketCors(String projectId, String bucketName) {
28+
// The ID of your GCP project
29+
// String projectId = "your-project-id";
30+
31+
// The ID of your GCS bucket
32+
// String bucketName = "your-unique-bucket-name";
33+
34+
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
35+
Bucket bucket =
36+
storage.get(bucketName, Storage.BucketGetOption.fields(Storage.BucketField.CORS));
37+
38+
// getCors() returns the List and copying over to an ArrayList so it's mutable.
39+
List<Cors> cors = new ArrayList<>(bucket.getCors());
40+
41+
// Clear bucket CORS configuration.
42+
cors.clear();
43+
44+
// Update bucket to remove CORS.
45+
bucket.toBuilder().setCors(cors).build().update();
46+
System.out.println("Removed CORS configuration from bucket " + bucketName);
47+
}
48+
}
49+
// [END storage_remove_cors_configuration]

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.cloud.examples.storage.buckets.ListBucketIamMembers;
3939
import com.google.cloud.examples.storage.buckets.ListBuckets;
4040
import com.google.cloud.examples.storage.buckets.MakeBucketPublic;
41+
import com.google.cloud.examples.storage.buckets.RemoveBucketCors;
4142
import com.google.cloud.examples.storage.buckets.RemoveBucketDefaultKMSKey;
4243
import com.google.cloud.examples.storage.buckets.RemoveBucketIamConditionalBinding;
4344
import com.google.cloud.examples.storage.buckets.RemoveBucketIamMember;
@@ -50,6 +51,7 @@
5051
import com.google.cloud.storage.Bucket;
5152
import com.google.cloud.storage.BucketInfo;
5253
import com.google.cloud.storage.Cors;
54+
import com.google.cloud.storage.HttpMethod;
5355
import com.google.cloud.storage.Storage;
5456
import com.google.cloud.storage.StorageException;
5557
import com.google.cloud.storage.StorageRoles;
@@ -376,6 +378,31 @@ public void testConfigureBucketCors() {
376378
assertTrue(cors.getMethods().get(0).toString().equalsIgnoreCase("GET"));
377379
}
378380

381+
@Test
382+
public void testRemoveBucketCors() {
383+
storage
384+
.get(BUCKET)
385+
.toBuilder()
386+
.setCors(
387+
ImmutableList.of(
388+
Cors.newBuilder()
389+
.setOrigins(ImmutableList.of(Cors.Origin.of("http://example.appspot.com")))
390+
.setMethods(ImmutableList.of(HttpMethod.GET))
391+
.setResponseHeaders(ImmutableList.of("Content-Type"))
392+
.setMaxAgeSeconds(3600)
393+
.build()))
394+
.build()
395+
.update();
396+
Cors cors = storage.get(BUCKET).getCors().get(0);
397+
assertNotNull(cors);
398+
assertTrue(cors.getOrigins().get(0).toString().contains("example.appspot.com"));
399+
assertTrue(cors.getResponseHeaders().contains("Content-Type"));
400+
assertEquals(3600, cors.getMaxAgeSeconds().intValue());
401+
assertTrue(cors.getMethods().get(0).toString().equalsIgnoreCase("GET"));
402+
RemoveBucketCors.removeBucketCors(PROJECT_ID, BUCKET);
403+
assertNull(storage.get(BUCKET).getCors());
404+
}
405+
379406
@Test
380407
public void testRequesterPays() throws Exception {
381408
EnableRequesterPays.enableRequesterPays(PROJECT_ID, BUCKET);

0 commit comments

Comments
 (0)