googleapis / google-cloud-php

Google Cloud Client Library for PHP
https://cloud.google.com/php/docs/reference
Apache License 2.0
1.08k stars 428 forks source link

PHP - Retry conformance in Google\Cloud\Storage\StorageClient breaks streaming large files using registerStreamWrapper #7473

Open bogdanstoik opened 1 week ago

bogdanstoik commented 1 week ago

Code:

$storage = new StorageClient([
    'projectId' => ...,
    'keyFile' => ...,
]);
$storage->registerStreamWrapper();
$f = fopen('gs://path/to/very/large/file', 'r');

Before commit a8211f2: code runs fast (<1s, regardless file size or connection speed). $f is referencing a readable stream and can be used without fully downloading the file

After commit a8211f2: code runs slow (depending on file size and connection speed), the file is fully downloaded on the client machine. $f is referencing a readable stream from the local file

Possible cause: in vendor/google/cloud-storage/src/Connection/Rest.php:350 Utils::copyToStream($fetchedStream, $resultStream); makes a full download of the stream, since registerStreamWrapper doesn't manage ['restOptions']['headers']['Range']

bshaffer commented 6 days ago

Thank you for filing this issue, and for your thorough research into the problem. I will take a look at this and see if I can't identify the issue.

bshaffer commented 5 days ago

@bogdanstoik I created a fix here. You can install it with composer by doing something like:

composer require google/cloud: dev-fix-storage-streamwrapper as 0.500.0

It will be in this week's release or next week, but we'd love to have you test it first to ensure it fixes your issue specifically!

bogdanstoik commented 4 days ago

I can confirm the patch fixes my specific issue. Thank you so much for the quick response!

bshaffer commented 4 days ago

@bogdanstoik thank YOU for notifying us of the problem and providing such a detailed issue as to make it easier to track down.