Cloud Translation API の一部のメソッドは長時間実行オペレーションを返します。これらのメソッドは非同期で、メソッドがレスポンスを返すときにオペレーションが完了していない場合があります。オペレーションのステータスを確認し、キャンセルすることもできます。
オペレーションのステータス
以下に、オペレーションのステータスをポーリングする方法を示します。
operation
リソースの get
メソッドを呼び出します。
リクエストのデータを使用する前に、次のように置き換えます。
- PROJECT_NUMBER_OR_ID: Google Cloud プロジェクトの数字または英数字の ID
- location-id: Cloud Storage バケット用に選択したロケーション
- operation-id: 上記で受け取ったオペレーション ID
HTTP メソッドと URL:
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
の値が返されます。
オペレーションのキャンセル
以下では、オペレーションをキャンセルする方法を示します。
operation
リソースで cancel
メソッドを呼び出します。
リクエストのデータを使用する前に、次のように置き換えます。
- PROJECT_NUMBER_OR_ID: Google Cloud プロジェクトの数字または英数字の ID。
- LOCATION: オペレーションの開始時に選択したロケーション。
- OPERATION_ID: キャンセルするオペレーションの ID。
HTTP メソッドと URL:
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)と空のレスポンスが返されます。
参考情報
- 一般的な問題またはエラーの解決に関するヘルプについては、トラブルシューティングページをご覧ください。