@@ -140,7 +140,15 @@ void UpdateBucketEncryptionEnforcementConfig(
140140 namespace gcs = ::google::cloud::storage;
141141 using ::google::cloud::StatusOr;
142142 [](gcs::Client client, std::string const & bucket_name) {
143+ StatusOr<gcs::BucketMetadata> original =
144+ client.GetBucketMetadata (bucket_name);
145+
146+ gcs::BucketMetadata updated_metadata = *original;
143147 gcs::BucketEncryption encryption;
148+ if (original->has_encryption ()) {
149+ encryption = original->encryption ();
150+ }
151+
144152 // 1. Update a specific type (e.g., change GMEK to FullyRestricted)
145153 encryption.google_managed_encryption_enforcement_config .restriction_mode =
146154 " FullyRestricted" ;
@@ -152,9 +160,10 @@ void UpdateBucketEncryptionEnforcementConfig(
152160 encryption.customer_supplied_encryption_enforcement_config
153161 .restriction_mode = " FullyRestricted" ;
154162
155- StatusOr<gcs::BucketMetadata> updated = client.PatchBucket (
156- bucket_name,
157- gcs::BucketMetadataPatchBuilder ().SetEncryption (encryption));
163+ updated_metadata.set_encryption (encryption);
164+
165+ StatusOr<gcs::BucketMetadata> updated =
166+ client.PatchBucket (bucket_name, *original, updated_metadata);
158167 if (!updated) throw std::move (updated).status ();
159168
160169 std::cout << " Encryption enforcement policy updated for bucket "
@@ -183,15 +192,6 @@ void RunAll(std::vector<std::string> const& argv) {
183192
184193 auto constexpr kBucketPeriod = std::chrono::seconds (2 );
185194
186- // Clean up any potentially leaked buckets from a previous run before creating
187- // them.
188- (void )examples::RemoveBucketAndContents (client, " g-" + bucket_name);
189- if (!examples::UsingEmulator ()) std::this_thread::sleep_for (kBucketPeriod );
190- (void )examples::RemoveBucketAndContents (client, " c-" + bucket_name);
191- if (!examples::UsingEmulator ()) std::this_thread::sleep_for (kBucketPeriod );
192- (void )examples::RemoveBucketAndContents (client, " rc-" + bucket_name);
193- if (!examples::UsingEmulator ()) std::this_thread::sleep_for (kBucketPeriod );
194-
195195 std::cout << " \n Running the SetBucketEncryptionEnforcementConfig() example"
196196 << std::endl;
197197 SetBucketEncryptionEnforcementConfig (client, {project_id, bucket_name});
0 commit comments