22#include "grk_config_private.h"
26#ifdef GRK_ENABLE_LIBCURL
31class AZFetcher :
public CurlFetcher
34 void parse(
const std::string& path)
override
36 ParsedFetchPath parsed;
38 std::string mutable_path = path;
39 if(path.starts_with(
"/vsiaz/"))
43 if(!auth_.username_.empty())
45 account = auth_.username_;
46 grklog.
debug(
"Using auth-provided account for vsiaz: %s", account.c_str());
50 grklog.
error(
"No Azure account provided for /vsiaz/ path");
51 throw std::runtime_error(
"No Azure account provided for /vsiaz/ path");
53 FetchPathParser::parseVsiPath(mutable_path, parsed,
"vsiaz");
54 parsed.host = account +
".blob.core.windows.net";
57 else if(path.starts_with(
"https://"))
60 FetchPathParser::parseHttpsPath(mutable_path, parsed);
64 grklog.
error(
"Unsupported URL format for Azure Blob: %s", path.c_str());
65 throw std::runtime_error(
"Unsupported URL format for Azure Blob");
68 grklog.
debug(
"Parsed AZ URL - Host: %s, Port: %d, Container: %s, Blob: %s", parsed.host.c_str(),
69 parsed.port, parsed.bucket.c_str(), parsed.key.c_str());
72 url_ =
"https://" + parsed.host +
"/" + parsed.bucket +
"/" + parsed.key;
73 grklog.
debug(
"Parsed AZFetcher URL: %s", url_.c_str());
76 void auth(CURL* curl)
override
79 CurlFetcher::auth(curl);
82 std::string final_url = url_;
85 std::string account = auth_.username_;
86 std::string secret = auth_.password_;
90 if(
const char* key = std::getenv(
"AZURE_STORAGE_ACCOUNT"))
93 grklog.
debug(
"Set Azure account = %s", account.c_str());
98 if(
const char* key = std::getenv(
"AZURE_STORAGE_KEY"))
101 grklog.
debug(
"Set Azure key = %s", secret.c_str());
103 else if(
const char* sas = std::getenv(
"AZURE_STORAGE_SAS_TOKEN"))
106 grklog.
debug(
"Set Azure SAS token = %s", secret.c_str());
111 if(!account.empty() && !secret.empty() && secret.find(
"?") != 0)
114 curl_easy_setopt(curl, CURLOPT_USERNAME, account.c_str());
115 curl_easy_setopt(curl, CURLOPT_PASSWORD, secret.c_str());
116 grklog.
debug(
"Applied account key authentication for account: %s", account.c_str());
118 else if(!secret.empty() && secret.find(
"?") == 0)
122 curl_easy_setopt(curl, CURLOPT_URL, final_url.c_str());
123 grklog.
debug(
"Applied SAS token authentication, final URL: %s", final_url.c_str());
127 grklog.
debug(
"No additional Azure authentication applied (public access or other auth)");
131 curl_slist* prepareAuthHeaders(curl_slist* headers)
override
136 strftime(date_buf,
sizeof(date_buf),
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
137 std::string date_header =
"x-ms-date: " + std::string(date_buf);
138 headers = curl_slist_append(headers, date_header.c_str());
140 return curl_slist_append(headers,
"x-ms-version: 2020-04-08");
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
virtual void error(const char *fmt,...)=0
virtual void debug(const char *fmt,...)=0