【发布时间】:2019-06-07 15:21:22
【问题描述】:
我在 GCS 事件触发的 Cloud Function 中调用 Cloud Speech-to-Text API。
在云函数(运行node index.js)之外执行此操作非常好,但我的错误随后出现。
使用this doc,我认为错误是由于身份验证问题造成的,但我尝试了几件事,现在不太确定。
我的代码是:
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const nl = require('@google-cloud/language');
const client_nl = new nl.LanguageServiceClient();
const speech = require('@google-cloud/speech');
const client_speech = new speech.SpeechClient();
exports.getRecording = (data,context) => {
const file = data;
if (file.resourceState === 'not_exists') {
// Ignore file deletions
return true;
} else if (!new RegExp(/\.(wav|mp3)/g).test(file.name)) {
// Ignore changes to non-audio files
return true;
}
console.log(`Analyzing gs://${file.bucket}/${file.name}`);
const bucket = storage.bucket(file.bucket);
const audio = {
uri: 'gs://${file.bucket}/${file.name}'
};
// Configure audio settings for BoF recordings
const audioConfig = {
encoding: 'LINEAR16',
sampleRateHertz: 44100,
languageCode: 'fr-FR'
};
const request = {
audio: audio,
config: audioConfig,
};
return client_speech.recognize(request)
.then(([transcription]) => {
const filename = `analysis.json`;
console.log(`Saving gs://${file.bucket}/${filename}`);
return bucket
.file(filename)
.save(JSON.stringify(transcription, null, 2));
});
然后我部署:
gcloud functions deploy getRecording --runtime nodejs10 --trigger-resource trigger-bucket-id --trigger-event google.storage.object.finalize --service-account my-service-account
我尝试了什么:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/file/keyfile.json- 添加了一个带有
"GOOGLE_APPLICATION_CREDENTIALS":"./keyfile.json"的config.json文件,以及根项目中的密钥文件和index.js中的require('./config.json') - 添加了一个选项 json
const options = {
projectId: 'my-project-id',
keyFilename: './key-file.json'
};
const client_speech = new speech.SpeechClient(options);
我不断收到此错误,需要一些帮助
D getRecording 573287126069013 2019-06-07 15:03:09.609 Function execution started
getRecording 573287126069013 2019-06-07 15:03:09.789 Analyzing gs://my-bucket/audio_trimed.wav
D getRecording 573287126069013 2019-06-07 15:03:10.979 Function execution took 1372 ms, finished with status: 'error'
E getRecording 573291408785013 2019-06-07 15:03:11.990 Error: Requested entity was not found.
at Http2CallStream.call.on (/srv/functions/node_modules/@grpc/grpc-js/build/src/client.js:101:45)
at Http2CallStream.emit (events.js:194:15)
at Http2CallStream.EventEmitter.emit (domain.js:459:23)
at Http2CallStream.endCall (/srv/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:63:18)
at handlingTrailers (/srv/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:152:18)
at process._tickCallback (internal/process/next_tick.js:68:7)
【问题讨论】:
标签: node.js google-cloud-platform google-cloud-speech