我需要检查项目中当前的云调度程序作业。使用这两个片段,但我不明白为什么会出现错误,即
{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)
有人知道我做错了什么吗?
不确定您在关注什么文档,但官方库文档提到您的操作方式是旧方式,您应该迁移到新方法。这在他们的迁移指南中有解释
之前
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 }, 'relative_uri': '/log_payload', 'http_method': 'POST', 'body': 'Hello World'.encode() }, 'schedule': '* * * * *', '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/