Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java挂起Google云平台实例_Java_Google Cloud Platform_Virtual Machine_Google Compute Engine_Google Compute Api - Fatal编程技术网

使用Java挂起Google云平台实例

使用Java挂起Google云平台实例,java,google-cloud-platform,virtual-machine,google-compute-engine,google-compute-api,Java,Google Cloud Platform,Virtual Machine,Google Compute Engine,Google Compute Api,我有一个Java web应用程序,在其中创建GCP VM实例并对其执行操作 我使用Java计算引擎API来执行创建、启动、停止等任务 我想表演: Compute compute = getComputeObj(); Suspend suspend = compute.instances().suspend(getProjectId(), getOrder().getAvailabilityZone(), getOrder().getMachineName()); Operation respon

我有一个Java web应用程序,在其中创建GCP VM实例并对其执行操作

我使用Java计算引擎API来执行创建、启动、停止等任务

我想表演:

Compute compute = getComputeObj();
Suspend suspend = compute.instances().suspend(getProjectId(), getOrder().getAvailabilityZone(), getOrder().getMachineName());
Operation response = suspend .execute();
但是,我不能用这个API“挂起”机器,因为“挂起”是beta版本(它不是computeenginev1API的一部分,所以它不是Java包装器的一部分)

在我看来,我有两个选择来解决这个问题,但我都没有完成:

  • 创建一个Java类“ComputeBeta”,它继承Compute类并实现一个Suspend类来执行操作。这是有问题的,因为原始Compute类中的URL字段是最终字段,所以我无法将URL从“v1”更改为“beta”URL

  • 执行到GCP beta API的“常规”httpconnection以挂起机器。这里的问题是,我无法找到API身份验证的正确语法,并且得到了401响应。到目前为止,我所尝试的:


  • 任何关于如何前进的帮助都将不胜感激

    您不必像这样使用您的服务帐户密钥。此密钥允许您创建凭据,然后使用它生成令牌

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
     String token = credentials.refreshAccessToken().getTokenValue();
    ...
     httpConnection.setRequestProperty("Authorization", "Bearer " + token);
    
    
    您还可以使用Google Transport factory来避免手动添加授权标头

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
    HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
    // Here a GET request, but you can build a POST request also
    HttpRequest request = factory.buildGetRequest(new GenericUrl("YOUR_URL"));
    HttpResponse httpResponse = request.execute();
    System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));
    
    如果您有依赖关系问题(通常不是因为您使用包含它的计算引擎库),您可以添加此依赖关系

            <dependency>
                <groupId>com.google.auth</groupId>
                <artifactId>google-auth-library-oauth2-http</artifactId>
                <version>0.21.1</version>
            </dependency>
    
    
    com.google.auth
    google-auth-library-oauth2-http
    0.21.1
    
    您不必像这样使用您的服务帐户密钥。此密钥允许您创建凭据,然后使用它生成令牌

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
     String token = credentials.refreshAccessToken().getTokenValue();
    ...
     httpConnection.setRequestProperty("Authorization", "Bearer " + token);
    
    
    您还可以使用Google Transport factory来避免手动添加授权标头

     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    // You can use explicitly your service account key file like this
    // GoogleCredentials credentials = GoogleCredentials.fromStream(this.getClass().getResourceAsStream("my_key_file.json"));
    
    HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(credentials));
    // Here a GET request, but you can build a POST request also
    HttpRequest request = factory.buildGetRequest(new GenericUrl("YOUR_URL"));
    HttpResponse httpResponse = request.execute();
    System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));
    
    如果您有依赖关系问题(通常不是因为您使用包含它的计算引擎库),您可以添加此依赖关系

            <dependency>
                <groupId>com.google.auth</groupId>
                <artifactId>google-auth-library-oauth2-http</artifactId>
                <version>0.21.1</version>
            </dependency>
    
    
    com.google.auth
    google-auth-library-oauth2-http
    0.21.1
    
    非常感谢,我现在就试试。只有一个问题,这个依赖项应该添加到哪里?如果您使用Maven,请在pom.xml文件的
    依赖项部分添加。如果你使用Gradle,你必须稍微转换一下,它是一个内联模型
    com.google.auth:google-auth-library-oauth2-http:0.21.1
    非常感谢,我现在就试试。只有一个问题,这个依赖项应该添加到哪里?如果您使用Maven,请在pom.xml文件的
    依赖项部分添加。如果您使用Gradle,您必须稍微对其进行转换,它是一个内联模型
    com.google.auth:google-auth-library-oauth2-http:0.21.1