Downloading an API file to Firebase Cloud Storage?











up vote
0
down vote

favorite












How do I save an audio file (about 10K) from IBM Watson Text-to-speech to Firebase Cloud Storage? Here's my code, copied from the 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);
});
});


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. Is fs not allowed in Google Cloud Functions?



Also, shouldn't line 6 be



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


not



var fs = require('fs'); 


According to NPM the fs package is deprecated.



Is pipe allowed in Google Cloud Functions? If so, what do I pipe the file to? I need something that looks like this:



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









share|improve this question
























  • I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
    – dpopp07
    Nov 7 at 23:38










  • You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
    – Doug Stevenson
    Nov 8 at 0:37










  • Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
    – Thomas David Kehoe
    Nov 8 at 15:41










  • According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
    – Thomas David Kehoe
    Nov 8 at 15:42










  • Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
    – Thomas David Kehoe
    Nov 8 at 15:44















up vote
0
down vote

favorite












How do I save an audio file (about 10K) from IBM Watson Text-to-speech to Firebase Cloud Storage? Here's my code, copied from the 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);
});
});


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. Is fs not allowed in Google Cloud Functions?



Also, shouldn't line 6 be



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


not



var fs = require('fs'); 


According to NPM the fs package is deprecated.



Is pipe allowed in Google Cloud Functions? If so, what do I pipe the file to? I need something that looks like this:



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









share|improve this question
























  • I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
    – dpopp07
    Nov 7 at 23:38










  • You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
    – Doug Stevenson
    Nov 8 at 0:37










  • Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
    – Thomas David Kehoe
    Nov 8 at 15:41










  • According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
    – Thomas David Kehoe
    Nov 8 at 15:42










  • Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
    – Thomas David Kehoe
    Nov 8 at 15:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How do I save an audio file (about 10K) from IBM Watson Text-to-speech to Firebase Cloud Storage? Here's my code, copied from the 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);
});
});


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. Is fs not allowed in Google Cloud Functions?



Also, shouldn't line 6 be



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


not



var fs = require('fs'); 


According to NPM the fs package is deprecated.



Is pipe allowed in Google Cloud Functions? If so, what do I pipe the file to? I need something that looks like this:



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









share|improve this question















How do I save an audio file (about 10K) from IBM Watson Text-to-speech to Firebase Cloud Storage? Here's my code, copied from the 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);
});
});


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. Is fs not allowed in Google Cloud Functions?



Also, shouldn't line 6 be



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


not



var fs = require('fs'); 


According to NPM the fs package is deprecated.



Is pipe allowed in Google Cloud Functions? If so, what do I pipe the file to? I need something that looks like this:



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






node.js google-cloud-functions firebase-storage text-to-speech ibm-watson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 0:07

























asked Nov 7 at 22:57









Thomas David Kehoe

1,88211333




1,88211333












  • I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
    – dpopp07
    Nov 7 at 23:38










  • You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
    – Doug Stevenson
    Nov 8 at 0:37










  • Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
    – Thomas David Kehoe
    Nov 8 at 15:41










  • According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
    – Thomas David Kehoe
    Nov 8 at 15:42










  • Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
    – Thomas David Kehoe
    Nov 8 at 15:44


















  • I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
    – dpopp07
    Nov 7 at 23:38










  • You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
    – Doug Stevenson
    Nov 8 at 0:37










  • Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
    – Thomas David Kehoe
    Nov 8 at 15:41










  • According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
    – Thomas David Kehoe
    Nov 8 at 15:42










  • Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
    – Thomas David Kehoe
    Nov 8 at 15:44
















I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
– dpopp07
Nov 7 at 23:38




I can help but I have a question first - what type of variable do you need file to be? A Buffer? I mostly want to know, where is that download() function coming from - or is that pseudocode?
– dpopp07
Nov 7 at 23:38












You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
– Doug Stevenson
Nov 8 at 0:37




You need to return a promise from your function that resolves when all the work is complete. pipe is probably an async function, which means you need to start thinking more carefully about how you want to do async programming in your function.
– Doug Stevenson
Nov 8 at 0:37












Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
– Thomas David Kehoe
Nov 8 at 15:41




Doug, IBM Watson textToSpeech.synthesize returns a callback, not a promise. I'll look into Google text-to-speech, if that returns a promise will it be easier to code?
– Thomas David Kehoe
Nov 8 at 15:41












According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
– Thomas David Kehoe
Nov 8 at 15:42




According to stackoverflow.com/questions/20085513/using-pipe-in-node-js-net, "The pipe() function [in Node] reads data from a readable stream as it becomes available and writes it to a destination writable stream." I want to write to a file. I'll look into whether there's a pipe-to-file Node command.
– Thomas David Kehoe
Nov 8 at 15:42












Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
– Thomas David Kehoe
Nov 8 at 15:44




Another answer in stackoverflow.com/questions/20085513/using-pipe-in-node-js-net says that "pipe() reads from a readable stream and writes to a writeable stream, much like a Unix pipe. It does all "reasonable" things along the way with errors, end of files, if one side falls behind etc." That sounds like async.
– Thomas David Kehoe
Nov 8 at 15:44












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Okay, reviewing the documentation for file.download(), I think you can make this work with little change to your code. file needs to be type File from the Google Storage library (you'll need to install this library). This type has a method called createWriteStream that you can stream the results of synthesize to. 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. This is why you need to listen for the finish event before downloading the file


  • 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. That said, it looks like you might not need it in your code at all






share|improve this answer





















  • Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
    – Thomas David Kehoe
    Nov 8 at 23:10












  • Thanks again, I've written up the working code as an answer and upvoted your answer.
    – Thomas David Kehoe
    Nov 9 at 0:22




















up vote
1
down vote



accepted










Thanks dpopp07, I got it!



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. Adding .ogg makes 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. 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. The contentType must be set to get a readable file, but auto makes this easy. The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199168%2fdownloading-an-api-file-to-firebase-cloud-storage%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Okay, reviewing the documentation for file.download(), I think you can make this work with little change to your code. file needs to be type File from the Google Storage library (you'll need to install this library). This type has a method called createWriteStream that you can stream the results of synthesize to. 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. This is why you need to listen for the finish event before downloading the file


    • 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. That said, it looks like you might not need it in your code at all






    share|improve this answer





















    • Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
      – Thomas David Kehoe
      Nov 8 at 23:10












    • Thanks again, I've written up the working code as an answer and upvoted your answer.
      – Thomas David Kehoe
      Nov 9 at 0:22

















    up vote
    1
    down vote













    Okay, reviewing the documentation for file.download(), I think you can make this work with little change to your code. file needs to be type File from the Google Storage library (you'll need to install this library). This type has a method called createWriteStream that you can stream the results of synthesize to. 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. This is why you need to listen for the finish event before downloading the file


    • 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. That said, it looks like you might not need it in your code at all






    share|improve this answer





















    • Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
      – Thomas David Kehoe
      Nov 8 at 23:10












    • Thanks again, I've written up the working code as an answer and upvoted your answer.
      – Thomas David Kehoe
      Nov 9 at 0:22















    up vote
    1
    down vote










    up vote
    1
    down vote









    Okay, reviewing the documentation for file.download(), I think you can make this work with little change to your code. file needs to be type File from the Google Storage library (you'll need to install this library). This type has a method called createWriteStream that you can stream the results of synthesize to. 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. This is why you need to listen for the finish event before downloading the file


    • 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. That said, it looks like you might not need it in your code at all






    share|improve this answer












    Okay, reviewing the documentation for file.download(), I think you can make this work with little change to your code. file needs to be type File from the Google Storage library (you'll need to install this library). This type has a method called createWriteStream that you can stream the results of synthesize to. 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. This is why you need to listen for the finish event before downloading the file


    • 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. That said, it looks like you might not need it in your code at all







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 22:20









    dpopp07

    27626




    27626












    • Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
      – Thomas David Kehoe
      Nov 8 at 23:10












    • Thanks again, I've written up the working code as an answer and upvoted your answer.
      – Thomas David Kehoe
      Nov 9 at 0:22




















    • Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
      – Thomas David Kehoe
      Nov 8 at 23:10












    • Thanks again, I've written up the working code as an answer and upvoted your answer.
      – Thomas David Kehoe
      Nov 9 at 0:22


















    Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
    – Thomas David Kehoe
    Nov 8 at 23:10






    Thanks, I was just reading the documentation and saw file.createWriteStream. Running your code I get an error message "file is not defined" at the file.createWriteStream line. The documentation includes "const file = myBucket.file('my-file');". I'll keep working at it.
    – Thomas David Kehoe
    Nov 8 at 23:10














    Thanks again, I've written up the working code as an answer and upvoted your answer.
    – Thomas David Kehoe
    Nov 9 at 0:22






    Thanks again, I've written up the working code as an answer and upvoted your answer.
    – Thomas David Kehoe
    Nov 9 at 0:22














    up vote
    1
    down vote



    accepted










    Thanks dpopp07, I got it!



    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. Adding .ogg makes 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. 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. The contentType must be set to get a readable file, but auto makes this easy. The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Thanks dpopp07, I got it!



      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. Adding .ogg makes 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. 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. The contentType must be set to get a readable file, but auto makes this easy. The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Thanks dpopp07, I got it!



        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. Adding .ogg makes 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. 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. The contentType must be set to get a readable file, but auto makes this easy. The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType.






        share|improve this answer














        Thanks dpopp07, I got it!



        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. Adding .ogg makes 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. 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. The contentType must be set to get a readable file, but auto makes this easy. The file is then written to the bucket, which is set to my Firebase Cloud Storage folder Test_Folder and the filename is myWordFileType.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 16:36

























        answered Nov 9 at 0:09









        Thomas David Kehoe

        1,88211333




        1,88211333






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199168%2fdownloading-an-api-file-to-firebase-cloud-storage%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini