Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_create_bucket_ip_filtering]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.BucketInfo.IpFilter.PublicNetworkSource;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.Arrays;

public class CreateBucketIpFilter {
public static void createBucketIpFilter(
String projectId, String bucketName, String publicCidrRange) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The public IPv4 CIDR range to allow
// String publicCidrRange = "192.0.2.0/24";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

IpFilter ipFilter =
IpFilter.newBuilder()
.setMode("Disabled")
.setAllowAllServiceAgentAccess(true)
.setPublicNetworkSource(PublicNetworkSource.of(Arrays.asList(publicCidrRange)))
.build();

BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setIpFilter(ipFilter).build();

Bucket bucket = storage.create(bucketInfo);

System.out.println(
"Created bucket " + bucketName + " with IP filter mode " + bucket.getIpFilter().getMode());
}
}
// [END storage_create_bucket_ip_filtering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_delete_ip_filtering_rules]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.BucketInfo.IpFilter.PublicNetworkSource;
import com.google.cloud.storage.BucketInfo.IpFilter.VpcNetworkSource;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.ArrayList;
import java.util.List;

public class DeleteBucketIpFilter {
public static Bucket deleteBucketIpFilterRules(
String projectId, String bucketName, String publicRange, String vpcNetwork) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The public IPv4/IPv6 CIDR range to remove
// String publicRange = "192.0.2.0/24";

// The VPC network name to remove
// String vpcNetwork = "projects/my-project/global/networks/my-vpc";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
IpFilter filter = bucket.getIpFilter();

if (filter == null) {
System.out.println("Bucket " + bucketName + " has no IP Filter configured.");
return bucket;
}

IpFilter.Builder builder = filter.toBuilder();
if (publicRange != null && filter.getPublicNetworkSource() != null) {
List<String> ranges =
new ArrayList<>(filter.getPublicNetworkSource().getAllowedIpCidrRanges());
ranges.remove(publicRange);
builder.setPublicNetworkSource(ranges.isEmpty() ? null : PublicNetworkSource.of(ranges));
}

if (vpcNetwork != null && filter.getVpcNetworkSources() != null) {
List<VpcNetworkSource> vpcs = new ArrayList<>(filter.getVpcNetworkSources());
vpcs.removeIf(source -> vpcNetwork.equals(source.getNetwork()));
builder.setVpcNetworkSources(vpcs.isEmpty() ? null : vpcs);
}

// Note: This sample selectively removes specific rules (a public CIDR range or VPC network)
// from an existing IP filter configuration. To completely clear the IP filter configuration
// from the bucket, use: storage.update(bucket.toBuilder().setIpFilter(null).build());
Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(builder.build()).build());
Comment thread
nidhiii-27 marked this conversation as resolved.
System.out.println("Deleted specified IP filtering rules for bucket " + bucketName);
return updatedBucket;
}
}
// [END storage_delete_ip_filtering_rules]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_disable_ip_filtering]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class DisableBucketIpFilter {
public static Bucket disableBucketIpFilter(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);

if (bucket.getIpFilter() == null) {
System.out.println("Bucket " + bucketName + " has no IP Filter configured.");
return bucket;
}

IpFilter disabledIpFilter = bucket.getIpFilter().toBuilder().setMode("Disabled").build();
Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(disabledIpFilter).build());

System.out.println("IP filtering disabled for bucket " + bucketName);
return updatedBucket;
}
}
// [END storage_disable_ip_filtering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_enable_ip_filtering]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class EnableBucketIpFilter {
public static Bucket enableBucketIpFilter(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);

if (bucket.getIpFilter() == null) {
System.out.println("Bucket " + bucketName + " has no IP Filter configured.");
return bucket;
}

IpFilter enabledIpFilter = bucket.getIpFilter().toBuilder().setMode("Enabled").build();
Bucket updatedBucket = storage.update(bucket.toBuilder().setIpFilter(enabledIpFilter).build());

System.out.println("IP filtering enabled for bucket " + bucketName);
return updatedBucket;
}
}
// [END storage_enable_ip_filtering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_get_ip_filtering]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class GetBucketIpFilter {
public static IpFilter getBucketIpFilter(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
IpFilter ipFilter = bucket.getIpFilter();

if (ipFilter == null) {
System.out.println("Bucket " + bucketName + " has no IP Filter configured.");
return null;
}

System.out.println("Bucket " + bucketName + " IP filter configuration: " + ipFilter);
return ipFilter;
}
}
// [END storage_get_ip_filtering]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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
*
* http://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.
*/

package com.example.storage.bucket;

// [START storage_list_buckets_ip_filtering]
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.IpFilter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class ListBucketsIpFilter {
public static void listBucketsIpFilter(String projectId) {
// The ID of your GCP project
// String projectId = "your-project-id";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Page<Bucket> buckets = storage.list();

for (Bucket bucket : buckets.iterateAll()) {
IpFilter ipFilter = bucket.getIpFilter();
String status =
(ipFilter == null || ipFilter.getMode() == null) ? "Not Configured" : ipFilter.getMode();
System.out.println("Bucket: " + bucket.getName() + ", IP Filter Mode: " + status);
}
}
}
// [END storage_list_buckets_ip_filtering]
Loading
Loading