Cloud Functionsで特定のGCSバケットへのファイルアップロードをトリガーとして
そのファイルがflacファイルであれば、Speech APIを使って翻訳テキストを同じGCSバケットに
返すような処理を書こうとしています。
ただ、Function execution took 10764 ms, finished with status: 'connection error'の
ようなエラーが出てしまい、原因が全く検討がつきません。
考えうる原因についてご教示いただけますと幸いです。コードは以下の通りです。
node.js
1/** 2 * Triggered from a message on a Cloud Storage bucket. 3 * 4 * @param {!Object} event The Cloud Functions event. 5 * @param {!Function} The callback function. 6 */ 7exports.processFile = (event, callback) => { 8 9 // Imports the Google Cloud client library 10 const speech = require('@google-cloud/speech'); 11 const storage = require('@google-cloud/storage')(); 12 13 // Instantiates a client 14 const client = new speech.SpeechClient(); 15 16 // Get Information about put object of Cloud Storage 17 const uploadedObject = event.data; // The Cloud Storage object. 18 const gcsUri = 'gs://' + uploadedObject.bucket + '/' + uploadedObject.name; // Uploaded object's URI. 19 const contentType = uploadedObject.contentType; // Object's content type. 20 21 console.log('URI of processing file : ' + gcsUri); 22 console.log('Type of processing file : ' + contentType); 23 24 // Except for audio file, expire function. 25 if(contentType === undefined){ 26 console.log('Content-type of uploaded file is unclear. This function does nothing.'); 27 process.exit(0); 28 } else if(contentType.indexOf(/audio/) === -1){ 29 console.log('Uploaded file is not audio file. This function does nothing.'); 30 process.exit(0); 31 } 32 33 // Configure about GCE object which is saved translated text. 34 // TODO: Now, the Quality of naming file is slipshod. 35 const textObject = storage.bucket(object.bucket).file('translated.txt'); 36 37 // The encoding of the audio file, e.g. 'LINEAR16' 38 const encoding = 'FLAC'; 39 // This param is optional for flac. The sample rate of the audio file in hertz, e.g. 16000. 40 // const sampleRateHertz = 44100; 41 // The BCP-47 language code to use, e.g. 'en-US' 42 const languageCode = 'ja-JP'; 43 44 const config = { 45 encoding: encoding, 46 // sampleRateHertz: sampleRateHertz, 47 languageCode: languageCode 48 }; 49 50 const audio = { 51 uri: gcsUri 52 }; 53 54 const request = { 55 config: config, 56 audio: audio 57 }; 58 59 // Detects speech in the audio file. This creates a recognition job that you 60 // can wait for now, or get its result later. 61 client.longRunningRecognize(request) 62 .then((data) => { 63 const operation = data[0]; 64 // Get a Promise representation of the final result of the job 65 return operation.promise(); 66 }) 67 .then((data) => { 68 const response = data[0]; 69 const transcription = response.results.map(result => 70 result.alternatives[0].transcript).join('\n'); 71 console.log('Transcription: ${transcription}'); 72 return textObject.save(transcription) 73 }) 74 .then(() => { 75 console.log('File saved.'); 76 }) 77 .catch((err) => { 78 console.error('ERROR:', err); 79 }); 80 // callback(); 81};
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/16 13:36