GCP API - machineImages.insert returns 503

GCP API - machineImages.insert returns 503

在我的应用程序中,我从机器创建图像以供以后重用和创建实例。

我有以下 JAVA 代码来创建图像:

public static String createGCPImage(String imageName, String projectId, String machineName) throws Exception {
        String machineNameUrl = "projects/" + projectId + "/global/instances/" + machineName;
        JsonObject imageRequest = new JsonObject();
        imageRequest.addProperty("name", imageName);
        imageRequest.addProperty("sourceInstance", machineNameUrl);
        String url = "https://compute.googleapis.com/compute/beta/projects/" + projectId + "/global/machineImages";
        
        ProjectOperation po = new ProjectOperation();
        po.setProjectId(order.getProject()).setServiceAccount();
        String serviceAccount = po.getServiceAccount();
        InputStream credentialStream = new ByteArrayInputStream(serviceAccount.getBytes());
        GoogleCredentials credentials = GoogleCredentials.fromStream(credentialStream);
        List<String> COMPUTE_SCOPES = Collections.singletonList(ComputeScopes.COMPUTE);
        if (credentials.createScopedRequired()) {
            credentials = credentials.createScoped(COMPUTE_SCOPES);
        }
        
        Gson gson = new Gson();
        String requestBody = gson.toJson(imageRequest);
        final HttpContent content = new ByteArrayContent("application/json", requestBody.getBytes("UTF8") );
        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
        HttpRequest request = factory.buildPostRequest(new GenericUrl(url), content);
        HttpResponse httpResponse = request.execute();

        return httpResponse.getStatusMessage();
    }


此请求 returns 以下响应:

Error: 503 Service Unavailable
POST https://compute.googleapis.com/compute/beta/projects/devopsucp/global/machineImages
{
  "error": {
    "code": 503,
    "message": "Internal error. Please try again or contact Google Support. (Code: '5B7AFB2001A53.AD5AC0C.4B01291B')",
    "errors": [
      {
        "message": "Internal error. Please try again or contact Google Support. (Code: '5B7AFB2001A53.AD5AC0C.4B01291B')",
        "domain": "global",
        "reason": "backendError"
      }
    ]
  }
}

当我尝试来自 GCP APIs 资源管理器的相同请求时,我收到相同的错误:

{
  "error": {
    "code": 503,
    "message": "Internal error. Please try again or contact Google Support. (Code: '5B7B0120B5674.A1CBB85.7900FC0D')",
    "errors": [
      {
        "message": "Internal error. Please try again or contact Google Support. (Code: '5B7B0120B5674.A1CBB85.7900FC0D')",
        "domain": "global",
        "reason": "backendError"
      }
    ]
  }
}

请求正文如下:

{
  "name": "test-machine-image-name",
  "sourceInstance": "https://www.googleapis.com/compute/v1/projects/devopsucp/global/instances/urigcpdev1"
}

503 是后端错误,可能在您尝试使用 API 的那一刻,它不可用。

我已经在我自己的项目中尝试了Method: machineImages.insert。我收到了输出 200,所以它对我有用。

但如果它仍然不适合您,作为解决方法,您可以:

  1. 创建您的实例的快照: Method: disks.createSnapshot

  2. 然后您可以从永久磁盘的快照创建磁盘映像。 Method: images.insert

或者您可以使用 gcloud 完成此任务。

我发现我的错误了! 在请求正文中,我将 sourceInstance 指定为:

"https://www.googleapis.com/compute/v1/projects/devopsucp/global/instances/urigcpdev1"

应该是:

"https://www.googleapis.com/compute/v1/projects/devopsucp/zones/{machineZone}/instances/urigcpdev1"

修复 instanceName 参数后,我收到 200 响应,图像按预期创建:)。

我之前收到的 503 响应非常混乱...