|
| 1 | +#ifndef XTENSOR_IO_AWS_HANDLER_HPP |
| 2 | +#define XTENSOR_IO_AWS_HANDLER_HPP |
| 3 | + |
| 4 | +#include "xtensor/xarray.hpp" |
| 5 | +#include "xtensor/xexpression.hpp" |
| 6 | +#include "xfile_array.hpp" |
| 7 | +#include <aws/core/Aws.h> |
| 8 | +#include <aws/s3/S3Client.h> |
| 9 | +#include <aws/s3/model/GetObjectRequest.h> |
| 10 | +#include <aws/s3/model/PutObjectRequest.h> |
| 11 | + |
| 12 | +namespace xt |
| 13 | +{ |
| 14 | + struct xio_aws_config |
| 15 | + { |
| 16 | + Aws::S3::S3Client client; |
| 17 | + Aws::String bucket; |
| 18 | + }; |
| 19 | + |
| 20 | + template <class C> |
| 21 | + class xio_aws_handler |
| 22 | + { |
| 23 | + public: |
| 24 | + using io_config = xio_aws_config; |
| 25 | + |
| 26 | + xio_aws_handler(); |
| 27 | + |
| 28 | + template <class E> |
| 29 | + void write(const xexpression<E>& expression, const std::string& path, xfile_dirty dirty); |
| 30 | + |
| 31 | + template <class ET> |
| 32 | + void read(ET& array, const std::string& path); |
| 33 | + |
| 34 | + void configure(const C& format_config, const xio_aws_config& io_config); |
| 35 | + void configure_io(const xio_aws_config& io_config); |
| 36 | + |
| 37 | + private: |
| 38 | + template <class E> |
| 39 | + void write(const xexpression<E>& expression, const char* path, xfile_dirty dirty); |
| 40 | + |
| 41 | + template <class ET> |
| 42 | + void read(ET& array, const char* path); |
| 43 | + |
| 44 | + C m_format_config; |
| 45 | + Aws::S3::S3Client m_client; |
| 46 | + Aws::String m_bucket; |
| 47 | + }; |
| 48 | + |
| 49 | + template <class C> |
| 50 | + xio_aws_handler<C>::xio_aws_handler() |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + template <class C> |
| 55 | + template <class E> |
| 56 | + inline void xio_aws_handler<C>::write(const xexpression<E>& expression, const std::string& path, xfile_dirty dirty) |
| 57 | + { |
| 58 | + write(expression, path.c_str(), dirty); |
| 59 | + } |
| 60 | + |
| 61 | + template <class C> |
| 62 | + template <class E> |
| 63 | + inline void xio_aws_handler<C>::write(const xexpression<E>& expression, const char* path, xfile_dirty dirty) |
| 64 | + { |
| 65 | + if (m_format_config.will_dump(dirty)) |
| 66 | + { |
| 67 | + Aws::String path2 = path; |
| 68 | + Aws::S3::Model::PutObjectRequest request; |
| 69 | + request.SetBucket(m_bucket); |
| 70 | + request.SetKey(path2); |
| 71 | + |
| 72 | + std::shared_ptr<Aws::IOStream> writer = Aws::MakeShared<Aws::FStream>("SampleAllocationTag", path, std::ios_base::in | std::ios_base::binary); |
| 73 | + dump_file(*writer, expression, m_format_config); |
| 74 | + |
| 75 | + request.SetBody(writer); |
| 76 | + |
| 77 | + Aws::S3::Model::PutObjectOutcome outcome = m_client.PutObject(request); |
| 78 | + |
| 79 | + if (!outcome.IsSuccess()) |
| 80 | + { |
| 81 | + auto err = outcome.GetError(); |
| 82 | + XTENSOR_THROW(std::runtime_error, std::string("Error: PutObject: ") + err.GetExceptionName().c_str() + ": " + err.GetMessage().c_str()); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + template <class C> |
| 88 | + template <class ET> |
| 89 | + inline void xio_aws_handler<C>::read(ET& array, const std::string& path) |
| 90 | + { |
| 91 | + read(array, path.c_str()); |
| 92 | + } |
| 93 | + |
| 94 | + template <class C> |
| 95 | + template <class ET> |
| 96 | + inline void xio_aws_handler<C>::read(ET& array, const char* path) |
| 97 | + { |
| 98 | + Aws::String path2 = path; |
| 99 | + Aws::S3::Model::GetObjectRequest request; |
| 100 | + request.SetBucket(m_bucket); |
| 101 | + request.SetKey(path2); |
| 102 | + |
| 103 | + Aws::S3::Model::GetObjectOutcome outcome = m_client.GetObject(request); |
| 104 | + |
| 105 | + if (!outcome.IsSuccess()) |
| 106 | + { |
| 107 | + auto err = outcome.GetError(); |
| 108 | + XTENSOR_THROW(std::runtime_error, std::string("Error: GetObject: ") + err.GetExceptionName().c_str() + ": " + err.GetMessage().c_str()); |
| 109 | + } |
| 110 | + |
| 111 | + auto& reader = outcome.GetResultWithOwnership().GetBody(); |
| 112 | + load_file<ET>(reader, array, m_format_config); |
| 113 | + } |
| 114 | + |
| 115 | + template <class C> |
| 116 | + inline void xio_aws_handler<C>::configure(const C& format_config, const xio_aws_config& io_config) |
| 117 | + { |
| 118 | + m_format_config = format_config; |
| 119 | + m_client = io_config.client; |
| 120 | + m_bucket = io_config.bucket; |
| 121 | + } |
| 122 | + |
| 123 | + template <class C> |
| 124 | + inline void xio_aws_handler<C>::configure_io(const xio_aws_config& io_config) |
| 125 | + { |
| 126 | + m_client = io_config.client; |
| 127 | + m_bucket = io_config.bucket; |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +#endif |
0 commit comments