【发布时间】:2019-09-20 17:13:51
【问题描述】:
我想从 Cloud Functions 调用下述 Cloud SQL API。
我还找到了调用 GCP API 的库。
但是,Cloud SQL 似乎没有任何模块。
我想知道为什么它没有实施。 API相对较新的原因是什么?还是我误解了库的目的,实际上它不应该在库中实现?
【问题讨论】:
标签: google-cloud-platform google-cloud-sql
我想从 Cloud Functions 调用下述 Cloud SQL API。
我还找到了调用 GCP API 的库。
但是,Cloud SQL 似乎没有任何模块。
我想知道为什么它没有实施。 API相对较新的原因是什么?还是我误解了库的目的,实际上它不应该在库中实现?
【问题讨论】:
标签: google-cloud-platform google-cloud-sql
在您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。
【讨论】:
您可以使用 gcloud sdk 调用 api。如果您只是想导出数据库,请查看此文档:https://cloud.google.com/sdk/gcloud/reference/sql/instances/export
【讨论】: