长时间运行的操作(高级版)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
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"
}
}
操作完成后,系统会返回 state
值 SUCCEEDED
。
操作取消
下面介绍了如何取消操作。
REST
对 operation
资源调用 cancel
方法。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_NUMBER_OR_ID:您的 Google Cloud 项目的数字或字母数字 ID。
- 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) 和一个空响应。
其他资源
- 如需有关解决常见问题或错误的帮助,请参阅问题排查页面。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-01-31。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-01-31。"],[],[]]