remote_handle.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <cassert>
8 #include <cstddef>
9 #include <cstring>
10 #include <memory>
11 #include <optional>
12 #include <string>
13 
14 #include <kvikio/defaults.hpp>
15 #include <kvikio/error.hpp>
16 #include <kvikio/threadpool_wrapper.hpp>
17 #include <kvikio/utils.hpp>
18 
19 struct curl_slist;
20 
21 namespace kvikio {
22 
23 class CurlHandle; // Prototype
24 
31 enum class RemoteEndpointType : uint8_t {
32  AUTO,
34  S3,
37  S3_PUBLIC,
42  WEBHDFS,
44  HTTP,
46 };
47 
57  protected:
58  RemoteEndpointType _remote_endpoint_type{RemoteEndpointType::AUTO};
60 
61  public:
62  virtual ~RemoteEndpoint() = default;
63 
71  virtual void setopt(CurlHandle& curl) = 0;
72 
78  virtual std::string str() const = 0;
79 
85  virtual std::size_t get_file_size() = 0;
86 
91  virtual void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) = 0;
92 
98  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
99 };
100 
107 class HttpEndpoint : public RemoteEndpoint {
108  private:
109  std::string _url;
110 
111  public:
117  HttpEndpoint(std::string url);
118 
119  ~HttpEndpoint() override = default;
120  void setopt(CurlHandle& curl) override;
121  std::string str() const override;
122  std::size_t get_file_size() override;
123  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
124 
131  static bool is_url_valid(std::string const& url) noexcept;
132 };
133 
140 class S3Endpoint : public RemoteEndpoint {
141  private:
142  std::string _url;
143  std::string _aws_sigv4;
144  std::string _aws_userpwd;
145  curl_slist* _curl_header_list{};
146 
147  public:
163  static std::string url_from_bucket_and_object(std::string bucket_name,
164  std::string object_name,
165  std::optional<std::string> aws_region,
166  std::optional<std::string> aws_endpoint_url);
167 
176  [[nodiscard]] static std::pair<std::string, std::string> parse_s3_url(std::string const& s3_url);
177 
193  S3Endpoint(std::string url,
194  std::optional<std::string> aws_region = std::nullopt,
195  std::optional<std::string> aws_access_key = std::nullopt,
196  std::optional<std::string> aws_secret_access_key = std::nullopt,
197  std::optional<std::string> aws_session_token = std::nullopt);
198 
216  S3Endpoint(std::pair<std::string, std::string> bucket_and_object_names,
217  std::optional<std::string> aws_region = std::nullopt,
218  std::optional<std::string> aws_access_key = std::nullopt,
219  std::optional<std::string> aws_secret_access_key = std::nullopt,
220  std::optional<std::string> aws_endpoint_url = std::nullopt,
221  std::optional<std::string> aws_session_token = std::nullopt);
222 
223  ~S3Endpoint() override;
224  void setopt(CurlHandle& curl) override;
225  std::string str() const override;
226  std::size_t get_file_size() override;
227  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
228 
235  static bool is_url_valid(std::string const& url) noexcept;
236 };
237 
245  private:
246  std::string _url;
247 
248  public:
249  explicit S3PublicEndpoint(std::string url);
250 
251  ~S3PublicEndpoint() override = default;
252  void setopt(CurlHandle& curl) override;
253  std::string str() const override;
254  std::size_t get_file_size() override;
255  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
256 
263  static bool is_url_valid(std::string const& url) noexcept;
264 };
265 
273  private:
274  std::string _url;
275 
276  public:
277  explicit S3EndpointWithPresignedUrl(std::string presigned_url);
278 
279  ~S3EndpointWithPresignedUrl() override = default;
280  void setopt(CurlHandle& curl) override;
281  std::string str() const override;
282  std::size_t get_file_size() override;
283  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
284 
291  static bool is_url_valid(std::string const& url) noexcept;
292 };
293 
298  private:
299  std::unique_ptr<RemoteEndpoint> _endpoint;
300  std::size_t _nbytes;
301 
302  public:
380  static RemoteHandle open(std::string url,
382  std::optional<std::vector<RemoteEndpointType>> allow_list = std::nullopt,
383  std::optional<std::size_t> nbytes = std::nullopt);
384 
391  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint, std::size_t nbytes);
392 
400  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint);
401 
402  // A remote handle is moveable but not copyable.
403  RemoteHandle(RemoteHandle&& o) = default;
404  RemoteHandle& operator=(RemoteHandle&& o) = default;
405  RemoteHandle(RemoteHandle const&) = delete;
406  RemoteHandle& operator=(RemoteHandle const&) = delete;
407 
413  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
414 
423  [[nodiscard]] std::size_t nbytes() const noexcept;
424 
430  [[nodiscard]] RemoteEndpoint const& endpoint() const noexcept;
431 
444  std::size_t read(void* buf, std::size_t size, std::size_t file_offset = 0);
445 
465  std::future<std::size_t> pread(void* buf,
466  std::size_t size,
467  std::size_t file_offset = 0,
468  std::size_t task_size = defaults::task_size(),
469  ThreadPool* thread_pool = &defaults::thread_pool());
470 };
471 
472 } // namespace kvikio
Representation of a curl easy handle pointer and its operations.
Definition: libcurl.hpp:80
A remote endpoint for HTTP/HTTPS resources.
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 for AWS S3 storage using presigned URLs.
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 for AWS S3 storage requiring credentials.
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.
A remote endpoint for publicly accessible S3 objects without authentication.
static bool is_url_valid(std::string const &url) noexcept
Whether the given URL is valid for S3 public endpoints.
std::size_t get_file_size() override
Get the size of the remote file.
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.
void setopt(CurlHandle &curl) override
Set needed connection options on a curl handle.
std::string str() const override
Get a description of this remote point instance.
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:112
KvikIO namespace.
Definition: batch.hpp:16
BS::thread_pool ThreadPool
Thread pool type used for parallel I/O operations.
RemoteEndpointType
Types of remote file endpoints supported by KvikIO.