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/utils.hpp>
17 
18 struct curl_slist;
19 
20 namespace kvikio {
21 
22 class CurlHandle; // Prototype
23 
30 enum class RemoteEndpointType : uint8_t {
31  AUTO,
33  S3,
36  S3_PUBLIC,
41  WEBHDFS,
43  HTTP,
45 };
46 
56  protected:
57  RemoteEndpointType _remote_endpoint_type{RemoteEndpointType::AUTO};
59 
60  public:
61  virtual ~RemoteEndpoint() = default;
62 
70  virtual void setopt(CurlHandle& curl) = 0;
71 
77  virtual std::string str() const = 0;
78 
84  virtual std::size_t get_file_size() = 0;
85 
90  virtual void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) = 0;
91 
97  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
98 };
99 
106 class HttpEndpoint : public RemoteEndpoint {
107  private:
108  std::string _url;
109 
110  public:
116  HttpEndpoint(std::string url);
117 
118  ~HttpEndpoint() override = default;
119  void setopt(CurlHandle& curl) override;
120  std::string str() const override;
121  std::size_t get_file_size() override;
122  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
123 
130  static bool is_url_valid(std::string const& url) noexcept;
131 };
132 
139 class S3Endpoint : public RemoteEndpoint {
140  private:
141  std::string _url;
142  std::string _aws_sigv4;
143  std::string _aws_userpwd;
144  curl_slist* _curl_header_list{};
145 
146  public:
162  static std::string url_from_bucket_and_object(std::string bucket_name,
163  std::string object_name,
164  std::optional<std::string> aws_region,
165  std::optional<std::string> aws_endpoint_url);
166 
175  [[nodiscard]] static std::pair<std::string, std::string> parse_s3_url(std::string const& s3_url);
176 
192  S3Endpoint(std::string url,
193  std::optional<std::string> aws_region = std::nullopt,
194  std::optional<std::string> aws_access_key = std::nullopt,
195  std::optional<std::string> aws_secret_access_key = std::nullopt,
196  std::optional<std::string> aws_session_token = std::nullopt);
197 
215  S3Endpoint(std::pair<std::string, std::string> bucket_and_object_names,
216  std::optional<std::string> aws_region = std::nullopt,
217  std::optional<std::string> aws_access_key = std::nullopt,
218  std::optional<std::string> aws_secret_access_key = std::nullopt,
219  std::optional<std::string> aws_endpoint_url = std::nullopt,
220  std::optional<std::string> aws_session_token = std::nullopt);
221 
222  ~S3Endpoint() override;
223  void setopt(CurlHandle& curl) override;
224  std::string str() const override;
225  std::size_t get_file_size() override;
226  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
227 
234  static bool is_url_valid(std::string const& url) noexcept;
235 };
236 
244  private:
245  std::string _url;
246 
247  public:
248  explicit S3PublicEndpoint(std::string url);
249 
250  ~S3PublicEndpoint() override = default;
251  void setopt(CurlHandle& curl) override;
252  std::string str() const override;
253  std::size_t get_file_size() override;
254  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
255 
262  static bool is_url_valid(std::string const& url) noexcept;
263 };
264 
272  private:
273  std::string _url;
274 
275  public:
276  explicit S3EndpointWithPresignedUrl(std::string presigned_url);
277 
278  ~S3EndpointWithPresignedUrl() override = default;
279  void setopt(CurlHandle& curl) override;
280  std::string str() const override;
281  std::size_t get_file_size() override;
282  void setup_range_request(CurlHandle& curl, std::size_t file_offset, std::size_t size) override;
283 
290  static bool is_url_valid(std::string const& url) noexcept;
291 };
292 
297  private:
298  std::unique_ptr<RemoteEndpoint> _endpoint;
299  std::size_t _nbytes;
300 
301  public:
379  static RemoteHandle open(std::string url,
381  std::optional<std::vector<RemoteEndpointType>> allow_list = std::nullopt,
382  std::optional<std::size_t> nbytes = std::nullopt);
383 
390  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint, std::size_t nbytes);
391 
399  RemoteHandle(std::unique_ptr<RemoteEndpoint> endpoint);
400 
401  // A remote handle is moveable but not copyable.
402  RemoteHandle(RemoteHandle&& o) = default;
403  RemoteHandle& operator=(RemoteHandle&& o) = default;
404  RemoteHandle(RemoteHandle const&) = delete;
405  RemoteHandle& operator=(RemoteHandle const&) = delete;
406 
412  [[nodiscard]] RemoteEndpointType remote_endpoint_type() const noexcept;
413 
422  [[nodiscard]] std::size_t nbytes() const noexcept;
423 
429  [[nodiscard]] RemoteEndpoint const& endpoint() const noexcept;
430 
443  std::size_t read(void* buf, std::size_t size, std::size_t file_offset = 0);
444 
457  std::future<std::size_t> pread(void* buf,
458  std::size_t size,
459  std::size_t file_offset = 0,
460  std::size_t task_size = defaults::task_size());
461 };
462 
463 } // 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
RemoteEndpointType
Types of remote file endpoints supported by KvikIO.