remote_handle.hpp
1 /*
2  * Copyright (c) 2024-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cassert>
19 #include <cstddef>
20 #include <cstring>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 
25 #include <kvikio/defaults.hpp>
26 #include <kvikio/error.hpp>
27 #include <kvikio/parallel_operation.hpp>
28 #include <kvikio/posix_io.hpp>
29 #include <kvikio/utils.hpp>
30 
31 struct curl_slist;
32 
33 namespace kvikio {
34 
35 class CurlHandle; // Prototype
36 
43 enum class RemoteEndpointType : uint8_t {
44  AUTO,
46  S3,
51  WEBHDFS,
53  HTTP,
55 };
56 
66  protected:
67  RemoteEndpointType _remote_endpoint_type{RemoteEndpointType::AUTO};
69 
70  public:
71  virtual ~RemoteEndpoint() = default;
72 
80  virtual void setopt(CurlHandle& curl) = 0;
81 
87  virtual std::string str() const = 0;
88 
94  virtual std::size_t get_file_size() = 0;
95 
100  virtual void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) = 0;
101 
107  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
108 };
109 
113 class HttpEndpoint : public RemoteEndpoint {
114  private:
115  std::string _url;
116 
117  public:
123  HttpEndpoint(std::string url);
124 
125  ~HttpEndpoint() override = default;
126  void setopt(CurlHandle& curl) override;
127  std::string str() const override;
128  std::size_t get_file_size() override;
129  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
130 
137  static bool is_url_valid(std::string const& url) noexcept;
138 };
139 
143 class S3Endpoint : public RemoteEndpoint {
144  private:
145  std::string _url;
146  std::string _aws_sigv4;
147  std::string _aws_userpwd;
148  curl_slist* _curl_header_list{};
149 
163  static std::string unwrap_or_default(std::optional<std::string> aws_arg,
164  std::string const& env_var,
165  std::string const& err_msg = "");
166 
167  public:
183  static std::string url_from_bucket_and_object(std::string bucket_name,
184  std::string object_name,
185  std::optional<std::string> aws_region,
186  std::optional<std::string> aws_endpoint_url);
187 
196  [[nodiscard]] static std::pair<std::string, std::string> parse_s3_url(std::string const& s3_url);
197 
213  S3Endpoint(std::string url,
214  std::optional<std::string> aws_region = std::nullopt,
215  std::optional<std::string> aws_access_key = std::nullopt,
216  std::optional<std::string> aws_secret_access_key = std::nullopt,
217  std::optional<std::string> aws_session_token = std::nullopt);
218 
236  S3Endpoint(std::pair<std::string, std::string> bucket_and_object_names,
237  std::optional<std::string> aws_region = std::nullopt,
238  std::optional<std::string> aws_access_key = std::nullopt,
239  std::optional<std::string> aws_secret_access_key = std::nullopt,
240  std::optional<std::string> aws_endpoint_url = std::nullopt,
241  std::optional<std::string> aws_session_token = std::nullopt);
242 
243  ~S3Endpoint() override;
244  void setopt(CurlHandle& curl) override;
245  std::string str() const override;
246  std::size_t get_file_size() override;
247  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
248 
255  static bool is_url_valid(std::string const& url) noexcept;
256 };
257 
263  private:
264  std::string _url;
265 
266  public:
267  explicit S3EndpointWithPresignedUrl(std::string presigned_url);
268 
269  ~S3EndpointWithPresignedUrl() override = default;
270  void setopt(CurlHandle& curl) override;
271  std::string str() const override;
272  std::size_t get_file_size() override;
273  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
274 
281  static bool is_url_valid(std::string const& url) noexcept;
282 };
283 
288  private:
289  std::unique_ptr<RemoteEndpoint> _endpoint;
290  std::size_t _nbytes;
291 
292  public:
370  static RemoteHandle open(std::string url,
372  std::optional<std::vector<RemoteEndpointType>> allow_list = std::nullopt,
373  std::optional<std::size_t> nbytes = std::nullopt);
374 
381  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint, std::size_t nbytes);
382 
390  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint);
391 
392  // A remote handle is moveable but not copyable.
393  RemoteHandle(RemoteHandle&& o) = default;
394  RemoteHandle& operator=(RemoteHandle&& o) = default;
395  RemoteHandle(RemoteHandle const&) = delete;
396  RemoteHandle& operator=(RemoteHandle const&) = delete;
397 
403  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
404 
413  [[nodiscard]] std::size_t nbytes() const noexcept;
414 
420  [[nodiscard]] RemoteEndpoint const& endpoint() const noexcept;
421 
434  std::size_t read(void* buf, std::size_t size, std::size_t file_offset = 0);
435 
448  std::future<std::size_t> pread(void* buf,
449  std::size_t size,
450  std::size_t file_offset = 0,
451  std::size_t task_size = defaults::task_size());
452 };
453 
454 } // namespace kvikio
Representation of a curl easy handle pointer and its operations.
Definition: libcurl.hpp:91
A remote endpoint using http.
void setup_range_request(CurlHandle &curl, std::size_t file_offset, std::size_t size) override
Set up the range request in order to read part of a file given the file offset and read size.
std::size_t get_file_size() override
Get the size of the remote file.
std::string str() const override
Get a description of this remote point instance.
HttpEndpoint(std::string url)
Create an http endpoint from a url.
void setopt(CurlHandle &curl) override
Set needed connection options on a curl handle.
static bool is_url_valid(std::string const &url) noexcept
Whether the given URL is valid for HTTP/HTTPS endpoints.
Abstract base class for remote endpoints.
RemoteEndpointType remote_endpoint_type() const noexcept
Get the type of the remote file.
virtual std::size_t get_file_size()=0
Get the size of the remote file.
virtual void setup_range_request(CurlHandle &curl, std::size_t file_offset, std::size_t size)=0
Set up the range request in order to read part of a file given the file offset and read size.
virtual void setopt(CurlHandle &curl)=0
Set needed connection options on a curl handle.
virtual std::string str() const =0
Get a description of this remote point instance.
Handle of remote file.
RemoteEndpointType remote_endpoint_type() const noexcept
Get the type of the remote file.
static RemoteHandle open(std::string url, RemoteEndpointType remote_endpoint_type=RemoteEndpointType::AUTO, std::optional< std::vector< RemoteEndpointType >> allow_list=std::nullopt, std::optional< std::size_t > nbytes=std::nullopt)
Create a remote file handle from a URL.
RemoteHandle(std::unique_ptr< RemoteEndpoint > endpoint)
Create a new remote handle from an endpoint (infers the file size).
RemoteHandle(std::unique_ptr< RemoteEndpoint > endpoint, std::size_t nbytes)
Create a new remote handle from an endpoint and a file size.
A remote endpoint using AWS's S3 protocol and expecting a presigned URL. File access via this type of...
std::size_t get_file_size() override
Get the size of the remote file.
std::string str() const override
Get a description of this remote point instance.
void setup_range_request(CurlHandle &curl, std::size_t file_offset, std::size_t size) override
Set up the range request in order to read part of a file given the file offset and read size.
static bool is_url_valid(std::string const &url) noexcept
Whether the given URL is valid for S3 endpoints with presigned URL.
void setopt(CurlHandle &curl) override
Set needed connection options on a curl handle.
A remote endpoint using AWS's S3 protocol.
static std::string url_from_bucket_and_object(std::string bucket_name, std::string object_name, std::optional< std::string > aws_region, std::optional< std::string > aws_endpoint_url)
Get url from a AWS S3 bucket and object name.
static bool is_url_valid(std::string const &url) noexcept
Whether the given URL is valid for S3 endpoints (excluding presigned URL).
S3Endpoint(std::pair< std::string, std::string > bucket_and_object_names, std::optional< std::string > aws_region=std::nullopt, std::optional< std::string > aws_access_key=std::nullopt, std::optional< std::string > aws_secret_access_key=std::nullopt, std::optional< std::string > aws_endpoint_url=std::nullopt, std::optional< std::string > aws_session_token=std::nullopt)
Create a S3 endpoint from a bucket and object name.
S3Endpoint(std::string url, std::optional< std::string > aws_region=std::nullopt, std::optional< std::string > aws_access_key=std::nullopt, std::optional< std::string > aws_secret_access_key=std::nullopt, std::optional< std::string > aws_session_token=std::nullopt)
Create a S3 endpoint from a url.
void setopt(CurlHandle &curl) override
Set needed connection options on a curl handle.
void setup_range_request(CurlHandle &curl, std::size_t file_offset, std::size_t size) override
Set up the range request in order to read part of a file given the file offset and read size.
static std::pair< std::string, std::string > parse_s3_url(std::string const &s3_url)
Given an url like "s3://<bucket>/<object>", return the name of the bucket and object.
std::string str() const override
Get a description of this remote point instance.
std::size_t get_file_size() override
Get the size of the remote file.
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:123
KvikIO namespace.
Definition: batch.hpp:27
RemoteEndpointType
Types of remote file endpoints supported by KvikIO.