微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。