Bug: S3Object.closed is False after close() for gzip, and makes S3 calls for zip
Expected Behaviour
After S3Object.close(), S3Object.closed is True. Checking closed does not do any work against S3.
Current Behaviour
S3Object.closed is implemented as self.transformed_stream.closed, and transformed_stream is created lazily. That has two side effects:
- For an object that was never read,
close()only closes the raw stream. A later.closedthen builds the transformed stream on top of the already closed raw stream. Withis_gzip=Trueit returnsFalsefor an object that was just closed. - With a transformation that reads on creation, checking
.closedissues S3 requests.ZipTransformreads the archive's central directory, so.closedcallsHeadObjectandGetObject.
Code snippet
import boto3
from aws_lambda_powertools.utilities.streaming import S3Object
client = boto3.client("s3", region_name="us-east-1", aws_access_key_id="x", aws_secret_access_key="x")
obj = S3Object("bucket", "key", boto3_client=client, is_gzip=True)
obj.close()
print(obj.closed) # False, expected True
For the zip case, use a botocore Stubber with no queued responses: obj.transform(ZipTransform(), in_place=True); obj.closed raises UnStubbedResponseError for HeadObject.
Possible Solution
When the transformed stream has not been created yet, answer from the raw stream (self.raw_stream.closed) instead of creating it. I have a small fix with tests ready.
Steps to Reproduce
Run the snippet above.
Powertools for AWS Lambda (Python) version
develop (1d4f1457, 3.36.0)
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Disclosure: I found this while reviewing the streaming utility with the help of an AI assistant (Claude Code). I ran the snippets above myself against develop.
Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
This is now released under 3.37.0 version!