I call Cloud Speech-to-Text API in a Cloud Function triggered by a GCS event.
Doing it outside of a Cloud Function (running node index.js
) is perfectly fine, but my error comes afterwards.
Using this doc, I believed that the error was due to an authentification issue, but I tried several things and I am not so sure now.
My code is :
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));
});
I then deploy with :
gcloud functions deploy getRecording --runtime nodejs10 --trigger-resource trigger-bucket-id --trigger-event google.storage.object.finalize --service-account my-service-account
What I tried :
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/file/keyfile.json
config.json
file with "GOOGLE_APPLICATION_CREDENTIALS":"./keyfile.json"
inside, and the keyfile in the root project and a require('./config.json')
in the index.js
const options = {
projectId: 'my-project-id',
keyFilename: './key-file.json'
};
const client_speech = new speech.SpeechClient(options);
I keep getting this error and would need some help
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)
@Bruce was right, I finally spotted the mistake.
I just couldn't see that it was written
uri:'gs://${file.bucket}/${file.name}'
and not
uri: `gs://${file.bucket}/${file.name}`
(yes the mistake was tiny but could have been avoided with more testing...