[英]Write a file from a Google Cloud Function to Firebase Storage using Node fs.writeFile?
I'm getting audiofiles from Google Text-to-Speech and then I want to write the files to Firebase Storage. 我从Google文本语音转换获取音频文件,然后将文件写入Firebase存储。 I copied the Text-to-Speech code from this documentation and the Firebase Storage code from here . 我从本文档复制了Text-to-Speech代码,并从此处复制了Firebase Storage代码。 Here's my Google Cloud Function: 这是我的Google Cloud Function:
exports.Google_T2S = functions.firestore.document('Users/{userID}/Spanish/T2S_Request').onUpdate((change, context) => {
if (change.after.data().word != undefined) {
// Performs the Text-to-Speech request
async function test() {
try {
const word = change.after.data().word; // the text
const longLanguage = 'Spanish';
const audioFormat = '.mp3';
const fs = require('fs');
const util = require('util');
const textToSpeech = require('@google-cloud/text-to-speech'); // Imports the Google Cloud client library
const client = new textToSpeech.TextToSpeechClient(); // Creates a client
let myWordFile = word.replace(/ /g,"_"); // replace spaces with underscores in the file name
myWordFile = myWordFile.toLowerCase(); // convert the file name to lower case
myWordFile = myWordFile + audioFormat; // append .mp3 to the file name;
// boilerplate copied from https://cloud.google.com/blog/products/gcp/use-google-cloud-client-libraries-to-store-files-save-entities-and-log-data
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('myProject-cd99d.appspot.com');
const file = bucket.file('Audio/' + longLanguage + '/' + myWordFile);
const request = { // Construct the request
input: {text: word},
// Select the language and SSML Voice Gender (optional)
voice: {languageCode: 'es-ES', ssmlGender: 'FEMALE'},
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = util.promisify(fs.writeFile);
await writeFile(file, response.audioContent, 'binary');
}
catch (error) {
console.error(error);
}
}
test();
} // close if
return 0;
});
The problem is here: 问题在这里:
const file = bucket.file('Audio/' + longLanguage + '/' + myWordFile);
const writeFile = util.promisify(fs.writeFile);
await writeFile(file, response.audioContent, 'binary');
fs.writeFile expects file
to be a string
, Buffer
, URL
, or integer
. fs.writeFile期望file
为string
, Buffer
, URL
或integer
。 It doesn't like that I put in a Google Cloud Storage function. 我不喜欢加入Google Cloud Storage功能。 What should I put there? 我应该放在那儿?
Node's fs
module is only for writing the local filesystem, so you can't use it to write to Cloud Storage. Node的fs
模块仅用于写入本地文件系统,因此您不能使用它来写入Cloud Storage。 If you want to write to Cloud Storage, you're going to have to use the Cloud Storage SDK (or the Admin SDK , which wraps the Cloud Storage SDK). 如果要写入Cloud Storage,则必须使用Cloud Storage SDK (或包装Cloud Storage SDK的Admin SDK)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.