Skip to content

fix: stream large file uploads to S3 instead of loading into memory - #12660

Closed
gouthamx67 wants to merge 1 commit into
appwrite:mainfrom
gouthamx67:1.9.x
Closed

gouthamx67 wants to merge 1 commit into
appwrite:mainfrom
gouthamx67:1.9.x

Conversation

@gouthamx67

Copy link
Copy Markdown

Problem

S3::uploadChunk() calls file_get_contents() to load the entire file into memory
before 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 the
existing in-memory path since chunks are already sized to fit.

Changes

  • patches/apply.php — Patch that modifies vendor/utopia-php/storage/src/Storage/Device/S3.php
    at build time to add file streaming support
  • Dockerfile — Run the patch during Docker build

The patch makes these changes to S3.php:

  • uploadChunk() — Single-chunk uploads call call() with a file path for streaming
  • call() — New optional $filePath parameter. When set: computes hashes via hash_file,
    streams via CURLOPT_INFILE, rewinds handle on retry, closes handle in finally

Backward Compatibility

Fully compatible. The $filePath parameter defaults to null, preserving existing
behavior for all non-S3 code paths.

@greptile-apps

greptile-apps Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes memory exhaustion on large single-chunk S3 uploads by streaming via CURLOPT_INFILE instead of loading the entire file with file_get_contents. The implementation is delivered as a build-time patch script (patches/apply.php) that rewrites vendor/utopia-php/storage/src/Storage/Device/S3.php during the Docker build.

  • apply.php performs six str_replace operations to modify uploadChunk() and call() in the upstream S3 class: it adds an optional $filePath parameter to call(), computes the SHA-256 hash via hash_file, streams via CURLOPT_UPLOAD/CURLOPT_INFILE, rewinds the handle on retries, and closes it in finally.
  • Dockerfile copies apply.php to /tmp and runs it immediately after the composer vendor directory is copied in — but the script uses __DIR__ (which resolves to /tmp) to build the default path to S3.php, so the vendor file is never found and the build fails unconditionally.

Confidence Score: 1/5

The 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 __DIR__ vs getcwd() mistake means the patch script immediately exits with code 1, causing RUN php /tmp/apply.php in the Dockerfile to fail. The image cannot be built, so the streaming improvement never reaches any environment. The patch logic itself is otherwise sound, but the delivery mechanism is broken.

patches/apply.php line 12 — the default S3.php path uses __DIR__ instead of getcwd(); the Dockerfile placement of the script in /tmp makes this the root cause of the build failure.

Important Files Changed

Filename Overview
patches/apply.php New patch script that modifies vendor S3.php at build time; contains a critical __DIR__ vs getcwd() path bug that causes the Docker build to fail when the script is placed in /tmp.
Dockerfile Adds two lines to copy and run apply.php after the vendor directory is in place; structurally correct, but the script it invokes uses the wrong path resolution, which will cause RUN php /tmp/apply.php to exit non-zero and fail the build.

Reviews (1): Last reviewed commit: "fix: stream large file uploads to S3 ins..." | Re-trigger Greptile

Comment thread patches/apply.php
* since they inherit uploadChunk from the base S3 class.
*/

$file = $argv[1] ?? __DIR__ . '/../vendor/utopia-php/storage/src/Storage/Device/S3.php';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 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.

Suggested change
$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';

Comment thread patches/apply.php
Comment on lines +157 to +160
if ($code === $original) {
fwrite(STDERR, "Patch had no effect — source file may already be patched or format differs.\n");
exit(1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

@HarshMN2345

Copy link
Copy Markdown
Member

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 main and reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants