Node.js调用Google翻译API翻译Cloud Storage中docx文件未输出结果求助
Google翻译API文档翻译结果处理问题解答
问题背景
我用Node.js调用Google翻译API翻译Cloud Storage里的docx文件,确认API已读取文件,但未在Cloud Storage找到翻译结果,返回的响应里有Buffer数据,想知道怎么读取这个数据,以及为什么结果没存到云存储。
代码如下:
const projectId = 'audio-testo'; const location = 'global'; const inputUri = 'gs://bucketuniv/le2.docx'; const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1; const translationClient = new TranslationServiceClient(); const documentInputConfig = { gcsSource: { inputUri: inputUri, }, }; async function translateDocument() { // Construct request const request = { parent: translationClient.locationPath(projectId, location), documentInputConfig: documentInputConfig, sourceLanguageCode: 'en-US', targetLanguageCode: 'it-IT', }; // Run request const [response] = await translationClient.translateDocument(request); console.log(response); } translateDocument();
返回结果:
{ documentTranslation: { byteStreamOutputs: [ <Buffer 50 4b 03 04 0a 00 00 00 08 00 c3 8a 19 59 d6 64 b3 51 ed 00 00 00 31 03 00 00 1c 00 00 00 77 6f 72 64 2f 5f 72 65 6c 73 2f 64 6f 63 75 6d 65 6e 74 2e ... 14849 more bytes> ], mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', detectedLanguageCode: '' }, glossaryDocumentTranslation: null, model: 'projects/654808374327/locations/global/models/general/nmt', glossaryConfig: null }
解答
1. 为什么翻译结果未输出到Cloud Storage?
Translate Document API默认不会自动将结果写入Cloud Storage,而是以字节流形式返回响应。如果要让API直接把翻译后的文件存到你的云存储桶,需要在请求参数中添加documentOutputConfig字段,指定GCS的输出路径。
修改后的请求代码示例:
async function translateDocument() { const request = { parent: translationClient.locationPath(projectId, location), documentInputConfig: documentInputConfig, sourceLanguageCode: 'en-US', targetLanguageCode: 'it-IT', // 添加输出配置,指定云存储路径 documentOutputConfig: { gcsDestination: { outputUri: 'gs://bucketuniv/le2_translated.docx' // 替换为你的目标路径 } } }; const [response] = await translationClient.translateDocument(request); console.log('翻译结果已保存到:', response.documentTranslation.gcsOutputUri); }
2. 如何读取返回的Buffer数据?
响应里byteStreamOutputs数组中的Buffer就是完整的翻译后docx文件内容,你可以通过以下两种方式处理:
方式一:写入本地文件
使用Node.js内置的fs模块将Buffer写入本地:
const fs = require('fs').promises; async function translateDocument() { const [response] = await translationClient.translateDocument(request); const translatedBuffer = response.documentTranslation.byteStreamOutputs[0]; // 写入本地文件 await fs.writeFile('./translated_file.docx', translatedBuffer); console.log('翻译文件已保存到本地'); }
方式二:上传到Cloud Storage
如果需要手动上传到云存储,使用@google-cloud/storage包:
const {Storage} = require('@google-cloud/storage'); const storage = new Storage({projectId: projectId}); const bucket = storage.bucket('bucketuniv'); // 替换为你的桶名 async function translateDocument() { const [response] = await translationClient.translateDocument(request); const translatedBuffer = response.documentTranslation.byteStreamOutputs[0]; const file = bucket.file('le2_translated.docx'); // 目标文件名 await file.save(translatedBuffer); console.log('翻译文件已上传到Cloud Storage'); }
内容的提问来源于stack exchange,提问作者Antilope
相关产品推荐
相关产品推荐

