本教學課程將說明如何建立工作流程,使用 Cloud Translation API 連接器,以非同步批次模式將檔案翻譯成其他語言。這可在處理輸入內容時提供即時輸出內容。
目標
教學課程內容:
- 建立輸入 Cloud Storage 值區。
- 請以英文建立兩個檔案,然後上傳至輸入值區。
- 建立工作流程,使用 Cloud Translation API 連接器將兩個檔案翻譯成法文和西班牙文,並將結果儲存在輸出值區。
- 部署及執行工作流程,以便自動化整個流程。
費用
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
事前準備
貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Storage, Translation, and Workflows APIs:
gcloud services enable storage.googleapis.com<wbr> translate.googleapis.com<wbr> workflows.googleapis.com
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Storage, Translation, and Workflows APIs:
gcloud services enable storage.googleapis.com<wbr> translate.googleapis.com<wbr> workflows.googleapis.com
- 更新 gcloud 元件:
gcloud components update
- 使用自己的帳戶登入:
gcloud auth login
- 設定本教學課程中使用的預設位置:
gcloud config set workflows/location us-central1
由於本教學課程使用位於
us-central1
的預設 AutoML Translation 模型,因此您必須將位置設為us-central1
。如果您使用的是預設以外的 AutoML 翻譯模型或詞彙表,請務必確保該模型或詞彙表位於與連接器呼叫相同的位置,否則系統會傳回
INVALID_ARGUMENT (400)
錯誤。詳情請參閱 batchTranslateText 方法。
建立輸入 Cloud Storage 值區和檔案
您可以使用 Cloud Storage 儲存物件。物件是不可變更的資料片段,由任何格式的檔案組成,並儲存在稱為值區的容器中。
建立 Cloud Storage 值區,用於儲存要翻譯的檔案:
BUCKET_INPUT=${GOOGLE_CLOUD_PROJECT}-input-files gcloud storage buckets create gs://${BUCKET_INPUT}
建立兩個英文檔案,並上傳至輸入值區:
echo "Hello World!" > file1.txt gcloud storage cp file1.txt gs://${BUCKET_INPUT} echo "Workflows connectors simplify calling services." > file2.txt gcloud storage cp file2.txt gs://${BUCKET_INPUT}
部署及執行工作流程
工作流程由一系列步驟組成,這些步驟可使用 Workflows 語法描述,並以 YAML 或 JSON 格式編寫。這是工作流程的定義。建立工作流程後,您可以部署工作流程,讓系統執行。
建立檔案名稱為
workflow.yaml
且包含下列內容的文字檔案:工作流程會指派變數、建立輸出值區,並啟動檔案翻譯作業,將結果儲存至輸出值區。
建立工作流程後,請部署工作流程:
gcloud workflows deploy batch-translation --source=workflow.yaml
執行工作流程:
gcloud workflows execute batch-translation
如要查看工作流程狀態,您可以執行傳回的指令。例如:
gcloud workflows executions describe eb4a6239-cffa-4672-81d8-d4caef7d8424 / --workflow batch-translation / --location us-central1
工作流程應為
ACTIVE
。幾分鐘後,系統會將翻譯完成的檔案 (法文和西班牙文) 上傳至輸出值區。
列出輸出值區中的物件
您可以列出輸出值區中的物件,確認工作流程是否正常運作。
擷取輸出值區名稱:
gcloud storage ls
輸出結果會與下列內容相似:
gs://PROJECT_ID-input-files/ gs://PROJECT_ID-output-files-TIMESTAMP/
列出輸出值區中的物件:
gcloud storage ls gs://PROJECT_ID-output-files-TIMESTAMP/** --recursive
幾分鐘後,系統會列出翻譯完成的檔案,包括法文和西班牙文各兩個。
清除所用資源
如果您是為了這個教學課程建立新專案,請刪除專案。如果您使用現有的專案,且希望保留該專案而不採用本教學課程中新增的變更,請刪除為教學課程建立的資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除教學課程資源
移除在教學課程設定期間新增的 gcloud 預設設定:
gcloud config unset workflows/location
刪除在本教學課程中建立的工作流程:
gcloud workflows delete WORKFLOW_NAME
刪除在本教學課程中建立的值區及其物件:
gcloud storage rm gs://BUCKET_NAME --recursive
其中
BUCKET_NAME
是要刪除的值區名稱。例如:my-bucket
。回應如下所示:
Removing gs://my-bucket/...
後續步驟
- 如要進一步瞭解 Workflows,請參閱「瞭解 Workflows」。
- 如要進一步瞭解 Workflows 語法,請參閱 Workflows 語法參考資料。
- 如要進一步瞭解 Workflows 連接器,請參閱「瞭解連接器」。