Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Function response stream

I have a script that is building a zip file on demand. It's working great locally using the Cloud Functions Local Emulator. When deployed, it's not streaming the response. The whole zip response is built, then sent. I know this is happening because the content-length response header is being set, which is not happening locally.

How can I get Cloud Functions to stream the response?

exports.zip = (req, res) => {
    const zipRequest = {
        media: [{
            'url': 'https://storage.googleapis.com/...',
            'file': 'file1.jpg'
        }, {
            'url': 'https://storage.googleapis.com/...',
            'file': 'file2.jpg'
        }, {
            'url': 'https://storage.googleapis.com/...',
            'file': 'file3.jpg'
        }],
        filename: 'photos.zip'
    };

    res.attachment(zipRequest.filename);
    res.setHeader('Content-Type', 'application/zip');

    let zip = Archiver('zip');
    zip.on('end', () => {
      res.end();
    });

    zip.pipe(res);
    zipRequest.media.forEach((file) => {
        zip.append(request.get(file.url), { name: file.name });
    });

    zip.finalize();
};
like image 450
Adam Avatar asked Apr 26 '26 09:04

Adam


1 Answers

Currently there is no way to stream the response as it will be buffering the response due to how the infrastructure is implemented and there's no way around it right now, which means it would not see the results until the response is closed.

like image 57
JL-V589 Avatar answered Apr 28 '26 03:04

JL-V589



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!