Enforce or restrict the encryption types for a bucketStay organized with collectionsSave and categorize content based on your preferences.
This document describes how to configure which encryption methods are allowed
or restricted for new objects in a Cloud Storage bucket. You can
configure a bucket to enforce or restrict the use of standard encryption
(Google default encryption), customer-managed encryption keys (CMEK), or
customer-supplied encryption keys (CSEK) for any new objects that are created
within the bucket.
For example, to help protect against ransomware attacks, you can require that
all new objects are encrypted with either standard encryption or CMEK, and
restrict the use of customer-supplied encryption keys.
For more information about the encryption methods that are available, seeData encryption options.
Cloud Storage enforces the encryption configuration for all actions
that create a new object, such as uploading an object, copying an object,
composing objects, and restoring a soft-deleted object.
Before you begin
To get the permissions that
you need to configure encryption enforcement for a bucket,
ask your administrator to grant you theStorage Admin(roles/storage.admin)
IAM role on bucket.
For more information about granting roles, seeManage access to projects, folders, and organizations.
This predefined role contains
the permissions required to configure encryption enforcement for a bucket. To see the exact permissions that are
required, expand theRequired permissionssection:
Required permissions
The following permissions are required to configure encryption enforcement for a bucket:
Set the configuration when creating a new bucket:storage.buckets.create
Update the configuration for an existing bucket:storage.buckets.update
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type. If you omit the enforcement
configuration for a specific encryption type, then that encryption type is allowed
by default.
use Google\Cloud\Storage\StorageClient;/*** Creates a bucket with specific encryption enforcement (e.g., CMEK-only).** @param string $bucketName The ID of your GCS bucket (e.g. "my-bucket").* @param string $kmsKeyName The name of the KMS key to be used as the default (e.g. "projects/my-project/...").*/function set_bucket_encryption_enforcement_config(string $bucketName, string $kmsKeyName): void{$storage = new StorageClient();$bucket = $storage->bucket($bucketName);// This configuration enforces that all objects uploaded to the bucket// must use Customer Managed Encryption Keys (CMEK).$options = ['encryption' => ['defaultKmsKeyName' => $kmsKeyName,'googleManagedEncryptionEnforcementConfig' => ['restrictionMode' => 'FullyRestricted',],'customerSuppliedEncryptionEnforcementConfig' => ['restrictionMode' => 'FullyRestricted',],'customerManagedEncryptionEnforcementConfig' => ['restrictionMode' => 'NotRestricted',],],];$storage->createBucket($bucketName, $options);printf('Bucket %s created with encryption enforcement configuration.' . PHP_EOL, $bucketName);}
fromgoogle.cloudimportstoragefromgoogle.cloud.storage.bucketimportEncryptionEnforcementConfigdefset_bucket_encryption_enforcement_config(bucket_name):"""Creates a bucket with encryption enforcement configuration."""# The ID of your GCS bucket# bucket_name = "your-unique-bucket-name"storage_client=storage.Client()bucket=storage_client.bucket(bucket_name)# Setting restriction_mode to "FullyRestricted" for Google-managed encryption (GMEK)# means objects cannot be created using the default Google-managed keys.bucket.encryption.google_managed_encryption_enforcement_config=(EncryptionEnforcementConfig(restriction_mode="FullyRestricted"))# Setting restriction_mode to "NotRestricted" for Customer-managed encryption (CMEK)# ensures that objects ARE permitted to be created using Cloud KMS keys.bucket.encryption.customer_managed_encryption_enforcement_config=(EncryptionEnforcementConfig(restriction_mode="NotRestricted"))# Setting restriction_mode to "FullyRestricted" for Customer-supplied encryption (CSEK)# prevents objects from being created using raw, client-side provided keys.bucket.encryption.customer_supplied_encryption_enforcement_config=(EncryptionEnforcementConfig(restriction_mode="FullyRestricted"))bucket.create()print(f"Created bucket{bucket.name}with Encryption Enforcement Config.")
Create a JSON file that contains the settings for the bucket. For a
complete list of settings, see theBuckets: Insertdocumentation. The following settings define only the bucket name
and encryption:
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type. If you omit the enforcement
configuration for a specific encryption type, then that encryption type is allowed
by default.
UsecURLto call the JSON API with aPOSTBucketrequest:
Create an XML file that contains the settings for the bucket. For a
complete list of settings, see theXML: Create a bucketdocumentation. The following settings define only the encryption
enforcement:
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type. If you omit the enforcement
configuration for a specific encryption type, then that encryption type is allowed
by default.
UsecURLto call the XML API with aPUTBucketrequest:
XML_FILE_NAME: the path to the XML file
that you created in the previous step.
PROJECT_ID: the ID or number of the
project for your bucket.
BUCKET_NAME: the name of the bucket.
Update the encryption types that are allowed for a bucket
To update which encryption methods are allowed for new objects in an existing
bucket, complete the following steps.
If the bucket has a default KMS key set, then you can't restrict both CMEK and
customer-supplied encryption keys because that would prevent new objects from
being created. Either allow CMEK or CSEK on such a bucket, or remove thedefault Cloud KMS keyfrom the bucket.
gcloud
Create a JSON file that contains the following information:
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type. If you omit an encryption type, the existing
configuration is retained.
usingGoogle.Apis.Storage.v1.Data;usingGoogle.Cloud.Storage.V1;usingSystem;publicclassBucketUpdateEncryptionEnforcementConfigSample{/// <summary>/// Updates the encryption enforcement configuration of the bucket./// </summary>/// <param name="bucketName">The name of the bucket.</param>/// <param name="encryptionData">The encryption configuration for the bucket.</param>publicBucket.EncryptionDataBucketUpdateEncryptionEnforcementConfig(stringbucketName="your-unique-bucket-name",Bucket.EncryptionDataencryptionData=null){varstorage=StorageClient.Create();varbucket=storage.GetBucket(bucketName);if(bucket.Encryptionisnull||(bucket.Encryption.CustomerManagedEncryptionEnforcementConfigisnull&&bucket.Encryption.CustomerSuppliedEncryptionEnforcementConfigisnull&&bucket.Encryption.GoogleManagedEncryptionEnforcementConfigisnull)){Console.WriteLine($"No Encryption Enforcement Configuration found for bucket {bucketName}");returnbucket.Encryption;}bucket.Encryption=encryptionData;bucket=storage.UpdateBucket(bucket);Console.WriteLine($"The Encryption Enforcement Configuration has been updated for the bucket {bucketName}");returnbucket.Encryption;}}
use Google\Cloud\Storage\StorageClient;/*** Updates or removes encryption enforcement configurations from a bucket.** @param string $bucketName The ID of your GCS bucket (e.g. "my-bucket").*/function update_bucket_encryption_enforcement_config(string $bucketName): void{$storage = new StorageClient();$bucket = $storage->bucket($bucketName);// Update a specific encryption type's restriction mode// This partial update preserves other existing encryption settings.$updateOptions = ['encryption' => ['googleManagedEncryptionEnforcementConfig' => ['restrictionMode' => 'FullyRestricted']]];$bucket->update($updateOptions);printf('Google-managed encryption enforcement set to FullyRestricted for %s.' . PHP_EOL, $bucketName);// Remove all encryption enforcement configurations altogether// Setting these values to null removes the policies from the bucket metadata.$clearOptions = ['encryption' => ['defaultKmsKeyName' => null,'googleManagedEncryptionEnforcementConfig' => null,'customerSuppliedEncryptionEnforcementConfig' => null,'customerManagedEncryptionEnforcementConfig' => null,],];$bucket->update($clearOptions);printf('All encryption enforcement configurations removed from bucket %s.' . PHP_EOL, $bucketName);}
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type. If you omit an encryption type, the existing
configuration is retained.
UsecURLto call the JSON API with aPATCHBucketrequest:
STANDARD_ENCRYPTION_RESTRICTION_MODE:
Whether encryption using standard encryption (Google default encryption) is allowed
when creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use standard encryption.
FullyRestricted: new objects can't use standard encryption.
CMEK_RESTRICTION_MODE:
Whether encryption using CMEKs is allowed when creating objects in this bucket. The
following values are supported:
NotRestricted: new objects can use CMEKs.
FullyRestricted: new objects can't use CMEKs.
CSEK_RESTRICTION_MODE:
Whether encryption using customer-supplied encryption keys is allowed when
creating objects in this bucket. The following values are supported:
NotRestricted: new objects can use customer-supplied encryption keys.
FullyRestricted: new objects can't use customer-supplied encryption keys.
You must allow at least one encryption type.
UsecURLto call the XML API with aPUTBucketrequest
scoped to?encryptionConfig:
[[["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 2026-04-17 UTC."],[],[]]