|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +function main(bucketName = 'my-bucket', filterMode = 'Enabled') { |
| 18 | + // [START storage_create_bucket_ip_filtering] |
| 19 | + /** |
| 20 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 21 | + */ |
| 22 | + // The ID of your GCS bucket |
| 23 | + // const bucketName = 'your-unique-bucket-name'; |
| 24 | + |
| 25 | + // Toggle data filtering (e.g., 'Enabled' | 'Disabled') |
| 26 | + // const filterMode = 'Enabled'; |
| 27 | + |
| 28 | + // Imports the Google Cloud client library |
| 29 | + const {Storage} = require('@google-cloud/storage'); |
| 30 | + |
| 31 | + // Creates a client |
| 32 | + const storage = new Storage(); |
| 33 | + |
| 34 | + async function createBucketWithIpFilter() { |
| 35 | + const ipFilter = { |
| 36 | + mode: filterMode, |
| 37 | + publicNetworkSource: { |
| 38 | + allowedIpCidrRanges: ['8.8.8.8/32'], |
| 39 | + }, |
| 40 | + allowCrossOrgVpcs: false, |
| 41 | + allowAllServiceAgentAccess: false, |
| 42 | + }; |
| 43 | + |
| 44 | + const [bucket] = await storage.createBucket(bucketName, { |
| 45 | + ipFilter: ipFilter, |
| 46 | + }); |
| 47 | + |
| 48 | + console.log( |
| 49 | + `Bucket ${bucket.name} created with IP filter mode ${bucket.metadata.ipFilter.mode}.` |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + createBucketWithIpFilter().catch(console.error); |
| 54 | + // [END storage_create_bucket_ip_filtering] |
| 55 | +} |
| 56 | + |
| 57 | +main(...process.argv.slice(2)); |
0 commit comments