简体   繁体   English

使用 Google Cloud API 和 Python - 使用库创建计划 Sanpshots

[英]Using Google Cloud API with Python - Using libs to creating Schedule Sanpshots

I'm probably very confused with the API concepts but I'm not understanding how to use REST Google API with Python.我可能对 API 概念感到非常困惑,但我不明白如何使用 REST Google API 和 ZA72117F35426B56927。

On the API documentation , it said to use HTTP Requests or client libs (in case of Python, libcloud or Google API Python Client Library. I saw examples for some functionalities that work well like, create VMs or attaching disks. On the API documentation , it said to use HTTP Requests or client libs (in case of Python, libcloud or Google API Python Client Library. I saw examples for some functionalities that work well like, create VMs or attaching disks.

Although, I could not find an example pure REST request, like, if I want to create a scheduled snapshot.虽然,我找不到纯 REST 请求的示例,例如,如果我想创建计划快照。

So I have two questions:所以我有两个问题:

  1. How to use libs to authenticate and call a function that the lib doesn't have a built-in method (like schedule snapshots)?如何使用库进行身份验证并调用该库没有内置方法(如计划快照)的 function?

According to documentation, should be a request like this:根据文档,应该是这样的请求:

https://compute.googleapis.com/compute/v1/projects/{PROJECT_ID}/regions/{REGION_ID}/resourcePolicies

    {
        "name": "name",
        "snapshotSchedulePolicy": {
        "schedule": {
            "dailySchedule": {
            "startTime": "12:00",
            "daysInCycle": "1"
            }
        },
        "retentionPolicy": {
            "maxRetentionDays": "5"
        },
        "snapshotProperties": {
            "guestFlush": "False",
            "labels": {
            "env": "dev",
            "media": "images"
            },
            "storageLocations": ["US"]
        }
    }
  1. Can I use the API, inside a Cloud Functions, without worrying to get a Token?我可以在 Cloud Functions 中使用 API,而不必担心获得令牌吗?

Someone can help me understand this better?有人可以帮助我更好地理解这一点吗?

Thanks in advance提前致谢

This can be confusing but it's reasonably well documented on Google Cloud and I encourage you to have a read around on there for more details.这可能会令人困惑,但它在 Google Cloud 上的记录相当完善,我鼓励您在那里阅读以了解更多详细信息。

For the specific API method you describe:对于您描述的特定 API 方法:

https://cloud.google.com/compute/docs/reference/rest/beta/resourcePolicies/insert https://cloud.google.com/compute/docs/reference/rest/beta/resourcePolicies/insert

This page includes the ability to invoke the API method yourself (see right hand side of the page).此页面包含自行调用 API 方法的功能(请参见页面右侧)。 This uses the most-excellent Google APIs Explorer which you may also access directly:这使用了最优秀的 Google APIs Explorer,您也可以直接访问它:

https://developers.google.com/apis-explorer https://developers.google.com/apis-explorer

and, drilling down into Compute Engine beta:并且,深入到 Compute Engine 测试版:

https://cloud.google.com/compute/docs/reference/rest/beta/#rest-resource:-beta.resourcepolicies https://cloud.google.com/compute/docs/reference/rest/beta/#rest-resource:-beta.resourcepolicies

Another useful (trick) is to issue (any) gcloud command with the flag --log-http .另一个有用的(技巧)是发出(任何)带有标志的gcloud命令--log-http This will then include the HTTP method request|response details that underlie the command and this can help you better understand what's going on.然后,这将包括 HTTP 方法请求|响应作为命令基础的详细信息,这可以帮助您更好地了解正在发生的事情。 In your case:在你的情况下:

gcloud beta compute resource-policies ... --log-http

There are 2 types of Google-provided library. Google 提供的库有 2 种类型。 The older API client libraries and the newer Cloud.较旧的 API 客户端库和较新的 Cloud。 See:看:

https://cloud.google.com/apis/docs/client-libraries-explained https://cloud.google.com/apis/docs/client-libraries-explained

API client libraries are machine-generated and you should be able to access all API methods for all libraries for all Google's supported languages with these. API 客户端库是机器生成的,您应该能够使用这些库访问所有库的所有API 方法。

Cloud client libraries are hand-crafted (mainly) and these often lag the underlying APIs.云客户端库(主要)是手工制作的,这些库通常落后于底层 API。

Compute Engine does not have Cloud client libraries (for any language; go figure.), So. Compute Engine没有Cloud 客户端库(适用于任何语言;go 图。),所以。 you should be able to invoke the beta resourcepolicies using the Python API client library.您应该能够使用 Python API 客户端库调用 beta 资源策略。

That said, you may always hand-craft REST calls yourself in your preferred language but this is discouraged because it requires you to manage auth etc. yourself and this can be challenging .也就是说,您可能总是手工制作 REST 以您喜欢的语言调用自己,但不鼓励这样做,因为它需要您自己管理身份验证等,这可能具有挑战性

Yes, you could make (any) REST call from Cloud Functions but you will always need to auth the call if the backing service requires authentication.是的,您可以从 Cloud Functions 进行(任何)REST 调用,但如果支持服务需要身份验证,您将始终需要对调用进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 的 Google Cloud Billing API - Google Cloud Billing API using Python 在 Python 中使用 API 创建 Google Cloud Function - Create Google Cloud Function using API in Python 使用Python从xml创建计划任务 - Creating Schedule Task from xml using Python 将 Google Calendar API 与 Python 3.7 Google Cloud Function 结合使用 - Using Google Calendar API with a Python 3.7 Google Cloud Function 在Google云端点项目(Python)中使用其他Google API - Using other Google API's in a Google cloud endpoints project (Python) 使用Python在Google Cloud Storage存储桶中创建/上传新文件 - Creating/Uploading new file at Google Cloud Storage bucket using Python 使用 Google API 在 Google 表格中通过 Python 创建新标签 - Creating New Tabs via Python in Google Sheets using Google API Google云端:使用python API获取GKE统计信息 - Google cloud: Get GKE stats using python API CERTIFICATE_VERIFY_FAILED使用google cloud api和Python - CERTIFICATE_VERIFY_FAILED using google cloud api with Python 如何在 Python 中使用谷歌云翻译 API 翻译大文本? - How to translate large text using Google Cloud Translation API in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM