GDAL
cpl_alibaba_oss.h
1/**********************************************************************
2 * $Id$
3 *
4 * Name: cpl_alibaba_oss.h
5 * Project: CPL - Common Portability Library
6 * Purpose: Alibaba Cloud Object Storage Service
7 * Author: Even Rouault <even.rouault at spatialys.com>
8 *
9 **********************************************************************
10 * Copyright (c) 2017, Even Rouault <even.rouault at spatialys.com>
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 ****************************************************************************/
30
31#ifndef CPL_ALIBABA_OSS_INCLUDED_H
32#define CPL_ALIBABA_OSS_INCLUDED_H
33
34#ifndef DOXYGEN_SKIP
35
36#include <cstddef>
37
38#include "cpl_string.h"
39
40#ifdef HAVE_CURL
41
42#include <curl/curl.h>
43#include <map>
44#include "cpl_aws.h"
45
46class VSIOSSHandleHelper final : public IVSIS3LikeHandleHelper
47{
48 CPL_DISALLOW_COPY_ASSIGN(VSIOSSHandleHelper)
49
50 std::string m_osURL{};
51 std::string m_osSecretAccessKey{};
52 std::string m_osAccessKeyId{};
53 std::string m_osEndpoint{};
54 std::string m_osBucket{};
55 std::string m_osObjectKey{};
56 bool m_bUseHTTPS = false;
57 bool m_bUseVirtualHosting = false;
58
59 void RebuildURL() override;
60
61 static bool GetConfiguration(const std::string &osPathForOption,
62 CSLConstList papszOptions,
63 std::string &osSecretAccessKey,
64 std::string &osAccessKeyId);
65
66 protected:
67 public:
68 VSIOSSHandleHelper(const std::string &osSecretAccessKey,
69 const std::string &osAccessKeyId,
70 const std::string &osEndpoint,
71 const std::string &osBucket,
72 const std::string &osObjectKey, bool bUseHTTPS,
73 bool bUseVirtualHosting);
74 ~VSIOSSHandleHelper();
75
76 static VSIOSSHandleHelper *
77 BuildFromURI(const char *pszURI, const char *pszFSPrefix,
78 bool bAllowNoObject, CSLConstList papszOptions = nullptr);
79 static std::string BuildURL(const std::string &osEndpoint,
80 const std::string &osBucket,
81 const std::string &osObjectKey, bool bUseHTTPS,
82 bool bUseVirtualHosting);
83
84 struct curl_slist *
85 GetCurlHeaders(const std::string &osVerb,
86 const struct curl_slist *psExistingHeaders,
87 const void *pabyDataContent = nullptr,
88 size_t nBytesContent = 0) const override;
89
90 bool CanRestartOnError(const char *, const char *pszHeaders,
91 bool bSetError) override;
92
93 const std::string &GetURL() const override
94 {
95 return m_osURL;
96 }
97
98 const std::string &GetBucket() const
99 {
100 return m_osBucket;
101 }
102
103 const std::string &GetObjectKey() const
104 {
105 return m_osObjectKey;
106 }
107
108 const std::string &GetEndpoint() const
109 {
110 return m_osEndpoint;
111 }
112
113 bool GetVirtualHosting() const
114 {
115 return m_bUseVirtualHosting;
116 }
117
118 std::string GetCopySourceHeader() const override
119 {
120 return "x-oss-copy-source";
121 }
122
123 void SetEndpoint(const std::string &osStr);
124 void SetVirtualHosting(bool b);
125
126 std::string GetSignedURL(CSLConstList papszOptions);
127};
128
129class VSIOSSUpdateParams
130{
131 private:
132 std::string m_osEndpoint{};
133
134 explicit VSIOSSUpdateParams(const VSIOSSHandleHelper *poHelper)
135 : m_osEndpoint(poHelper->GetEndpoint())
136 {
137 }
138
139 void UpdateHandlerHelper(VSIOSSHandleHelper *poHelper)
140 {
141 poHelper->SetEndpoint(m_osEndpoint);
142 }
143
144 static std::mutex gsMutex;
145 static std::map<std::string, VSIOSSUpdateParams> goMapBucketsToOSSParams;
146
147 public:
148 VSIOSSUpdateParams() = default;
149
150 static void UpdateMapFromHandle(VSIOSSHandleHelper *poHandleHelper);
151 static void UpdateHandleFromMap(VSIOSSHandleHelper *poHandleHelper);
152 static void ClearCache();
153};
154
155#endif /* HAVE_CURL */
156
157#endif /* #ifndef DOXYGEN_SKIP */
158
159#endif /* CPL_ALIBABA_OSS_INCLUDED_H */
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition: cpl_port.h:1042
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1183
Various convenience functions for working with strings and string lists.