Skip to content

fix(s3): finalize multipart uploads with exactly the selected parts - #14601

Merged
TorstenDittmann merged 3 commits into
mainfrom
torsten/determined-euler-ol3e4h
Oct 16, 2026
Merged

TorstenDittmann merged 3 commits into
mainfrom
torsten/determined-euler-ol3e4h

Conversation

@TorstenDittmann

@TorstenDittmann TorstenDittmann commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Someone reported that S3 multipart uploads failed when they tested them with files-sdk. I ran files-sdk 3.1.1 (@aws-sdk/client-s3 / @aws-sdk/lib-storage 3.1152.0) against a local Appwrite, on both the Local storage device and the S3 storage device (with a RustFS backend). This PR fixes two bugs in how the gateway finalizes multipart uploads. Both appear only on the S3 storage device, which is what Cloud uses.

1. Single-part uploads failed with EntityTooSmall. When CompleteMultipartUpload received one part, the gateway uploaded an empty part 2 before finalizing. That made part 1 a non-final part, and S3 backends reject non-final parts under 5 MiB with EntityTooSmall. files-sdk resumable uploads (control: new UploadControl()) send one part for any file smaller than a part, so all of those failed. The padding isn't needed, because both devices already finalize a prepared one-chunk upload: Local::finalize() joins the single part (whole = false), and S3::finalize() completes with it. The gateway now finalizes with the selected part count, and uploadEmptyMultipartFinalChunk() is removed.

2. Unselected parts were stored (found by review). S3 allows completing with fewer parts than were uploaded. The S3 device completed its backend upload with every part in its metadata, so the stored bytes included parts the client did not select, while the recorded size and ETag counted only the selected ones. The gateway now limits the device parts to the selected part numbers before finalizing.

Tests. tests/e2e/Services/Storage/S3CustomServerTest.php adds the first e2e tests for the S3 gateway. They send SigV4-signed requests to /v1/s3 and cover:

  • a single part smaller than 5 MiB
  • completing with parts 1–2 of 3 uploaded parts
  • failure cases: completing an upload that was already completed, a wrong ETag, and parts that don't start at 1

They are tagged s3, so the Storage lane runs them on the Local device and the S3 Storage lane runs them on MinIO.

Not changed here (needs a decision): Swoole's package_max_length in app/http.php is 12 MiB. Any S3 request body above that, whether a single PutObject or an UploadPart with a larger partSize, gets a bare 413 from Swoole with no S3 XML error, and clients show it as UnknownError. The files-sdk docs and CLI examples use --part-size 16777216 (16 MiB), and that fails every part. The default 5 MiB parts work. Fixing this means raising the global limit or streaming request bodies, which has memory trade-offs, so it is left out of this PR.

Test Plan

New e2e tests (S3CustomServerTest):

Run Result
With this PR, S3 device (RustFS) ✅ 2 tests, 45 assertions
With this PR, Local device ✅ 2 tests, 45 assertions
With main's Base.php, S3 device ❌ both tests fail: 500 EntityTooSmall ("Your proposed upload is smaller than the minimum allowed object size")

files-sdk's s3() adapter against /v1/s3. Each case downloads the object back and checks it byte for byte:

Case Local before S3 device before After (both devices)
multipart: true, 12 MiB (3 × 5 MiB parts) ✅ ✅ ✅
Unknown-length ReadableStream, 12 MiB ✅ ✅ ✅
Resumable UploadControl, 12 MiB ✅ ✅ ✅
Resumable UploadControl, 1 B / 1 MB (single part) ✅ ❌ EntityTooSmall ✅
Raw SDK: upload 2 parts, complete with part 1 ✅ ❌ 10 MiB stored, expected 5 MiB ✅
Raw SDK: upload 3 parts, complete with parts 1–2 ✅ ❌ 15 MiB stored, expected 10 MiB ✅
multipart: { partSize: 16 MiB } ❌ 413 (unchanged, see above)

Static checks on the changed files:

  • pint --test with the repo pint.json: clean
  • phpstan analyse -c phpstan.neon: clean
  • Rector dry-run with tests/tools/rector.php: clean
  • phpunit tests/unit/Platform/Modules/S3: OK (41 tests)

Related PRs and Issues

  • None

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (N/A, no API metadata change)

🤖 Generated with Claude Code

https://claude.ai/code/session_017yqpHr9VatgPv6T5nEXmrB

@luxor-code-review

luxor-code-review Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This change deletes the uploadEmptyMultipartFinalChunk padding path in the S3 module base and instead always finalizes the multipart upload with the actual number of uploaded parts. Small single-part uploads now pass S3's non-final-part size rule correctly, and retries no longer stage an extra empty part.

Files Change
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Removes the empty-final-chunk upload and single finalization path; finalize is called with the real chunk count and ETag computation stays consistent with the uploaded parts.

Reviewed commit: 075be7e

@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This change finalizes S3 multipart uploads using the submitted part count and restricts device metadata to the submitted parts. It also adds signed-request end-to-end coverage for small single-part uploads, selected-part completion, and invalid completion requests.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Filters multipart device metadata to selected part numbers and removes the empty-part padding path.
tests/e2e/Services/Storage/S3CustomServerTest.php Adds SigV4 end-to-end tests for multipart completion and failure cases.

Reviewed 5e0424e · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@TorstenDittmann TorstenDittmann changed the title fix(s3): complete single-part multipart uploads without an empty padding part fix(s3): finalize multipart uploads with exactly the selected parts Oct 16, 2026
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → torsten/determined-euler-ol3e4h (after).

Metric Before After Change
🚀 Requests/sec 209.5 201.63 ⚪ -3.8%
⏱️ Latency P50 82.57 ms 86.52 ms ⚪ +4.8%
⏱️ Latency P95 188.61 ms 198.69 ms 🔴 +5.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 86.52 198.69 12,540 201.63 +10.08
Account 160.87 290.78 660 11.36 +10.58
TablesDB 82.63 150.1 6,820 112.64 +4.94
Storage 82.76 187.85 3,300 55.97 +4.73
Functions 123.52 240.49 1,760 30.69 +15.25

Top API waits (after)

API request Max wait (ms)
account.name.update 476.31
functions.create 476.04
account.prefs.update 454.56
tablesdb.rows.create 376.51
functions.variables.update 343.04

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

claude and others added 3 commits October 16, 2026 13:33
…ing part

CompleteMultipartUpload with one part uploaded an empty part 2 before
finalizing. On S3 storage devices that turns part 1 into a non-final
part, so any single-part upload under 5 MiB failed with EntityTooSmall
("Your proposed upload is smaller than the minimum allowed object
size"). files-sdk resumable uploads hit this for every file smaller
than one part.

Both devices finalize a prepared one-chunk upload directly (Local joins
the single part, S3 completes with it), so finalize with the selected
part count instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017yqpHr9VatgPv6T5nEXmrB
Co-Authored-By: Torsten Dittmann <[email protected]>
… upload

S3 lets CompleteMultipartUpload select fewer parts than were uploaded.
The S3 storage device completes its backend upload with every part in
the device metadata, so an object completed with parts 1-2 of 3 stored
part 3's bytes too, while its size and ETag counted only the selected
parts. Keep only the selected parts before finalizing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017yqpHr9VatgPv6T5nEXmrB
Co-Authored-By: Torsten Dittmann <[email protected]>
Adds SigV4-signed e2e tests for CompleteMultipartUpload: a single part
smaller than 5 MiB, completion with a subset of the uploaded parts, and
the failure cases (completed upload, wrong ETag, parts not starting at
1). Tagged `s3` so the S3 Storage lane runs them against MinIO, where
both bugs fixed on this branch failed with EntityTooSmall.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017yqpHr9VatgPv6T5nEXmrB
Co-Authored-By: Torsten Dittmann <[email protected]>
@TorstenDittmann
TorstenDittmann force-pushed the torsten/determined-euler-ol3e4h branch from 2281515 to 5e0424e Compare October 16, 2026 13:33
@TorstenDittmann
TorstenDittmann merged commit 90900d7 into main Oct 16, 2026
30 checks passed
@TorstenDittmann
TorstenDittmann deleted the torsten/determined-euler-ol3e4h branch October 16, 2026 14:27
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

3 participants