簡體   English   中英

通過 Java API 創建 Google Cloud Function:“io.grpc.StatusRuntimeException: INVALID_ARGUMENT”

[英]Creating Google Cloud Function via Java API: “io.grpc.StatusRuntimeException: INVALID_ARGUMENT”

我正在嘗試使用 Java 客戶端 API 與此客戶端創建 Google Cloud Function:

Credentials myCredentials = ServiceAccountCredentials.fromStream(new FileInputStream(keyFile));
CloudFunctionsServiceSettings settings = CloudFunctionsServiceSettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)).build();
client = CloudFunctionsServiceClient.create(settings);


String project = "my-project-name";
String location = "us-central1";

LocationName locationName = LocationName.of( project, location );
CloudFunction function = CloudFunction.newBuilder().build();
CloudFunction response = client.createFunctionAsync(locationName, function).get();

我嘗試了不同的調用,但我總是得到以下堆棧跟蹤:

java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: The request has errors
        at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:566)
        at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:547)
        at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:86)
        at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:62)
        at com.google.api.gax.longrunning.OperationFutureImpl.get(OperationFutureImpl.java:127)
        at it.myapp.test.App.main(App.java:59)
Caused by: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: The request has errors
        at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:49)
        at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
        at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
        at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97)
        at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
        at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1041)
        at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:30)
        at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1215)
        at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:983)
        at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:771)
        at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:563)
        at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:533)
        at io.grpc.internal.DelayedClientCall$DelayedListener$3.run(DelayedClientCall.java:464)
        at io.grpc.internal.DelayedClientCall$DelayedListener.delayOrExecute(DelayedClientCall.java:428)
        at io.grpc.internal.DelayedClientCall$DelayedListener.onClose(DelayedClientCall.java:461)
        at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:553)
        at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:68)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:739)
        at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:718)
        at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
        at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: The request has errors
        at io.grpc.Status.asRuntimeException(Status.java:535)
        ... 16 more

我的 pom.xml 有以下設置:

<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-functions-bom</artifactId>
    <version>1.0.8</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>
</dependencyManagement>
<dependencies>
 <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-functions</artifactId>
 </dependency>
</dependencies>

有誰知道我做錯了什么?

謝謝

該錯誤意味着您的 Function Builder 缺少創建 function 所需的參數。 如果您嘗試通過 Cloud Console 創建 function,則需要輸入 function 名稱、入口點、運行時、觸發器類型和源代碼等詳細信息。

我已經聯系了工程師,他們現在被告知日志 output 中缺少詳細信息。

作為解決方案,這里有一個示例代碼,它將創建一個在 Java 11 上運行的雲 Function。當然,您始終可以選擇所需的任何類型的運行時:

package function;

import com.google.cloud.functions.v1.CloudFunctionsServiceClient;
import com.google.cloud.functions.v1.HttpsTrigger;
import com.google.cloud.functions.v1.CloudFunction;
import com.google.cloud.functions.v1.LocationName;

public class App {
  public static void main( String[] args ){
    try {

      // TODO: Add your credentials here

      CloudFunctionsServiceClient cloudFunctionsServiceClient = CloudFunctionsServiceClient.create();
      String location = LocationName.of("[PROJECT_ID]", "us-central1").toString();
      CloudFunction function = CloudFunction.newBuilder()
        .setName(location + "/functions/[FUNCTION_NAME]")
        .setEntryPoint("functions.HelloHttp") // fully qualified class name (FQN)
        .setRuntime("java11")
        .setHttpsTrigger(HttpsTrigger.getDefaultInstance())
        .setSourceArchiveUrl("gs://[BUCKET_NAME]/source_code.zip")
        .build(); 
  
      CloudFunction response = cloudFunctionsServiceClient.createFunctionAsync(location, function).get();
    }catch (Exception e){
      e.printStackTrace();
    }
  }
}

注意:如果您的壓縮源代碼來自存儲桶,請確保您的源文件位於 ZIP 文件的根目錄,而不是包含文件的文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM