Repository: ClickHouse/ClickHouse
File: ci/praktika/s3.py (Python)

Code before:
                            current_etag = head_response.get("ETag", "").strip('"')
                            current_version = int(
                                head_response.get("Metadata", {}).get("version", "0")
                            )

                            # Verify we're updating from expected version
                            if current_version != version - 1:
                                print(
                                    f"Version mismatch: expected {version - 1}, found {current_version} (concurrent write detected)"
                                )
                                return False

                            # Read file content for put_object (needed for If-Match)
                            with open(local_path, "rb") as f:
                                client.put_object(
                                    Bucket=bucket,
                                    Key=key,
                                    Body=f.read(),
                                    ContentType=(
                                        "text/plain"
                                        if text
                                        else "application/octet-stream"
                                    ),
                                    Metadata={"version": str(version)},
                                    IfMatch=current_etag,
                                )
                        except ClientError as e:
                            error_code = e.response.get("Error", {}).get("Code", "")
                            if error_code == "PreconditionFailed":
                                print(
                                    f"Precondition failed: object was modified by another process"
                                )
                                return False
                            raise

                    print(f"Uploaded file with version {version} using boto3")
                    StorageUsage.add_uploaded(local_path)
                    return True

                except ClientError as e:
                    error_code = e.response.get("Error", {}).get("Code", "")
                    if error_code == "PreconditionFailed":
                        print("Precondition failed (concurrent write detected)")
                        return False
                    print(f"ERROR: Failed to upload file using boto3: {error_code}")
                    if not no_strict:
                        raise
                    return False
                except Exception as e:
                    print(f"ERROR: Failed to upload file using boto3: {e}")
                    if not no_strict:

Review comment (suggestion):
This loads the entire file into memory via `f.read()` before uploading. Since `Body` can be a file-like object, pass `f` directly to `Body` to stream the upload and avoid memory spikes (and potential issues if the file grows).

Code after:
                    if version == 0:
                        # DESTRUCTIVE: Version 0 overwrites without conditions (NOT safe for concurrent use)
                        print(
                            f"Uploading file with version 0 (destructive reset) using boto3"
                        )
                        client.upload_file(
                            str(local_path), bucket, key, ExtraArgs=extra_args
                        )
                    else:
                        # For version > 0, use conditional PUT with If-Match (ETag check)
                        # First get current ETag
                        try:
                            head_response = client.head_object(Bucket=bucket, Key=key)
                            current_etag = head_response.get("ETag", "").strip('"')
                            current_version = int(
                                head_response.get("Metadata", {}).get("version", "0")
                            )

                            # Verify we're updating from expected version
                            if current_version != version - 1:
                                print(
                                    f"Version mismatch: expected {version - 1}, found {current_version} (concurrent write detected)"
                                )
                                return False

                            # Stream file content for put_object (needed for If-Match)
                            with open(local_path, "rb") as f:
                                client.put_object(
                                    Bucket=bucket,
                                    Key=key,
                                    Body=f,
                                    ContentType=(
                                        "text/plain"
                                        if text
                                        else "application/octet-stream"
                                    ),
                                    Metadata={"version": str(version)},
                                    IfMatch=current_etag,
                                )
                        except ClientError as e:
                            error_code = e.response.get("Error", {}).get("Code", "")
                            if error_code == "PreconditionFailed":
                                print(
                                    f"Precondition failed: object was modified by another process"
                                )
                                return False
                            raise

                    print(f"Uploaded file with version {version} using boto3")
                    StorageUsage.add_uploaded(local_path)
                    return True