AttributeError: 'CloudSchedulerClient' 对象没有属性 'location_path'

问题描述

我需要检查项目中当前的云调度程序作业。并使用这两个片段,但我不明白为什么我会收到错误,即

{AttributeError: 'CloudSchedulerClient' 对象没有属性 'location_path'}

我已经使用了从谷歌云文档中看到的这段代码。

from google.cloud.scheduler import CloudSchedulerClient

def display_cloud_scheduler(project):
  client = CloudSchedulerClient.from_service_account_json(
  r"./xxxx.json")
  print(client)
  parent = client.location_path(project,'us-east1')
  for element in client.list_jobs(parent):
    print(element)
from google.cloud import scheduler_v1

def display_cloud_scheduler(project):
  client = scheduler_v1.CloudSchedulerClient.from_service_account_json(
  r"./xxxx.json")
  print(client)
  parent = client.location_path(project,'us-east1')
  for element in client.list_jobs(parent):
    print(element)

有人知道我做错了什么吗?

解决方法

不确定您在遵循什么文档,但 official library docs 提到您的做法是 方式,您应该迁移到新方式方法。这在他们的 migration 指南中有解释:

之前

from google.cloud import scheduler

client = scheduler.CloudSchedulerClient()

parent = client.location_path("<PROJECT_ID>","<LOCATION>")
job = {
       'app_engine_http_target': {
           'app_engine_routing': {
               'service': service_id
           },'relative_uri': '/log_payload','http_method': 'POST','body': 'Hello World'.encode()
       },'schedule': '* * * * *','time_zone': 'America/Los_Angeles'
   }

response = client.create_job(parent,job)

之后

from google.cloud import scheduler

client = scheduler.CloudSchedulerClient()
parent = "projects/<PROJECT_ID>/locations/<LOCATION>"
job = {
       'app_engine_http_target': {
           'app_engine_routing': {
               'service': service_id
           },'time_zone': 'America/Los_Angeles'
   }

response = client.create_job(
   request={
       "parent": parent,"job": job
   }
)

所以我建议检查您的代码并使用新方法。此外,在该迁移指南中还提到了以下有关可以帮助更轻松地完成这些事情的迁移脚本的内容:

脚本 fixup_scheduler_{version}_keywords.py 随库一起提供。它需要一个输入目录(带有要转换的代码)和一个空的目标目录。

$ fixup_scheduler_v1_keywords.py --input-directory .samples/ --output-directory samples/

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...