Вы не можете указать путь к облачному хранилищу Google. Предполагая, что вы развернули свою функцию с правами доступа для доступа к BLOB-объекту (ключ. json файл) из корзины, вы можете загрузить этот файл из облачного хранилища Google в каталог \tmp
вашей облачной функции.
Загрузка объектов
const {Storage} = require('@google-cloud/storage');
const {BigQuery} = require('@google-cloud/bigquery');
// Creates a client
const storage = new Storage();
async function downloadFile() {
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: \tmp\key.json,
};
// Downloads the file
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
}
downloadFile().catch(console.error);
const options = {
keyFilename: '/tmp/key.json',
projectId: 'my_project',
};
const bigquery = new BigQuery(options);
Лучшим решением было бы сохранить файл key.json
с Google Secret Manager . Затем назначьте облачной функции роль secretmanager.secretAccessor
и получите доступ к секретной облачной функции.
Создание секретов и версий
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const name = 'projects/my-project/secrets/my-secret/versions/5';
// const name = 'projects/my-project/secrets/my-secret/versions/latest';
// Imports the Secret Manager library
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
// Instantiates a client
const client = new SecretManagerServiceClient();
async function accessSecretVersion() {
const [version] = await client.accessSecretVersion({
name: name,
});
// Extract the payload as a string.
const payload = version.payload.data.toString('utf8');
// WARNING: Do not print the secret in a production environment - this
// snippet is showing how to access the secret material.
console.info(`Payload: ${payload}`);
}
accessSecretVersion();