Flex  0.17.9
oss_storage.h
Go to the documentation of this file.
1 
16 #ifndef FLEX_UTILS_REMOTE_OSS_STORAGE_H_
17 #define FLEX_UTILS_REMOTE_OSS_STORAGE_H_
18 
19 #ifdef BUILD_WITH_OSS
20 
21 #include <filesystem>
22 #include <string>
23 #include "flex/third_party/aliyun-oss-cpp-sdk/sdk/include/alibabacloud/oss/OssClient.h"
24 #include "flex/third_party/aliyun-oss-cpp-sdk/sdk/include/alibabacloud/oss/client/ClientConfiguration.h"
26 #include "flex/utils/result.h"
27 
28 namespace gs {
29 
30 struct OSSConf {
31  static constexpr const char* kOSSAccessKeyId = "OSS_ACCESS_KEY_ID";
32  static constexpr const char* kOSSAccessKeySecret = "OSS_ACCESS_KEY_SECRET";
33  static constexpr const char* kOSSEndpoint = "OSS_ENDPOINT";
34  static constexpr const char* kOSSBucketName = "OSS_BUCKET_NAME";
35  static constexpr const char* kOSSConcurrency = "OSS_CONCURRENCY";
36  // Avoid storing or printing the accesskey_id and accesskey_secret
37  std::string accesskey_id_;
38  std::string accesskey_secret_;
39  std::string endpoint_;
40  std::string bucket_name_;
41  int32_t concurrency_ = 4;
42  uint64_t partition_size_ = 1024 * 1024 * 128;
43  AlibabaCloud::OSS::ClientConfiguration client_conf_;
44 
45  void load_conf_from_env();
46 };
47 
48 class OSSRemoteStorageUploader : public RemoteStorageUploader {
49  public:
50  OSSRemoteStorageUploader(OSSConf conf = {}) : conf_(conf) {}
51 
52  ~OSSRemoteStorageUploader() override = default;
53 
54  // Will try to load the accesskey_id and accesskey_secret from the environment
55  // variables if they are not set in the OSSConf.
56  Status Open() override;
57 
58  Status Put(const std::string& local_path, const std::string& remote_path,
59  bool override = false) override;
60 
61  Status Delete(const std::string& remote_path) override;
62 
63  Status Close() override;
64 
65  private:
66  OSSConf conf_;
67  std::shared_ptr<AlibabaCloud::OSS::OssClient> client_;
68 };
69 
70 class OSSRemoteStorageDownloader : public RemoteStorageDownloader {
71  public:
72  OSSRemoteStorageDownloader(OSSConf conf = {}) : conf_(conf) {}
73 
74  ~OSSRemoteStorageDownloader() override = default;
75 
76  Status Open() override;
77 
78  Status Get(const std::string& remote_path,
79  const std::string& local_path) override;
80 
81  Status List(const std::string& remote_path,
82  std::vector<std::string>& list) override;
83 
84  Status Close() override;
85 
86  private:
87  bool get_metadata_etag(const std::string& remote_path, std::string& etag);
88  OSSConf conf_;
89  std::shared_ptr<AlibabaCloud::OSS::OssClient> client_;
90 };
91 } // namespace gs
92 
93 #endif // BUILD_WITH_OSS
94 
95 #endif // FLEX_UTILS_REMOTE_OSS_STORAGE_H_
Definition: adj_list.h:23