加密 Speech-to-Text 資源

本頁說明如何在 Speech-to-Text 中設定加密金鑰,加密 Speech-to-Text 資源。

Speech-to-Text 可讓您提供 Cloud Key Management Service 加密金鑰,並使用提供的金鑰加密資料。如要進一步瞭解加密功能,請參閱加密頁面。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Speech-to-Text APIs.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「身分與存取權管理」頁面
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. Install the Google Cloud CLI.

  7. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  8. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. Enable the Speech-to-Text APIs.

    Enable the APIs

  12. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「身分與存取權管理」頁面
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  13. Install the Google Cloud CLI.

  14. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  15. 如要初始化 gcloud CLI,請執行下列指令:

    gcloud init
  16. 用戶端程式庫可以使用應用程式預設憑證,輕鬆向 Google API 進行驗證,然後傳送要求給這些 API。使用應用程式預設憑證,您可以在本機測試及部署應用程式,不必變更基礎程式碼。詳情請參閱「 驗證以使用用戶端程式庫」。

  17. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  18. 此外,請確認您已安裝用戶端程式庫

    啟用 Cloud Key Management Service 金鑰的存取權

    Speech-to-Text 會使用服務帳戶存取 Cloud KMS 金鑰。依預設,服務帳戶無法存取 Cloud KMS 金鑰。

    服務帳戶電子郵件地址如下:

    service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com
    

    如要使用 Cloud KMS 金鑰加密 Speech-to-Text 資源,請為這個服務帳戶指派 roles/cloudkms.cryptoKeyEncrypterDecrypter 角色:

    gcloud projects add-iam-policy-binding PROJECT_NUMBER \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
        --role=roles/cloudkms.cryptoKeyEncrypterDecrypter

    如要進一步瞭解專案 IAM 政策,請參閱「管理專案、資料夾和機構的存取權」一文。

    如要進一步瞭解如何管理 Cloud Storage 存取權,請參閱 Cloud Storage 說明文件中的「建立及管理存取權控管清單」。

    指定加密金鑰

    以下範例說明如何使用 Config 資源,將加密金鑰提供給 Speech-to-Text:

    Python

    import os
    
    from google.cloud.speech_v2 import SpeechClient
    from google.cloud.speech_v2.types import cloud_speech
    
    PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
    
    
    def enable_cmek(
        kms_key_name: str,
    ) -> cloud_speech.Config:
        """Enable Customer-Managed Encryption Keys (CMEK) in a project and region.
        Args:
            kms_key_name (str): The full resource name of the KMS key to be used for encryption.
                E.g,: projects/{PROJECT_ID}/locations/{LOCATION}/keyRings/{KEY_RING}/cryptoKeys/{KEY_NAME}
        Returns:
            cloud_speech.Config: The response from the update configuration request,
            containing the updated configuration details.
        """
        # Instantiates a client
        client = SpeechClient()
    
        request = cloud_speech.UpdateConfigRequest(
            config=cloud_speech.Config(
                name=f"projects/{PROJECT_ID}/locations/global/config",
                kms_key_name=kms_key_name,
            ),
            update_mask={"paths": ["kms_key_name"]},
        )
    
        # Updates the KMS key for the project and region.
        response = client.update_config(request=request)
    
        print(f"Updated KMS key: {response.kms_key_name}")
        return response
    
    

    在專案的 [Config] 資源中指定加密金鑰後,在相應位置建立的任何新資源都會使用這個金鑰加密。如要進一步瞭解加密內容和時間,請參閱加密頁面。

    加密資源的 kms_key_namekms_key_version_name 欄位會填入 Speech-to-Text API 回應。

    移除加密措施

    如要避免日後資源使用加密金鑰加密,請使用上述程式碼,並在要求中提供空字串 ("") 做為金鑰。這樣可確保新資源不會加密。這項指令不會解密現有資源。

    金鑰輪替和刪除

    金鑰輪替後,以舊版 Cloud KMS 金鑰加密的資源仍會以該版本加密。金鑰輪替後建立的任何資源,都會使用新的預設金鑰版本加密。金鑰輪替後,使用 Update* 方法更新的任何資源,都會以新的預設金鑰版本重新加密。

    刪除金鑰後,語音轉文字服務就無法解密資料,也無法建立資源,或存取以已刪除金鑰加密的資源。同樣地,如果撤銷金鑰的語音轉文字權限,語音轉文字就無法解密您的資料,也無法建立或存取以撤銷語音轉文字權限的金鑰加密的資源。

    重新加密資料

    如要重新加密資源,請在更新 Config 資源中的金鑰規格後,為每個資源呼叫對應的 Update* 方法。

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

    1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

      gcloud auth application-default revoke
    2. Optional: Revoke credentials from the gcloud CLI.

      gcloud auth revoke

    控制台

  19. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  20. In the project list, select the project that you want to delete, and then click Delete.
  21. In the dialog, type the project ID, and then click Shut down to delete the project.
  22. gcloud

  23. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  24. In the project list, select the project that you want to delete, and then click Delete.
  25. In the dialog, type the project ID, and then click Shut down to delete the project.
  26. 後續步驟