|
| 1 | +# Copyright 2022 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 | +# https://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 | + |
| 16 | +def create_table_cmek(table_id: str, kms_key_name: str) -> None: |
| 17 | + orig_table_id = table_id |
| 18 | + orig_key_name = kms_key_name |
| 19 | + # [START bigquery_create_table_cmek] |
| 20 | + from google.cloud import bigquery |
| 21 | + |
| 22 | + client = bigquery.Client() |
| 23 | + |
| 24 | + # TODO(dev): Change table_id to the full name of the table you want to create. |
| 25 | + table_id = "your-project.your_dataset.your_table_name" |
| 26 | + |
| 27 | + # Set the encryption key to use for the table. |
| 28 | + # TODO: Replace this key with a key you have created in Cloud KMS. |
| 29 | + kms_key_name = "projects/your-project/locations/us/keyRings/test/cryptoKeys/test" |
| 30 | + |
| 31 | + # [END bigquery_create_table_cmek] |
| 32 | + |
| 33 | + table_id = orig_table_id |
| 34 | + kms_key_name = orig_key_name |
| 35 | + |
| 36 | + # [START bigquery_create_table_cmek] |
| 37 | + table = bigquery.Table(table_id) |
| 38 | + table.encryption_configuration = bigquery.EncryptionConfiguration( |
| 39 | + kms_key_name=kms_key_name |
| 40 | + ) |
| 41 | + table = client.create_table(table) # API request |
| 42 | + |
| 43 | + print(f"Created {table_id}.") |
| 44 | + print(f"Key: {table.encryption_configuration.kms_key_name}.") |
| 45 | + |
| 46 | + # [END bigquery_create_table_cmek] |
0 commit comments