[英]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. 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.