我正在使用 这个 nodejs bigquery 客户端
我在 string\text 中有服务帐户 json,出于安全原因,我想避免将其写入临时文件。
是否可以选择new BigQuery()
将服务帐户作为字符串而不是文件路径提供?
在任何地方都找不到此选项,在所有示例中都需要提供文件路径或导出GOOGLE_APPLICATION_CREDENTIALS变量。
谢谢。
我在 string\text 中有服务帐户 json,出于安全原因,我想避免将其写入临时文件。
是否可以选择new BigQuery()
将服务帐户作为字符串而不是文件路径提供?
在任何地方都找不到此选项,在所有示例中都需要提供文件路径或导出GOOGLE_APPLICATION_CREDENTIALS变量。
谢谢。
可以将服务帐户中的值用作身份验证字符串。您可以使用BigQueryOptions并传递凭据对象。Credentials 对象将需要client_email
并且private_key
可以在您的服务帐户 json 中找到。
使用您在问题中链接的示例代码,BigQueryOptions
可以以这种方式实现。
const creds = {
client_email: 'your_service_account@project-id.iam.gserviceaccount.com',
private_key: '-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n'
};
const bigquery = new BigQuery(credentials=creds);
整个代码将是:
const {BigQuery} = require('@google-cloud/bigquery');
const creds = {
client_email: 'your_service_account@project-id.iam.gserviceaccount.com',
private_key: '-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n'
};
const bigquery = new BigQuery(credentials=creds);
async function query() {
// Queries the U.S. given names dataset for the state of Texas.
const query = `SELECT name
FROM \`bigquery-public-data.usa_names.usa_1910_2013\`
WHERE state = 'TX'
LIMIT 100`;
// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
const options = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
};
// Run the query as a job
const [job] = await bigquery.createQueryJob(options);
console.log(`Job ${job.id} started.`);
// Wait for the query to finish
const [rows] = await job.getQueryResults();
// Print the results
console.log('Rows:');
rows.forEach(row => console.log(row));
}
query()
输出片段: