Copilot commented on code in PR #2167:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2167#discussion_r3187097234


##########
extensions/aws/s3/MinifiToAwsInputStream.h:
##########
@@ -0,0 +1,105 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <algorithm>
+#include <cstdint>
+#include <iostream>
+#include <memory>
+#include <span>
+#include <vector>
+
+#include "utils/ConfigurationUtils.h"
+#include "minifi-cpp/io/InputStream.h"
+#include "minifi-cpp/utils/gsl.h"
+
+namespace org::apache::nifi::minifi::aws::s3 {
+
+class MinifiInputStreamBuf : public std::streambuf {
+ public:
+  MinifiInputStreamBuf(std::shared_ptr<io::InputStream> stream, uint64_t 
content_length, gsl::not_null<std::basic_ios<char>*> owner)
+      : stream_(std::move(stream)),
+        start_pos_(stream_->tell()),
+        content_length_(content_length),
+        buffer_(utils::configuration::DEFAULT_BUFFER_SIZE),
+        owner_(owner) {}
+
+ protected:
+  int_type underflow() override {
+    if (gptr() < egptr()) {
+      return traits_type::to_int_type(*gptr());
+    }
+    const uint64_t stream_pos = stream_->tell();
+    if (stream_pos >= start_pos_ + content_length_) {
+      return traits_type::eof();
+    }
+    const auto remaining = (start_pos_ + content_length_) - stream_pos;
+    const auto to_read = 
std::min<uint64_t>(utils::configuration::DEFAULT_BUFFER_SIZE, remaining);
+    const auto bytes_read = 
stream_->read(std::span(reinterpret_cast<std::byte*>(buffer_.data()), 
gsl::narrow<size_t>(to_read)));
+    if (io::isError(bytes_read)) {
+      owner_->setstate(std::ios_base::badbit);
+      return traits_type::eof();

Review Comment:
   Unlike the previous `readFlowFileStream` implementation, a read error from 
the underlying `io::InputStream` is now converted into `badbit`/EOF instead of 
throwing `StreamReadException`. `PutS3Object` only has explicit handling for 
`StreamReadException` (extensions/aws/processors/PutS3Object.cpp:287-298), so 
read failures here will bypass the existing error path and surface as a generic 
S3 failure instead of the processor's stream-read handling.
   



##########
extensions/aws/s3/S3Wrapper.cpp:
##########
@@ -68,32 +69,11 @@ std::string 
S3Wrapper::getEncryptionString(Aws::S3Crt::Model::ServerSideEncrypti
   return "";
 }
 
-std::shared_ptr<Aws::StringStream> S3Wrapper::readFlowFileStream(const 
std::shared_ptr<io::InputStream>& stream, uint64_t read_limit, uint64_t& 
read_size_out) {
-  std::array<std::byte, BUFFER_SIZE> buffer{};
-  auto data_stream = std::make_shared<Aws::StringStream>();
-  uint64_t read_size = 0;
-  while (read_size < read_limit) {
-    const auto next_read_size = (std::min)(read_limit - read_size, 
uint64_t{BUFFER_SIZE});
-    const auto read_ret = stream->read(std::span(buffer).subspan(0, 
next_read_size));
-    if (io::isError(read_ret)) {
-      throw StreamReadException("Reading flow file inputstream failed!");
-    }
-    if (read_ret > 0) {
-      data_stream->write(reinterpret_cast<char*>(buffer.data()), 
gsl::narrow<std::streamsize>(read_ret));
-      read_size += read_ret;
-    } else {
-      break;
-    }
-  }
-  read_size_out = read_size;
-  return data_stream;
-}
-
 std::optional<PutObjectResult> S3Wrapper::putObject(const 
PutObjectRequestParameters& put_object_params, const 
std::shared_ptr<io::InputStream>& stream, uint64_t flow_size) {
-  uint64_t read_size{};
-  auto data_stream = readFlowFileStream(stream, flow_size, read_size);
   auto request = 
createPutObjectRequest<Aws::S3Crt::Model::PutObjectRequest>(put_object_params);
-  request.SetBody(data_stream);
+  auto aws_stream = std::make_shared<MinifiToAwsInputStream>(stream, 
flow_size);
+  request.SetBody(aws_stream);
+  request.SetContentLength(static_cast<long long>(flow_size));  // 
NOLINT(runtime/int,google-runtime-int) AWS SDK expects long long for content 
length

Review Comment:
   This PR fixes the >2 GB Windows upload path by explicitly setting 
`ContentLength`, but there is still no regression assertion that the generated 
requests actually carry the expected content length. The existing PutS3Object 
tests already inspect other request fields, so adding a check here would help 
prevent this Windows-specific fix from silently regressing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to