问题
为了将生成的二进制音频文件保存到Google Cloud Storage (),我正在使用Google Text To Speech ()。
在Firebase的Cloud Functions环境中,保存本地二进制文件似乎不是一个好主意。因此,我正在寻找如何写入和检索临时二进制文件?当我尝试检索当前创建的临时文件时,我收到一个错误。
在下面尝试的解决方案中,临时文件显示为保存在日志中,然后我尝试在文件创建回调的成功部分中检索临时文件。
错误
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",
};
});
下一步
我会尝试上传一个普通的文本文件,而不是二进制文件,看看是不是临时文件格式有问题。
https://stackoverflow.com/questions/53658279
复制相似问题