長時間執行的作業 (進階)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Cloud Translation API 的部分方法會傳回長時間執行的作業。這些方法是以非同步的方式執行,因此在方法傳回回應時,可能尚未完成作業。您可以檢查作業的狀態或取消作業。
作業狀態
以下說明如何輪詢作業的狀態。
REST
呼叫 operation
資源的 get
方法。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID: Google Cloud 專案的數值或英數字元 ID
- location-id:您為 Cloud Storage 值區選擇的位置
- operation-id:您在先前步驟收到的作業 ID
HTTP 方法和網址:
GET https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/location-id/operations/operation-id
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_NUMBER_OR_ID" \
"https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/location-id/operations/operation-id"
PowerShell (Windows)
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER_OR_ID" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/location-id/operations/operation-id" | Select-Object -Expand Content
您應該會收到如下的 JSON 回應:
{
"name": "projects/project-number/locations/location-id/operations/operation-id",
"metadata": {
"@type": "type.googleapis.com/google.cloud.translation.v3.SomeOperationMetadata",
"state": "SUCCEEDED",
"submitTime": "2019-11-27T22:59:40Z"
}
}
作業完成後,會傳回 SUCCEEDED
的 state
值。
取消作業
以下說明如何取消作業。
REST
呼叫 operation
資源的 cancel
方法。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_NUMBER_OR_ID:專案的數值或英數字元 ID。 Google Cloud
- LOCATION:您在開始作業時選擇的位置。
- OPERATION_ID:要取消的作業 ID。
HTTP 方法和網址:
POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION_ID/operations/OPERATION_ID:cancel
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "x-goog-user-project: PROJECT_NUMBER_OR_ID" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION_ID/operations/OPERATION_ID:cancel"
PowerShell (Windows)
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred"; "x-goog-user-project" = "PROJECT_NUMBER_OR_ID" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID/locations/LOCATION_ID/operations/OPERATION_ID:cancel" | Select-Object -Expand Content
您應該會收到執行成功的狀態碼 (2xx) 和空白回應。
其他資源
- 如需解決常見問題或錯誤的說明,請參閱「疑難排解」頁面。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-09 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-09 (世界標準時間)。"],[],[]]