如何使用节点Js写入和检索临时二进制文件? [英] How to Write and Retrieve a Temporary Binary File With Node Js?

查看:59
本文介绍了如何使用节点Js写入和检索临时二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google文字转语音( TTS ),以便将生成的二进制音频文件保存到 Google云存储( GCS ).

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

在Firebase的Cloud Functions环境中,保存本地二进制文件似乎不是一个好主意.因此,我正在寻找如何编写和检索临时二进制文件?.当我尝试检索当前创建的临时文件时,会收到错误消息.

Saving a local binary file does not seem like a good idea in the Firebase's Cloud Functions environment. Therefore I'm looking how to write and retrieve a temporary binary file? When I attempt to retrieve the temporary file created currently I receive an error.

在尝试的解决方案中,临时文件显示为已保存在日志中,然后我尝试在文件创建回调的成功部分中检索临时文件.

In the attempted solution below the temp file shows as being saved in the log, then I'm attempting to retrieve the temp file in the success portion of the file creation callback.

Error: Cannot parse JSON response  
at ApiError (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:43:9)
  at Util.parseHttpRespBody (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:167:42)
  at Util.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:116:117)
  at retryRequest (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:403:22)
  at onResponse (/user_code/node_modules/@google-cloud/storage/node_modules/retry-request/index.js:200:7)
  at /user_code/node_modules/@google-cloud/storage/node_modules/teeny-request/build/src/index.js:158:17
  at process._tickDomainCallback (internal/process/next_tick.js:135:7)

尝试的解决方案

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const path = require('path');
const os = require('os');
const {Storage} = require('@google-cloud/storage');
const projectId = 'coinverse-media-staging';
const storage = new Storage({
  projectId: projectId,
});

const fs = require('fs');
const textToSpeech = require('@google-cloud/text-to-speech');

const client = new textToSpeech.TextToSpeechClient();

admin.initializeApp();

const text = 'Hello, world!';

const request = {
   input: {text: text},
   // Select the language and SSML Voice Gender (optional)
   voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
   // Select the type of audio encoding
   audioConfig: {audioEncoding: 'MP3'},
}; 

exports.getAudiocast = functions.https.onCall((data, context) => {

var bucket = storage.bucket('gs://[project-name].appspot.com/content/feeds/en-audio/');

client.synthesizeSpeech(request, (err, response) => {
    if (err) {
      console.error('ERROR:', err);
      return;
    }

    const tempFile = path.join(os.tmpdir(), (data.id + '.mp3'));

    fs.writeFile(tempFile, response.audioContent, 'binary', err => {
      if (err) {
        console.error('ERROR:', err);
        return;
      }

      console.log('Audio content written to file: ' + tempFile);

      bucket.upload(tempFile), function(err, file) {
        if (!err) {
          console.log('Audiocast uploaded!');
        } else {
          console.error('Audiocast upload error: ' + err.message);
        }
      };
    });  
});

return {
    filePath: "cloudStorage/someFilePath",
};
});

下一步

我将尝试上传普通文本文件而不是二进制文件,以查看是否存在临时文件格式.

Next Step

I will try uploading a normal text file instead of the binary file to see if the temporary file format is the issue.

推荐答案

感谢@Doug_Stevenson和@AndersonMendes提供指导!

Thanks @Doug_Stevenson and @AndersonMendes for the guidance!

我在我的 Google Cloud Storage 中同时包含了 bucket ID和文件路径,该文件路径是错误的来源.

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.

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

bucket.upload(tempFile, { destination: ("directory/someFolderName/" + fileName) }, (err, file) => {
        if (!err) {
          console.log('Audiocast uploaded!');
        } else {
          console.error('Audiocast upload error: ' + err.message);
        }
      });

这篇关于如何使用节点Js写入和检索临时二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆