lordgamez commented on code in PR #2167: URL: https://github.com/apache/nifi-minifi-cpp/pull/2167#discussion_r3195522565
########## 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: This stream is directly passed to the AWS SDK. Minifi is not handling the error in case of read failure, but the AWS SDK does, and it checks the state instead of handling and exception. I tested that in case of read failure AWS returns a failure with AWS_IO_STREAM_READ_FAILED with this solution so it should be okay. ########## 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: We do not have support for AWS docker tests for Windows yet, so it is not possible. We also tried testing large >1GB flow files in the past and it did not work in the CI environment. This test should be considered in the future, but not viable at the moment -- 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]
