【问题标题】:Why aren't there any SDKs to call Cloud SQL API?为什么没有 SDK 可以调用 Cloud SQL API?
【发布时间】:2019-09-20 17:13:51
【问题描述】:

我想从 Cloud Functions 调用下述 Cloud SQL API。

我还找到了调用 GCP API 的库。

但是,Cloud SQL 似乎没有任何模块。

我想知道为什么它没有实施。 API相对较新的原因是什么?还是我误解了库的目的,实际上它不应该在库中实现?

【问题讨论】:

    标签: google-cloud-platform google-cloud-sql


    【解决方案1】:

    在您linked 的页面底部,您将找到不同语言的示例代码,以通过构建客户端来调用该 API。例如,Node.js 的示例代码如下所示:

    const {google} = require('googleapis');
    var sqlAdmin = google.sqladmin('v1beta4');
    
    authorize(function(authClient) {
      var request = {
        // Project ID of the project that contains the instance to be exported.
        project: 'my-project',  // TODO: Update placeholder value.
    
        // Cloud SQL instance ID. This does not include the project ID.
        instance: 'my-instance',  // TODO: Update placeholder value.
    
        resource: {
          // TODO: Add desired properties to the request body.
        },
    
        auth: authClient,
      };
    
      sqlAdmin.instances.export(request, function(err, response) {
        if (err) {
          console.error(err);
          return;
        }
    
        // TODO: Change code below to process the `response` object:
        console.log(JSON.stringify(response, null, 2));
      });
    });
    
    function authorize(callback) {
      google.auth.getApplicationDefault(function(err, authClient) {
        if (err) {
          console.error('authentication failed: ', err);
          return;
        }
        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
          var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
          authClient = authClient.createScoped(scopes);
        }
        callback(authClient);
      });
    }
    

    要从 Cloud Function 连接到 Cloud SQL 实例,请遵循文档here

    【讨论】:

    • 谢谢,我完全错过了它,现在我发现不是 google-cloud-node 而是 google-api-nodejs-client 具有调用 API 的接口。我不完全理解其中的区别,但无论如何现在我可以尝试。再次感谢。
    • 对于任何人来说:googleapis.dev/nodejs/googleapis/latest/sqladmin/classes/… 如果你向下滚动页面,你会看到一个更简洁的代码导入示例(在我看来)。
    【解决方案2】:

    您可以使用 gcloud sdk 调用 api。如果您只是想导出数据库,请查看此文档:https://cloud.google.com/sdk/gcloud/reference/sql/instances/export

    【讨论】:

    • 感谢您的回答。据我了解,库 google-cloud-node / google-cloud-go 具有与 sdk 相同的功能。我想知道为什么有一些库没有但 sdk 有的功能。由于我想在 Cloud Functions 中调用 API,因此最好直接从 JS 或 Go 等中使用它。
    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2011-03-04
    • 2021-06-09
    • 2015-02-20
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多