正在将 API 文件下载到 Firebase 云存储?
Downloading an API file to Firebase Cloud Storage?
如何将 IBM Watson Text-to-speech 中的音频文件(约 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()
我必须以某种方式将 IBM Watson 提供的文件转换为 Firebase 云存储可识别的文件。 Google Cloud Functions 中不允许 fs
吗?
此外,第 6 行不应该是
var fs = require('fs-js');
不是
var fs = require('fs');
根据 NPM,fs
包已弃用。
Google Cloud Functions 中是否允许 pipe
?如果是这样,我应该将文件传输到什么地方?我需要这样的东西:
}).pipe(file);
file.download()
好的,查看 file.download()
的文档,我认为您只需对代码进行少量更改即可完成这项工作。 file
需要从 Google 存储库中输入 File
(您需要安装 this library). This type has a method called 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()
应该在 Google Cloud Functions 中被允许并且它 是 异步。这就是为什么你需要在下载文件之前监听 finish
事件
fs
仅在 public NPM 上被弃用,但这不是您导入的包,fs
(文件系统)模块是 Node's core built-in packages 之一。也就是说,看起来您的代码中可能根本不需要它
谢谢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
。这些函数向 IBM Watson Text-to-speech 发送一个 HTTP 请求,returns 一个回调,而不是一个承诺,所以代码有点难看。关键在于 HTTP 响应通过节点命令 pipe
将文件发送到 Google 云存储命令 file.createWriteStream。必须设置 contentType
才能获得可读文件,但是 auto
使这很容易。然后将文件写入存储桶,该存储桶设置为我的 Firebase Cloud Storage 文件夹 Test_Folder
,文件名为 myWordFileType
.
如何将 IBM Watson Text-to-speech 中的音频文件(约 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()
我必须以某种方式将 IBM Watson 提供的文件转换为 Firebase 云存储可识别的文件。 Google Cloud Functions 中不允许 fs
吗?
此外,第 6 行不应该是
var fs = require('fs-js');
不是
var fs = require('fs');
根据 NPM,fs
包已弃用。
Google Cloud Functions 中是否允许 pipe
?如果是这样,我应该将文件传输到什么地方?我需要这样的东西:
}).pipe(file);
file.download()
好的,查看 file.download()
的文档,我认为您只需对代码进行少量更改即可完成这项工作。 file
需要从 Google 存储库中输入 File
(您需要安装 this library). This type has a method called 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()
应该在 Google Cloud Functions 中被允许并且它 是 异步。这就是为什么你需要在下载文件之前监听finish
事件fs
仅在 public NPM 上被弃用,但这不是您导入的包,fs
(文件系统)模块是 Node's core built-in packages 之一。也就是说,看起来您的代码中可能根本不需要它
谢谢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
。这些函数向 IBM Watson Text-to-speech 发送一个 HTTP 请求,returns 一个回调,而不是一个承诺,所以代码有点难看。关键在于 HTTP 响应通过节点命令 pipe
将文件发送到 Google 云存储命令 file.createWriteStream。必须设置 contentType
才能获得可读文件,但是 auto
使这很容易。然后将文件写入存储桶,该存储桶设置为我的 Firebase Cloud Storage 文件夹 Test_Folder
,文件名为 myWordFileType
.