fix: upload 0-byte object when closing open_s3_object in write mode - #3422
Open
hsusul wants to merge 1 commit into
Open
fix: upload 0-byte object when closing open_s3_object in write mode#3422hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #3421
Affected API
awswrangler.s3._fs.open_s3_object/awswrangler.s3Background & Root Cause
When an S3 object stream is opened in write mode (
mode="w"ormode="wb") usingopen_s3_object, and closed after writing 0 bytes (e.g. creating an empty file, writingb"", or exiting the context manager withoutwrite()),_S3ObjectBase.close()previously checked:When
self._parts_count == 0andself._buffer.tell() == 0(0 bytes written), both conditions evaluated toFalse. Consequently,put_objectwas skipped, leaving the S3 object non-existent if new, or retaining pre-existing content if overwriting.Corrected Behavior
_S3ObjectBase.close()now callsput_objectwheneverself._parts_count == 0, uploadingBody=self._buffer.getvalue()(which isb""when 0 bytes are written). This matches standard Python file semantics (open(path, "wb")), correctly creating or truncating the S3 object to 0 bytes.Local Reproduction & Verification
Added a unit test
test_open_s3_object_empty_file_writeintests/unit/test_moto.pycovering:"wb"mode without writing, verifyingdoes_object_existreturnsTrueandContentLength == 0."w"mode and writing"", verifying the object is truncated to 0 bytes (ContentLength == 0).Exact Validation Results
pytest tests/unit/test_moto.py-> 46 passed in 3.25spytest tests/unit/test_utils.py-> 18 passed in 0.02sruff format --check .-> Passedruff check .-> Passedgit diff --check-> CleanAWS Integration Tests Not Run
Live AWS service tests (requiring AWS infrastructure / credentials) were not executed. Verification was completed using the repository's unit test suite with
moto.Compatibility & Resource Ownership Behavior