简体   繁体   English

将API文件下载到Firebase Cloud Storage?

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

How do I save an audio file (about 10K) from IBM Watson Text-to-speech to Firebase Cloud Storage? 如何将IBM Watson Text-to-speech中的音频文件(大约10K)保存到Firebase Cloud Storage? Here's my code, copied from the IBM Watson documentation : 这是我的代码,从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);
  });
});

The missing code is between 缺少的代码介于

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

and

file.download()

Somehow I have to convert the file provided by IBM Watson into a file that Firebase Cloud Storage recognizes. 我必须以某种方式将IBM Watson提供的文件转换为Firebase Cloud Storage可以识别的文件。 Is fs not allowed in Google Cloud Functions? Google Cloud Functions中不允许使用fs吗?

Also, shouldn't line 6 be 另外,第六行不应该是

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

not

var fs = require('fs'); 

According to NPM the fs package is deprecated. 根据NPM ,不建议使用fs软件包。

Is pipe allowed in Google Cloud Functions? Google Cloud Functions允许使用pipe吗? If so, what do I pipe the file to? 如果是这样,我该通过什么管道传输文件? I need something that looks like this: 我需要这样的东西:

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

Okay, reviewing the documentation for file.download() , I think you can make this work with little change to your code. 好的,请查阅file.download()的文档,我认为您可以对代码进行一点改动就可以完成这项工作。 file needs to be type File from the Google Storage library (you'll need to install this library ). file必须为Google存储库中的File类型(您需要安装此库 )。 This type has a method called createWriteStream that you can stream the results of synthesize to. 此类型具有一个称为createWriteStream的方法,您可以将synthesize结果流式传输到该synthesize I didn't test this but I believe it should be correct or should at least point you in the right direction: 我没有对此进行测试,但我相信它应该是正确的,或者至少应该为您指明正确的方向:

// 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);
        });
    });
});

For the record: 作为记录:

  • pipe() should be allowed in Google Cloud Functions and it is async. Google Cloud Functions中应允许使用pipe() ,并且它异步的。 This is why you need to listen for the finish event before downloading the file 这就是为什么你需要监听finish下载的文件之前,事件
  • fs is only deprecated on public NPM but that is not the package you were importing, the fs (file system) module is one of Node's core built-in packages . fs仅在公共NPM上已弃用,而不是您要导入的软件包, fs (文件系统)模块是Node的核心内置软件包之一 That said, it looks like you might not need it in your code at all 也就是说,您似乎根本不需要在代码中使用它

Thanks dpopp07, I got it! 谢谢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;
    });

The function triggers when a new word is written to a Firestore location, then extracts the word and calls it myWord . 当将新单词写入Firestore位置时,该函数触发,然后提取该单词并将其myWord Adding .ogg makes myWordFileType . 添加.ogg将使myWordFileType成为myWordFileType The functions sends an HTTP request to IBM Watson Text-to-speech, which returns a callback, not a promise, so the code is a bit ugly. 这些函数将HTTP请求发送到IBM Watson Text-to-speech,该请求返回一个回调,而不是Promise,因此代码有点难看。 The crux is where the HTTP response goes through Node command pipe to the send the file to the Google Cloud Storage command file.createWriteStream . 关键是HTTP响应通过Node命令pipe将文件发送到Google Cloud Storage命令file.createWriteStream的地方 The contentType must be set to get a readable file, but auto makes this easy. 必须将contentType设置为获取可读文件,但是使用auto可以使此操作变得容易。 The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType . 然后将文件写入存储桶,该存储桶设置到我的Firebase Cloud Storage文件夹Test_Folder ,文件名是myWordFileType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用http cloud云功能从Firebase云存储下载文件时,没有此类文件或目录 - no such file or directory when downloading a file from firebase cloud storage using http cloud cloud functions 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 使用Cloud Functions REST API请求将文件存储在Firebase存储中 - Store file in Firebase Storage using Cloud Functions REST API request 谷歌云存储的 firebase 云函数 API 错误 - firebase cloud function API Error with Google Cloud Storage APP Engine Google Cloud Storage - 下载文件时出现错误 500 - APP Engine Google Cloud Storage - Error 500 when downloading a file 使用 Firebase Cloud Functions 将文件上传到 Firebase 存储时出错 - Error while uploading file to Firebase Storage using Firebase Cloud Functions 从 REST API 获取图像并将其上传到 Firebase 云存储使用 Z035489FF8D324159ZEEA 功能 Cloud Storage - Getting image from REST API and uploading it to Firebase Cloud Storage using Firebase Cloud Functions 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 如何从云存储中获取文件并作为本地文件处理而无需下载? - How to get file from cloud storage and process as local file without downloading? Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js - Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM