How to Write and Retrieve a Temporary Binary File With Node Js?

前端 未结 3 710
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 08:26

Issue

I am working with Google Text To Speech (TTS) in order to save a generated binary audio file to Google Cloud Storage (GCS).

Saving a local binary file d

相关标签:
3条回答
  • 2021-01-22 08:47

    Thanks @Doug_Stevenson and @AndersonMendes for the guidance!

    Solution

    I was including both the bucket id to my Google Cloud Storage and the file path in the same string which was the source of the error.

    Bucket String

    var bucket = storage.bucket('gs://[projectName].appspot.com');

    GCS Upload Method

    bucket.upload(tempFile, { destination: ("directory/someFolderName/" + fileName) }, (err, file) => {
            if (!err) {
              console.log('Audiocast uploaded!');
            } else {
              console.error('Audiocast upload error: ' + err.message);
            }
          });
    
    0 讨论(0)
  • 2021-01-22 09:06

    Same answer, my code:

        const http = require('http');
        const fs = require('fs');
        const path = require('path');
        const os = require('os');
    
        var options = {
            destination: ('Audio/' + longLanguage + '/' + pronunciation + '/' + word + '.mp3'),
            contentType: 'audio/' + audioType
        };
    
        function oedPromise() {
            return new Promise(function(resolve, reject) {
              const tempFile = path.join(os.tmpdir(), (word + '.mp3'));
              const file = fs.createWriteStream(tempFile)
              http.get(apiURL, function(response) {
                response.pipe(file)
                .on('error', function(error) {
                  console.error(error);
                  reject(error);
                })
                .on('finish', function() {
                  myBucket.upload(tempFile, options)
                  .then(function(data) {
                    return;
                  })
                  .catch(error => console.error(error));
                });
              });
            });
        }
    
    0 讨论(0)
  • 2021-01-22 09:09

    I cannot comment due to lack of reputation points on this platform.

    You need to delete your temp files:

    https://firebase.google.com/docs/functions/tips#always_delete_temporary_files

    0 讨论(0)
提交回复
热议问题