首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用google-cloud-python API访问Dataproc时出现无效区域错误

使用google-cloud-python API访问Dataproc时出现无效区域错误
EN

Stack Overflow用户
提问于 2018-11-21 04:44:36
回答 3查看 1.8K关注 0票数 2

我正在尝试使用google-cloud-python库在Dataproc中创建一个集群,然而,当设置region = 'us-central1'时,我得到以下异常:

代码语言:javascript
复制
google.api_core.exceptions.InvalidArgument: 400 Region 'us-central1' is invalid.
Please see https://cloud.google.com/dataproc/docs/concepts/regional-endpoints
for additional information on regional endpoints

代码(基于example):

代码语言:javascript
复制
#!/usr/bin/python

from google.cloud import dataproc_v1

client = dataproc_v1.ClusterControllerClient()

project_id = 'my-project'
region = 'us-central1'
cluster = {...}

response = client.create_cluster(project_id, region, cluster)
EN

回答 3

Stack Overflow用户

发布于 2018-11-21 04:45:33

Dataproc使用region字段来路由REST请求,但是在gRPC客户端中没有使用该字段(因此出现错误)。

只有global多区域才能通过默认端点访问。要使用区域端点,如us-central1,您必须将该端点配置为在客户端的transport上寻址。

Dataproc区域端点遵循以下模式:<region>-dataproc.googleapis.com:443region字段应设置为与端点中的区域相同的值。

示例:

代码语言:javascript
复制
#!/usr/bin/python

from google.cloud import dataproc_v1
from google.cloud.dataproc_v1.gapic.transports import cluster_controller_grpc_transport

transport = cluster_controller_grpc_transport.ClusterControllerGrpcTransport(
    address='us-central1-dataproc.googleapis.com:443')
client = dataproc_v1.ClusterControllerClient(transport)

project_id = 'my-project'
region = 'us-central1'
cluster = {...}

response = client.create_cluster(project_id, region, cluster)
票数 4
EN

Stack Overflow用户

发布于 2018-11-21 04:53:24

类似地,使用google-cloud-java客户端:

代码语言:javascript
复制
ClusterControllerSettings settings =
     ClusterControllerSettings.newBuilder()
        .setEndpoint("us-central1-dataproc.googleapis.com:443")
        .build();
 try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settings)) {
   String projectId = "my-project";
   String region = "us-central1";
   Cluster cluster = Cluster.newBuilder().build();
   Cluster response =
       clusterControllerClient.createClusterAsync(projectId, region, cluster).get();
 }
票数 0
EN

Stack Overflow用户

发布于 2019-10-04 21:38:40

目前,推荐的更改默认API端点的方式是to use client_options

client_options (

,google.api_core.client_options.ClientOptions) -客户端选项,用于设置客户端上的用户选项。应通过client_options设置接口端点。

下面是一个从json文件加载凭据的示例(使用f字符串的Python 3.6+语法):

代码语言:javascript
复制
from google.cloud.dataproc_v1 import ClusterControllerClient


client = ClusterControllerClient.from_service_account_file(
             service_account_json_path,
             client_options={'api_endpoint': f'{your_region}-dataproc.googleapis.com:443'})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53401219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档