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();
}

此请求返回以下响应:

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 API资源管理器的相同请求时,我会得到相同的错误:

{
"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"
}

我发现了我的错误!在请求正文中,我将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回复相当令人困惑。。。

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

我在自己的项目中尝试过方法:machineImages.insert。我收到了200的输出,所以它对我有效

但如果它仍然不适合你,作为变通方法,你可以:

  1. 创建实例的快照:方法:disks.createSnapshot

  2. 然后,您可以从永久磁盘的快照创建磁盘映像。方法:图像。插入

或者您可以使用gcloud执行此任务。

相关内容

  • 没有找到相关文章

最新更新