繁体   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