Node.js 如何通过Firebase云函数触发GCP谷歌语音API?

Node.js 如何通过Firebase云函数触发GCP谷歌语音API?,node.js,firebase,google-cloud-firestore,google-cloud-functions,google-speech-api,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,Google Speech Api,我在GCP中有一个工作的nodejs程序,它使用GCP语音到文本API 异步函数main(){ //导入Google云客户端库 const speech=require(“@googlecloud/speech”); 常数fs=要求('fs'); //创建一个客户端 const client=new speech.SpeechClient(); 常量文件名='。/resource/amazing30s_mono.flac';//mono-clip //读取本地音频文件并将其转换为base64 c

我在GCP中有一个工作的nodejs程序,它使用GCP语音到文本API

异步函数main(){ //导入Google云客户端库 const speech=require(“@googlecloud/speech”); 常数fs=要求('fs'); //创建一个客户端 const client=new speech.SpeechClient(); 常量文件名='。/resource/amazing30s_mono.flac';//mono-clip //读取本地音频文件并将其转换为base64 const file=fs.readFileSync(文件名); const audioBytes=file.toString('base64'); //音频文件的编码、采样率(赫兹)和BCP-47语言代码 常量音频={ 内容:音频字节, }; 常量配置={ 语言代码:'en-US',//马来语试试'ms-MY'(https://cloud.google.com/speech-to-text/docs/languages) enableautomaticpuntuation:true,//https://cloud.google.com/speech-to-text/docs/automatic-punctuation }; 常量请求={ 音频:音频, config:config, }; //检测音频文件中的语音 const[response]=等待客户端识别(请求); 常量转录=响应。结果 .map(result=>result.alternations[0]。转录本) .join('\n'); log(`Transcription:${Transcription}`); } main().catch(console.error); 我有一个工作类型脚本Firebase Cloud Func

import * as functions from 'firebase-functions';

export const reqWithParam = functions.https.onRequest((request, response) => {
  let name = request.query.name
  response.send("Request with name= " + name);
});
但是我怎样才能把这两个程序结合起来呢?或者,如何将nodejs代码引入Firebase云函数,使Firebase云函数触发GoogleAPI将语音转换为文本

以上两个程序都在同一个GCP项目下,同一个服务器(Google cloud shell)

我已经通读了,但仍有遗漏的空白

  • 如何“导入”节点程序
  • 如何获取音频文件的GS路径
  • 如何验证令牌(同一GCP项目)
  • 任何显示“通过Firebase调用GCP API”详细步骤的YouTube教程

当我注意到nodejs代码可以毫无错误地粘贴到Typescript中时,我感到很惊讶!我设法在Firebase中完美地运行google API

export const GCPSpeech2Text = functions.https.onRequest(async (req, res) => {

    const speech = require('@google-cloud/speech');
    // Creates a client
    const client = new speech.SpeechClient();
    const gcsUri = 'gs://<project-name>.appspot.com/<file path...>/myaudio.flac'

    const encoding = 'FLAC';
    const languageCode = 'en-US';

    const config = {
        encoding: encoding,
        languageCode: languageCode,
        audioChannelCount: 2, // hit 'Invalid audio channel count' if not specify
        enableAutomaticPunctuation: true, // https://cloud.google.com/speech-to-text/docs/automatic-punctuation
    };

    const audio = {
        uri: gcsUri,
    };

    const request = {
        audio: audio,
        config: config,
    };

    // Detects speech in the audio file
    const [response] = await client.recognize(request);
    const transcription = response.results
        .map((result: any) => result.alternatives[0].transcript)
        .join('\n');
    res.send(`Transcription ${transcription} created.`);
})
export const GCPSpeech2Text=functions.https.onRequest(异步(req,res)=>{
const speech=require(“@googlecloud/speech”);
//创建一个客户端
const client=new speech.SpeechClient();
const gcsUri='gs://.appspot.com//myaudio.flac'
常量编码='FLAC';
常量语言代码='en US';
常量配置={
编码:编码,
languageCode:languageCode,
audioChannelCount:2,//如果未指定,请点击“无效的音频通道计数”
enableautomaticpuntuation:true,//https://cloud.google.com/speech-to-text/docs/automatic-punctuation
};
常量音频={
uri:gcsUri,
};
常量请求={
音频:音频,
config:config,
};
//检测音频文件中的语音
const[response]=等待客户端识别(请求);
常量转录=响应。结果
.map((结果:any)=>result.alternations[0]。转录本)
.join('\n');
res.send(`translation${translation}已创建。`);
})
是啊