代码之家  ›  专栏  ›  技术社区  ›  Thomas David Kehoe

下载API文件到Firebase云存储?

  •  0
  • Thomas David Kehoe  · 技术社区  · 7 年前

    如何将IBM Watson文本到语音的音频文件(约10K)保存到Firebase云存储?这是我的代码,从IBM Watson复制过来的 documentation

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();
    
    var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
    var fs = require('fs');    
    
    exports.TextToSpeech = functions.firestore.document('Test_Value').onUpdate((change, context) => {
    
      var textToSpeech = new TextToSpeechV1({
        username: 'groucho',
        password: 'swordfish',
        url: 'https://stream.watsonplatform.net/text-to-speech/api'
      });
    
      var synthesizeParams = {
        text: 'Hello world',
        accept: 'audio/wav',
        voice: 'en-US_AllisonVoice'
      };
    
      textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
        console.log(error);
      }).pipe(fs.createWriteStream('hello_world.wav')); // what goes here?
    
      const file = ?????
      file.download()
      .then(function(data) {
        console.log("File downloaded."
      })
      .catch(error => {
        console.error(error);
      });
    });
    

    缺少的代码介于

    }).pipe(fs.createWriteStream('hello_world.wav'));
    

    file.download()
    

    我不得不将ibmwatson提供的文件转换成Firebase云存储可以识别的文件。是 fs 谷歌云功能中不允许?

    var fs = require('fs-js');
    

    var fs = require('fs'); 
    

    根据 NPM 这个 fs公司 包已弃用。

    pipe

    }).pipe(file);
    file.download()
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   dpopp07    7 年前

    file.download() ,我认为您可以使这项工作几乎没有改变您的代码。 file 需要类型 File 从Google存储库(您需要安装 this library createWriteStream 你可以把结果流出来 synthesize 到。我没有对此进行测试,但我相信它应该是正确的,或者至少应该为您指出正确的方向:

    // looks like you'll need to require this package also
    const { Storage } = require('@google-cloud/storage');
    
    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp();
    
    var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
    var fs = require('fs');    
    
    const storage = new Storage();
    const myBucket = storage.bucket('my-bucket');
    
    exports.TextToSpeech = functions.firestore.document('Test_Value').onUpdate((change, context) => {
    
      var textToSpeech = new TextToSpeechV1({
        username: 'groucho',
        password: 'swordfish',
        url: 'https://stream.watsonplatform.net/text-to-speech/api'
      });
    
      var synthesizeParams = {
        text: 'Hello world',
        accept: 'audio/wav',
        voice: 'en-US_AllisonVoice'
      };
    
      const file = myBucket.file('my-file'); // name your file something here
    
      textToSpeech
        .synthesize(synthesizeParams)
        .on('error', function(error) {
          console.log(error);
        })
        .pipe(file.createWriteStream()) // the File object has a `createWriteStream` method for writing streams to it
        .on('error', function(err) {
          console.log(err);
        })
        .on('finish', function() {
          // The file upload is complete.
          file.download()
            .then(function(data) {
              console.log("File downloaded.");
            })
            .catch(error => {
              console.error(error);
            });
        });
    });
    

    记录在案:

    • pipe() 应该允许在谷歌云功能和它 异步。这就是为什么你需要倾听 finish 下载文件前的事件
    • fs fs公司 Node's core built-in packages . 也就是说,看起来您可能根本不需要在代码中使用它
        2
  •  1
  •   Thomas David Kehoe    7 年前

    exports.TextToSpeech = functions.firestore.document('Test_Word').onUpdate((change, context) => {
    
    if (change.after.data().word != undefined) {
        myWord = change.after.data().word;
        myWordFileType = myWord + '.ogg';
    
      var synthesizeParams = {
            text: myWord,
            accept: 'audio/ogg',
            voice: 'en-US_AllisonVoice'
          };
    
          const {Storage} = require('@google-cloud/storage');
          const storage = new Storage();
          const bucket = storage.bucket('myapp.appspot.com');
          const file = bucket.file('Test_Folder' + myWordFileType);
    
          var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
    
          var textToSpeech = new TextToSpeechV1({
            username: 'groucho',
            password: 'swordfish',
            url: 'https://stream.watsonplatform.net/text-to-speech/api'
          });
    
          textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
            console.log(error);
          }).pipe(file.createWriteStream({contentType: 'auto'}))
          .on('error', function(err) {})
          .on('finish', function() {
            console.log("Complete.");
          });
          }
          return 0;
        });
    

    当一个新词写入Firestore位置时,该函数触发,然后提取该词并调用它 myWord . 添加.ogg使 myWordFileType pipe file.createWriteStream contentType 必须设置为获取可读文件,但是 auto 这很简单。然后将文件写入bucket,该bucket被设置为我的Firebase云存储文件夹 Test_Folder 文件名是 myWordFileType .