Discover
Get started
Create buckets
Access and manage buckets
Upload and download objects
Access and manage objects
Get insights on your stored data
Cache objects
Control data lifecycles
Make requests
Secure data
Monitor data and usage
Protection, backup, and recovery
Mount buckets with Cloud Storage FUSE
Work across products, Clouds, and platforms
Troubleshoot
Stay organized with collections Save and categorize content based on your preferences.
Organization Policy provides predefined constraints for Cloud Storage. However, if you want more granular, customizable control over the specific fields that are restricted in your organization policies, you can also create custom constraints and use those custom constraints in an organization policy.
This page describes how to set custom constraints to enforce policies on Cloud Storage resources.
To test a new constraint before it is enforced on your production environment, use the Policy Simulator.
Policy inheritanceBy default, organization policies are inherited by the descendants of the resources on which you enforce the policy. For example, if you enforce a policy on an organization, Google Cloud enforces the policy on all projects in the organization. To learn more about this behavior and how to change it, refer to Hierarchy evaluation rules.
PricingThe Organization Policy Service, including predefined and custom constraints, is offered at no charge.
LimitationsCustom constraints for Cloud Storage resources can only be set up by using the Google Cloud console or Google Cloud CLI.
Custom constraints can only be enforced on the CREATE
or UPDATE
methods for Cloud Storage resources.
Newly enforced custom constraints don't apply automatically to existing resources. Existing resources must be updated for the constraint to apply.
To find existing resources that will need to be updated, you can enforce a dry-run organization policy.
Custom constraints cannot be used to constrain ACLs or IAM policies on objects or buckets.
For Cloud Storage, you can set custom constraints on the following resource:
storage.googleapis.com/Bucket
For information about the roles required to manage organization policies with custom constraints, see Required roles.
In addition to managing organization policies, you might want to test the custom constraints you create. To test custom constraints, it's recommended that you use the least permissive predefined or custom role that contains the permissions required to test the specific constraint. To see which permissions and roles are required, refer to roles and permissions for Cloud Storage.
Set up a custom constraint ConsoleIn the Google Cloud console, go to the Organization policies page.
Select the Project picker at the top of the page.
From the Project picker, select the resource for which you want to set the organization policy.
Click add Custom constraint.
In the Display name field, enter a human-friendly name for the constraint. This field has a maximum length of 200 characters. Don't use PII or sensitive data in constraint names, because they could be exposed in error messages.
In the Constraint ID box, enter the name you want for your new custom constraint. A custom constraint must start with custom.
, and can only include uppercase letters, lowercase letters, or numbers, for example, custom.enforceBucketVersioning
. The maximum length of this field is 70 characters, not counting the prefix, for example, organizations/123456789/customConstraints/custom.
.
In the Description field, enter a human-friendly description of the constraint to display as an error message when the policy is violated. This field has a maximum length of 2000 characters.
In the Resource type field, select the name of the Google Cloud REST resource containing the object and field you want to restrict. For example, storage.googleapis.com/Bucket
.
Under Enforcement method, select whether to enforce the constraint on the REST CREATE
or UPDATE
method.
To define a condition, click edit Edit condition.
In the Add condition panel, create a CEL condition that refers to a supported service resource, for example resource.versioning.enabled == true
. This field has a maximum length of 1000 characters.
Click Save.
Under Action, select whether to allow or deny the evaluated method if the condition is met.
Click Create constraint.
When you have entered a value into each field, the equivalent YAML configuration for this custom constraint appears on the right.
gcloudTo create a custom constraint using the Google Cloud CLI, create a YAML file for the custom constraint:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- RESOURCE_NAME
methodTypes:
- METHOD1
- METHOD2
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
Replace the following:
ORGANIZATION_ID
: your organization ID, such as 123456789
.
CONSTRAINT_NAME
: the name you want for your new custom constraint. A custom constraint must start with custom.
, and can only include uppercase letters, lowercase letters, or numbers, for example, custom.enforceBucketVersioning
. The maximum length of this field is 70 characters, not counting the prefix, for example, organizations/123456789/customConstraints/custom.
.
RESOURCE_NAME
: the fully qualified name of the Google Cloud REST resource containing the object and field you want to restrict. For example, storage.googleapis.com/Bucket
.
METHOD1,METHOD2
: a list of RESTful methods for which to enforce the constraint. Can be CREATE
or CREATE
and UPDATE
.
CONDITION
: a CEL condition that refers to a supported service resource, for example "resource.versioning.enabled == true"
. This field has a maximum length of 1000 characters. For details about CEL usage, see Common Expression Language.
ACTION
: the action to take if the condition
is met. This can be either ALLOW
or DENY
.
DISPLAY_NAME
: a human-friendly name for the constraint. This field has a maximum length of 200 characters.
DESCRIPTION
: a human-friendly description of the constraint to display as an error message when the policy is violated. This field has a maximum length of 2000 characters.
After you have created the YAML file for a new custom constraint, you must set it up to make it available for organization policies in your organization. To set up a custom constraint, use the gcloud org-policies set-custom-constraint
command:
gcloud org-policies set-custom-constraint CONSTRAINT_PATHReplace
CONSTRAINT_PATH
with the full path to your custom constraint file. For example, /home/user/customconstraint.yaml
. Once completed, your custom constraints are available as organization policies in your list of Google Cloud organization policies. To verify that the custom constraint exists, use the gcloud org-policies list-custom-constraints
command:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_IDReplace
ORGANIZATION_ID
with the ID of your organization resource. For more information, see Viewing organization policies.
If the request is successful, the output is similar to the following:
CUSTOM_CONSTRAINT ACTION_TYPE METHOD_TYPES RESOURCE_TYPES DISPLAY_NAME
custom.uniformBucketLevelAccess
DENY CREATE,UPDATE storage.googleapis.com/Bucket Enable object versioning
For more information on setting up and managing custom constraints, see Create and manage custom constraints.
Enforce a constraintYou can enforce a constraint by creating an organization policy that references it, and then applying that organization policy to a Google Cloud resource.
ConsoleTo create an organization policy with boolean rules, create a policy YAML file that references the constraint:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
Replace the following:
PROJECT_ID
: the project on which you want to enforce your constraint.CONSTRAINT_NAME
: the name you defined for your custom constraint. For example, custom.uniformBucketLevelAccess
.To enforce the organization policy containing the constraint, run the following command:
gcloud org-policies set-policy POLICY_PATH
Replace POLICY_PATH
with the full path to your organization policy YAML file. The policy requires up to 15 minutes to take effect.
Requests that get denied for violating a custom constraint fail with a 412
error: CUSTOM_ORGPOLICY_CONSTRAINT_FAILED
.
Create an enforceCMEK.yaml
constraint file with the following information:
name: organizations/ORGANIZATION_ID/customConstraints/custom.customerManagedEncryptionKeys resource_types: storage.googleapis.com/Bucket method_types: – CREATE – UPDATE condition: "has(resource.encryption.defaultKmsKeyName)" action_type: ALLOW display_name: Enforce Cloud KMS key description: When this constraint is enforced, newly created buckets and newly updated buckets must be encrypted with a Cloud KMS key. The Cloud KMS key on existing buckets can be updated but not deleted.
Set the custom constraint.
gcloud org-policies set-custom-constraint enforceCMEK.yaml
Create a enforceCMEK-policy.yaml
policy file with the following information.
name: projects/PROJECT_ID/policies/custom.customerManagedEncryptionKeys spec: rules: – enforce: true
Replace PROJECT_ID
with your project ID.
In this example, we enforce this constraint at the project level, but you might also set this at the organization or folder level.
Enforce the policy.
gcloud org-policies set-policy enforceCMEK-policy.yaml
The following sections provide the syntax of some custom constraints that you might find useful:
Use case Syntax Bucket retention policies must have a period that falls within the specified durationsname: organizations/ORGANIZATION_ID/customConstraints/custom.retentionPolicy method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "resource.retentionPolicy.retentionPeriod not in [3600, 2678400]" action_type: DENY display_name: Bucket retention policy is either 3,600 seconds or 2,678,400 seconds description: Newly created buckets and newly updated buckets must have a retention policy that's either 3,600 seconds or 2,678,400 seconds.Buckets must have Object Versioning enabled
name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioning method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "resource.versioning.enabled == true" action_type: ALLOW display_name: Buckets must have Object Versioning enabled description: Newly created buckets and newly updated buckets must have Object Versioning enabled.Buckets must be named using a specific regular expression
name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketName method_types: – CREATE resource_types: storage.googleapis.com/Bucket condition: "resource.name.matches('^[a-zA-Z]+$')" action_type: ALLOW display_name: Bucket names must match the specified regular expression description: Newly created buckets must have a name that matches the specified regular expression. Only letters are allowed in the bucket name.Buckets cannot have Bucket Lock enabled
name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLock method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "resource.retentionPolicy.isLocked == true" action_type: DENY display_name: Prohibit the use of Bucket Lock description: Newly created buckets and newly updated buckets cannot have Bucket Lock enabled.Buckets cannot have Object Retention Lock enabled
name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLock method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "resource.objectRetention.mode == 'Enabled'" action_type: DENY display_name: Objects cannot have retention configurations description: Newly created buckets and newly updated buckets cannot have Object Retention Lock enabled.Buckets located in the
US
or EU
multi-regions must have a retention period of 86,400 seconds
name: organizations/ORGANIZATION_ID/customConstraints/custom.locationRetentionPolicy method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "(resource.location.startsWith('US') || resource.location.startsWith('EU')) && resource.retentionPolicy.retentionPeriod != 86400" action_type: DENY display_name: All buckets in US and EU must have a retention policy of 86,400 seconds description: Newly created buckets and newly updated buckets located in US and EU regions must have a retention policy of 86,400 seconds.Buckets must have labels1
name: organizations/ORGANIZATION_ID/customConstraints/custom.labels method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "'my_annotations.data.source' in resource.labels && resource.labels['my_annotations.data.source'] in ['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']" action_type: ALLOW display_name: Buckets must have a label classifying the contents of the bucket description: Newly created buckets and newly updated buckets must have the label my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, or SOURCE_VIDEOS key.Buckets must be located in a dual-region
name: organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUS method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "'US-EAST1' in resource.customPlacementConfig.dataLocations && 'US-EAST4' in resource.customPlacementConfig.dataLocations" action_type: ALLOW display_name: Buckets must be located in a dual-region description: Newly created buckets and newly updated buckets must be located in a dual-region composed of the us-east1 and us-east4 regions.Buckets cannot use legacy storage classes
name: organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClass method_types: – CREATE – UPDATE resource_types: storage.googleapis.com/Bucket condition: "resource.storageClass in ['STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE']" action_type: ALLOW display_name: Buckets cannot use legacy storage classes description: Newly created buckets and newly updated buckets must use Standard storage, Nearline storage, Coldline storage, or Archive storage.Bucket IP filtering must restrict requests from all the public internet
name: organizations/ORGANIZATION_ID/customConstraints/custom.IpFilter method_types: – CREATE resource_types: storage.googleapis.com/Bucket condition: "!has(resource.ipFilter) || (resource.ipFilter.mode == 'Disabled' || resource.ipFilter.publicNetworkSource.allowedIpCidrRanges.size() > 0)" action_type: DENY display_name: Bucket IP filter rules must restrict all the public network description: Newly created buckets must have IP filtering and IP filtering rules must restrict all public network resources.
1 Specifying a bucket label key that does not exist returns a BAD_CONDITION
error. This error blocks the CREATE
and UPDATE
methods from being run on the bucket. To avoid this error, always check that the label key exists first using "my_annotations.data.source" in resource.labels
.
The following table contains the expression fields you can use to create conditions. Conditions are written in Common Expression Language (CEL). Note that the value of these expression fields are case sensitive.
For descriptions of the following expression fields and what values you can specify, refer to the Buckets resource representation for the JSON API.
Expression field Value typebilling.requesterPays
bool
cors
list
cors.maxAgeSeconds
int
cors.method
list
cors.origin
list
cors.responseHeader
list
customPlacementConfig.dataLocations
1 list
defaultEventBasedHold
bool
encryption.defaultKmsKeyName
string
iamConfiguration.publicAccessPrevention
string
iamConfiguration.uniformBucketLevelAccess.enabled
bool
labels
map
lifecycle.rule
list
lifecycle.rule.action.storageClass
1 string
lifecycle.rule.action.type
string
lifecycle.rule.condition.age
int
lifecycle.rule.condition.createdBefore
string
lifecycle.rule.condition.customTimeBefore
string
lifecycle.rule.condition.daysSinceCustomTime
int
lifecycle.rule.condition.daysSinceNoncurrentTime
int
lifecycle.rule.condition.isLive
bool
lifecycle.rule.condition.matchesPrefix
list
lifecycle.rule.condition.matchesStorageClass
list
lifecycle.rule.condition.matchesSuffix
list
lifecycle.rule.condition.noncurrentTimeBefore
string
lifecycle.rule.condition.numNewerVersions
int
location
1 string
locationType
string
logging.logBucket
string
logging.logObjectPrefix
string
ipFilter.mode
string
ipFilter.publicNetworkSource
object
ipFilter.publicNetworkSource.allowedIpCidrRanges
list
ipFilter.vpcNetworkSources
list
ipFilter.vpcNetworkSources.network
string
ipFilter.vpcNetworkSources.allowedIpCidrRanges
list
objectRetention.mode
string
name
string
projectNumber
2 string
retentionPolicy.isLocked
3 bool
retentionPolicy.retentionPeriod
int
rpo
string
softDeletePolicy.retentionDurationSeconds
int
storageClass
1 string
versioning.enabled
bool
website.mainPageSuffix
string
website.notFoundPage
string
1 The value to this field must be written in uppercase.
2 This field is deprecated.
3 This field can only be used to prohibit the use of Bucket Lock, not enforce Bucket Lock.
ConsiderationsBucket labels aren't recommended for usage in custom constraint conditions. Instead, use tags, which can only be set by individuals with required IAM roles and are more tightly controlled than labels.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-10-03 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.5