Cloud function timing out -
edit: solved, see below
i'm using google cloud function retrieve images cloud storage. if not requests timing out
the *simplified function code:
'use-strict'; var gcs = require('@google-cloud/storage')() exports.main = function(request, response) { const bucket = "..." const key = request.query["t"] console.log("retrieving gcs: ", bucket, key) var buckethandle = gcs.bucket(bucket) buckethandle.file(key) .createreadstream() .pipe(response) }
log entries typical execution:
testing-previewer 8o2wyxtl5e98 function execution started <snip> testing-previewer z0cc9a0c0qqi retrieving gcs: <bucket> <key> testing-previewer gvc1ki6d097e function execution took 60002 ms, finished status: 'timeout' <snip>
i have verified file exists in bucket , key matches, has no effect on requests timing out or not. problem be? appreciated!
edit: solved, here how code should written
buckethandle.file(key) .createreadstream() .on("error", err => { console.log("stream error: ", err) response.status(500).end() }) .on("end", () => { console.log("stream finished successfully") response.status(200).end() }) .pipe(response)
it turns out pipe()
not handle "end"
event you; if event not handled, response never sent. bonus, if key incorrect, createreadstream()
succeed, pipe()
fail, handle "error"
event exit function invocation early
Comments
Post a Comment