指定區域端點

Speech-to-Text 提供美國和歐盟區域 API 端點。 如果您使用區域端點,靜態和使用中的資料會保留在歐洲或美國的大陸邊界內。如果必須控管資料位置,才能遵守當地法規要求,請務必指定端點。API 行為沒有任何功能變更。

使用地區端點

使用地區端點時,請務必在 parent 字串中加入相符的 useu 位置。如要進一步瞭解如何設定辨識要求主體,請參閱 RecognitionConfig 說明文件。

通訊協定

如要使用區域端點執行語音辨識,請在下表中執行適用的指令,設定正確的端點:

多區域 端點覆寫
歐盟 $ export CLOUD_SPEECH_ENDPOINT=https://eu-speech.googleapis.com
美國 $ export CLOUD_SPEECH_ENDPOINT=https://us-speech.googleapis.com

下列程式碼範例說明如何傳送 recognize request,將所有資料限制在特定區域。您可以將 EUUS 區域端點替換為 CLOUD_SPEECH_ENDPOINT 變數。

$ curl   -H "Content-Type: application/json"  \
         -H  "Authorization: Bearer "$(gcloud auth print-access-token)   \
          $CLOUD_SPEECH_ENDPOINT/v1/speech:recognize \
         --data "{
        'config': {
            'encoding': 'LINEAR16',
            'languageCode': 'en-US'
        },
        'audio': {
            'uri':'gs://speech-samples-00/commercial_mono.wav'
        }
    }"

這個範例會使用 Google Cloud CLI 為使用者帳戶產生憑證。如要瞭解如何安裝及初始化 gcloud CLI,請參閱快速入門導覽課程

要求主體中提供的音訊內容已採用 Base64 編碼。 如要進一步瞭解如何使用 Base64 編碼音訊,請參閱這篇文章。如要進一步瞭解 content 欄位,請參閱 RecognitionAudio

gcloud

下列指令會設定區域端點:

多區域 端點覆寫
歐盟 gcloud config set api_endpoint_overrides/speech https://eu-speech.googleapis.com/
美國 gcloud config set api_endpoint_overrides/speech https://us-speech.googleapis.com/

設定區域端點後,當您傳送後續的 recognize requests 時,所有資料都會限制在指定區域內。 以下範例示範如何發出辨識要求。

$ gcloud ml speech recognize gs://cloud-samples-tests/speech/brooklyn.flac \
    --language-code=en-US --log-http

Python

如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。 詳情請參閱 Speech-to-Text Python API 參考說明文件

如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。


# Imports the Google Cloud client library
from google.api_core import client_options
from google.cloud import speech


def sync_recognize_with_multi_region_gcs() -> speech.RecognizeResponse:
    """Recognizes speech synchronously in the GCS bucket."""

    # Instantiates a client

    # Pass an additional argument, ClientOptions, to specify the new endpoint.
    _client_options = client_options.ClientOptions(
        api_endpoint="eu-speech.googleapis.com"
    )

    client = speech.SpeechClient(client_options=_client_options)

    # The name of the audio file to transcribe
    gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"

    audio = speech.RecognitionAudio(uri=gcs_uri)

    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code="en-US",
    )

    # Detects speech in the audio file
    response = client.recognize(config=config, audio=audio)

    for result in response.results:
        print(f"Transcript: {result.alternatives[0].transcript}")

    return response.results

限制全球 API 端點用量

如要強制使用區域端點,請使用constraints/gcp.restrictEndpointUsage機構政策限制,封鎖對全域 API 端點的要求。詳情請參閱「限制端點用量」。