Speech-to-Text API 无法通过 "Requested entity was not found" 错误

Can't go through "Requested entity was not found" error with Speech-to-Text API

我在由 GCS 事件触发的 Cloud Function 中调用 Cloud Speech-to-Text API。 在 Cloud Function (运行 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

我尝试了什么:

  1. export GOOGLE_APPLICATION_CREDENTIALS=/path/to/file/keyfile.json
  2. 添加了一个 config.json 文件,其中包含 "GOOGLE_APPLICATION_CREDENTIALS":"./keyfile.json",根项目中的密钥文件和 index.js
  3. 中的 require('./config.json')
  4. 添加了一个选项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)

this code 有帮助吗? 有一些 Whosebug 帖子可能会有所帮助 here, and here

我建议更详细的日志记录/调试。 例如,你能证明这两行符合你的预期吗?字符串是否生成格式正确的 uri?

const bucket = storage.bucket(file.bucket);
  const audio = {
    uri: 'gs://${file.bucket}/${file.name}'
  };

如果您找到解决方案,请告诉我们

@Bruce 说的对,我终于发现错误了。 我只是看不到它是写的 uri:'gs://${file.bucket}/${file.name}'

而不是

uri: `gs://${file.bucket}/${file.name}`

(是的,这个错误很小,但可以通过更多测试来避免...