簡體   English   中英

將API文件下載到Firebase Cloud Storage?

[英]Downloading an API file to Firebase Cloud Storage?

如何將IBM Watson Text-to-speech中的音頻文件(大約10K)保存到Firebase Cloud Storage? 這是我的代碼,從IBM Watson 文檔復制而來:

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()

我必須以某種方式將IBM Watson提供的文件轉換為Firebase Cloud Storage可以識別的文件。 Google Cloud Functions中不允許使用fs嗎?

另外,第六行不應該是

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

var fs = require('fs'); 

根據NPM ,不建議使用fs軟件包。

Google Cloud Functions允許使用pipe嗎? 如果是這樣,我該通過什么管道傳輸文件? 我需要這樣的東西:

}).pipe(file);
file.download()

好的,請查閱file.download()的文檔,我認為您可以對代碼進行一點改動就可以完成這項工作。 file必須為Google存儲庫中的File類型(您需要安裝此庫 )。 此類型具有一個稱為createWriteStream的方法,您可以將synthesize結果流式傳輸到該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);
        });
    });
});

作為記錄:

  • Google Cloud Functions中應允許使用pipe() ,並且它異步的。 這就是為什么你需要監聽finish下載的文件之前,事件
  • fs僅在公共NPM上已棄用,而不是您要導入的軟件包, fs (文件系統)模塊是Node的核心內置軟件包之一 也就是說,您似乎根本不需要在代碼中使用它

謝謝dpopp07,我明白了!

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成為myWordFileType 這些函數將HTTP請求發送到IBM Watson Text-to-speech,該請求返回一個回調,而不是Promise,因此代碼有點難看。 關鍵是HTTP響應通過Node命令pipe將文件發送到Google Cloud Storage命令file.createWriteStream的地方 必須將contentType設置為獲取可讀文件,但是使用auto可以使此操作變得容易。 然后將文件寫入存儲桶,該存儲桶設置到我的Firebase Cloud Storage文件夾Test_Folder ,文件名是myWordFileType

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM