Austin Schuh | 8611071 | 2022-09-16 15:40:54 -0700 | [diff] [blame] | 1 | |
| 2 | #ifndef AOS_EVENTS_LOGGING_S3_FETCHER_H_ |
| 3 | #define AOS_EVENTS_LOGGING_S3_FETCHER_H_ |
| 4 | |
| 5 | #include <aws/s3/S3Client.h> |
| 6 | #include <aws/s3/model/GetObjectRequest.h> |
| 7 | |
| 8 | #include <future> |
| 9 | #include <string_view> |
| 10 | |
| 11 | #include "aos/containers/resizeable_buffer.h" |
| 12 | #include "aos/events/logging/buffer_encoder.h" |
| 13 | |
| 14 | namespace aos::logger { |
| 15 | |
| 16 | // Fetches data from an S3 URL. |
| 17 | class S3Fetcher final : public DataDecoder { |
| 18 | public: |
| 19 | explicit S3Fetcher(std::string_view url); |
| 20 | S3Fetcher(const S3Fetcher &) = delete; |
| 21 | S3Fetcher &operator=(const S3Fetcher &) = delete; |
| 22 | |
| 23 | size_t Read(uint8_t *begin, uint8_t *end) final; |
| 24 | std::string_view filename() const final { return url_; } |
| 25 | |
| 26 | private: |
| 27 | const std::string url_; |
| 28 | |
| 29 | // The current chunk we're reading from. Empty if there is no current chunk or |
| 30 | // we've read all of it. |
| 31 | ResizeableBuffer current_chunk_; |
| 32 | // If valid, the next chunk which we've triggered to be retrieved in the |
| 33 | // background. |
| 34 | std::future<Aws::S3::Model::GetObjectOutcome> get_next_chunk_; |
| 35 | |
| 36 | // The next byte index we're going to request. This means we've already made |
| 37 | // requests for all prior bytes, but not necessarily received them. |
| 38 | uint64_t next_byte_to_request_ = 0; |
| 39 | |
| 40 | // Set once we've received data for the end of the object. Some of it may |
| 41 | // still be in current_chunk_ though. |
| 42 | bool end_of_object_ = false; |
| 43 | |
| 44 | // Kicks off a request for the next chunk. |
| 45 | void StartRequest(); |
| 46 | }; |
| 47 | |
| 48 | Aws::S3::S3Client& GetS3Client(); |
| 49 | |
| 50 | struct ObjectName { |
| 51 | std::string bucket, key; |
| 52 | }; |
| 53 | |
| 54 | ObjectName ParseUrl(std::string_view url); |
| 55 | |
| 56 | // Does an S3 object listing with the given URL prefix. Returns the URLs for all |
| 57 | // the objects under it. |
| 58 | std::vector<std::string> ListS3Objects(std::string_view url); |
| 59 | |
| 60 | } // namespace aos::logger |
| 61 | |
| 62 | #endif // AOS_EVENTS_LOGGING_S3_FETCHER_H_ |