質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

1回答

5270閲覧

GCP Cloud Functionsの'connection error'の原因が不明

komicool

総合スコア24

Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

1クリップ

投稿2018/05/04 23:49

編集2018/05/05 01:07

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};

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

process.exit(0)を呼んでしまっているため、cloud functionsのruntimeと接続が切れてしまっていると考えられます。

cloud functionsのバックグラウンド関数では正常終了後にはcallback関数を呼び出し、cloud functions側に終了を通知する必要があります。

今回のコードですと、process.exit(0)をしている場所はreturn callback();に、
また、console.log('File saved.');の後にも、
callback()を呼び出して下さい。

また、catchの中でも、異常終了を通知する為にcallback(errorMessage)を呼び出して下さい。

ドキュメント

投稿2018/05/12 07:39

編集2018/05/12 07:42
soundTricker

総合スコア137

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

komicool

2018/05/16 13:36

コード自体は機能するようになったのですが、正常な終了のさせ方についてはよくわからず継続して悩んでいたので個人的にとてもありがたい回答でした。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問