Repository navigation
fix: stream large file uploads to S3 instead of loading into memory - #12660
gouthamx67 wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes memory exhaustion on large single-chunk S3 uploads by streaming via
Confidence Score: 1/5The Docker build will fail on every run until the path bug in apply.php is fixed; the feature cannot be tested or shipped as-is. The patches/apply.php line 12 — the default S3.php path uses Important Files Changed
Reviews (1): Last reviewed commit: "fix: stream large file uploads to S3 ins..." | Re-trigger Greptile |
| * since they inherit uploadChunk from the base S3 class. | ||
| */ | ||
|
|
||
| $file = $argv[1] ?? __DIR__ . '/../vendor/utopia-php/storage/src/Storage/Device/S3.php'; |
There was a problem hiding this comment.
Wrong default path when run from
/tmp/ — Docker build will always fail
__DIR__ resolves to the directory of the PHP script file, which is /tmp when copied there by Docker. So the computed default path becomes /tmp/../vendor/... → /vendor/utopia-php/storage/src/Storage/Device/S3.php, which does not exist. file_get_contents returns false, the script writes "Cannot read …" to STDERR and calls exit(1), causing RUN php /tmp/apply.php to fail and break the Docker build entirely.
The path was designed to work when run directly from the repo root (where __DIR__ would be <repo>/patches and ../vendor/… would correctly resolve). The fix is either to use getcwd() instead of __DIR__, or to copy the script next to the vendor directory and run it from there.
| $file = $argv[1] ?? __DIR__ . '/../vendor/utopia-php/storage/src/Storage/Device/S3.php'; | |
| $file = $argv[1] ?? getcwd() . '/vendor/utopia-php/storage/src/Storage/Device/S3.php'; |
| if ($code === $original) { | ||
| fwrite(STDERR, "Patch had no effect — source file may already be patched or format differs.\n"); | ||
| exit(1); | ||
| } |
There was a problem hiding this comment.
Partial patch application goes undetected
The guard $code === $original only catches the case where none of the six str_replace calls matched anything. If a future upstream formatting change (e.g. a trailing space, indent style change, or small refactor) causes even one of the six patterns to silently miss, the file is written in a partially-patched state — correct S3 streaming is not guaranteed, but the build succeeds and no error is raised. Consider tracking a success counter across all six replacements and failing if it isn't exactly 6.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing as stale since there has been no activity for a while. Thanks for the contribution! If this is still relevant, feel free to rebase on |
Problem
S3::uploadChunk()callsfile_get_contents()to load the entire file into memorybefore uploading. For files >500MB, this causes PHP memory exhaustion.
Solution
For single-chunk uploads (chunk==1 && chunks==1), bypass the in-memory path and
stream directly via cURL
CURLOPT_INFILE. Multipart uploads continue using theexisting in-memory path since chunks are already sized to fit.
Changes
patches/apply.php— Patch that modifiesvendor/utopia-php/storage/src/Storage/Device/S3.phpat build time to add file streaming support
Dockerfile— Run the patch during Docker buildThe patch makes these changes to
S3.php:uploadChunk()— Single-chunk uploads callcall()with a file path for streamingcall()— New optional$filePathparameter. When set: computes hashes viahash_file,streams via
CURLOPT_INFILE, rewinds handle on retry, closes handle in finallyBackward Compatibility
Fully compatible. The
$filePathparameter defaults tonull, preserving existingbehavior for all non-S3 code paths.