aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-02-26 22:59:29 +0800
committerGitHub <noreply@github.com>2019-02-26 22:59:29 +0800
commitebd4d50e6e32d140be2bca0f1b175618c41f2478 (patch)
treed9e2df8cb37c0bb12040de7361cd5afc2972c4ee
parent6bfb082ea298ac4366d4808f3a1b7c065cf89302 (diff)
parent06d633598727763c54b4b049dbc213106474d10c (diff)
Merge pull request #2703 from pradd/support_s3_region_param
Support parameters in S3 URLs
-rw-r--r--src/libstore/download.cc31
-rw-r--r--src/libstore/store-api.cc16
-rw-r--r--src/libstore/store-api.hh4
3 files changed, 41 insertions, 10 deletions
diff --git a/src/libstore/download.cc b/src/libstore/download.cc
index 467f570bb..22382ab1d 100644
--- a/src/libstore/download.cc
+++ b/src/libstore/download.cc
@@ -614,6 +614,22 @@ struct CurlDownloader : public Downloader
writeFull(wakeupPipe.writeSide.get(), " ");
}
+#ifdef ENABLE_S3
+ std::tuple<std::string, std::string, Store::Params> parseS3Uri(std::string uri)
+ {
+ auto [path, params] = splitUriAndParams(uri);
+
+ auto slash = path.find('/', 5); // 5 is the length of "s3://" prefix
+ if (slash == std::string::npos)
+ throw nix::Error("bad S3 URI '%s'", path);
+
+ std::string bucketName(path, 5, slash - 5);
+ std::string key(path, slash + 1);
+
+ return {bucketName, key, params};
+ }
+#endif
+
void enqueueDownload(const DownloadRequest & request,
Callback<DownloadResult> callback) override
{
@@ -622,12 +638,15 @@ struct CurlDownloader : public Downloader
// FIXME: do this on a worker thread
try {
#ifdef ENABLE_S3
- S3Helper s3Helper("", Aws::Region::US_EAST_1, "", ""); // FIXME: make configurable
- auto slash = request.uri.find('/', 5);
- if (slash == std::string::npos)
- throw nix::Error("bad S3 URI '%s'", request.uri);
- std::string bucketName(request.uri, 5, slash - 5);
- std::string key(request.uri, slash + 1);
+ auto [bucketName, key, params] = parseS3Uri(request.uri);
+
+ std::string profile = get(params, "profile", "");
+ std::string region = get(params, "region", Aws::Region::US_EAST_1);
+ std::string scheme = get(params, "scheme", "");
+ std::string endpoint = get(params, "endpoint", "");
+
+ S3Helper s3Helper(profile, region, scheme, endpoint);
+
// FIXME: implement ETag
auto s3Res = s3Helper.getObject(bucketName, key);
DownloadResult res;
diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc
index 913a11121..c13ff1156 100644
--- a/src/libstore/store-api.cc
+++ b/src/libstore/store-api.cc
@@ -842,12 +842,11 @@ namespace nix {
RegisterStoreImplementation::Implementations * RegisterStoreImplementation::implementations = 0;
-
-ref<Store> openStore(const std::string & uri_,
- const Store::Params & extraParams)
+/* Split URI into protocol+hierarchy part and its parameter set. */
+std::pair<std::string, Store::Params> splitUriAndParams(const std::string & uri_)
{
auto uri(uri_);
- Store::Params params(extraParams);
+ Store::Params params;
auto q = uri.find('?');
if (q != std::string::npos) {
for (auto s : tokenizeString<Strings>(uri.substr(q + 1), "&")) {
@@ -873,6 +872,15 @@ ref<Store> openStore(const std::string & uri_,
}
uri = uri_.substr(0, q);
}
+ return {uri, params};
+}
+
+ref<Store> openStore(const std::string & uri_,
+ const Store::Params & extraParams)
+{
+ auto [uri, uriParams] = splitUriAndParams(uri_);
+ auto params = extraParams;
+ params.insert(uriParams.begin(), uriParams.end());
for (auto fun : *RegisterStoreImplementation::implementations) {
auto store = fun(uri, params);
diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh
index f504735e0..ad0f8df11 100644
--- a/src/libstore/store-api.hh
+++ b/src/libstore/store-api.hh
@@ -798,4 +798,8 @@ ValidPathInfo decodeValidPathInfo(std::istream & str,
for paths created by makeFixedOutputPath() / addToStore(). */
std::string makeFixedOutputCA(bool recursive, const Hash & hash);
+
+/* Split URI into protocol+hierarchy part and its parameter set. */
+std::pair<std::string, Store::Params> splitUriAndParams(const std::string & uri);
+
}