2016-05-04 17:16:48 +02:00
|
|
|
#if ENABLE_S3
|
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/s3-binary-cache-store.hh"
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-24 02:36:11 +02:00
|
|
|
#include <absl/strings/ascii.h>
|
2020-05-25 03:19:01 +02:00
|
|
|
#include <absl/strings/match.h>
|
2016-12-22 17:39:05 +01:00
|
|
|
#include <aws/core/Aws.h>
|
2017-12-22 12:05:13 +01:00
|
|
|
#include <aws/core/VersionConfig.h>
|
2017-11-15 14:16:04 +01:00
|
|
|
#include <aws/core/auth/AWSCredentialsProvider.h>
|
|
|
|
#include <aws/core/auth/AWSCredentialsProviderChain.h>
|
2016-04-21 16:02:48 +02:00
|
|
|
#include <aws/core/client/ClientConfiguration.h>
|
2017-02-21 11:42:38 +01:00
|
|
|
#include <aws/core/client/DefaultRetryStrategy.h>
|
2017-06-19 17:01:01 +02:00
|
|
|
#include <aws/core/utils/logging/FormattedLogSystem.h>
|
|
|
|
#include <aws/core/utils/logging/LogMacros.h>
|
2018-05-07 15:23:51 +02:00
|
|
|
#include <aws/core/utils/threading/Executor.h>
|
2016-04-21 16:02:48 +02:00
|
|
|
#include <aws/s3/S3Client.h>
|
|
|
|
#include <aws/s3/model/GetObjectRequest.h>
|
|
|
|
#include <aws/s3/model/HeadObjectRequest.h>
|
2016-04-21 17:53:47 +02:00
|
|
|
#include <aws/s3/model/ListObjectsRequest.h>
|
2016-12-22 17:39:05 +01:00
|
|
|
#include <aws/s3/model/PutObjectRequest.h>
|
2018-05-07 15:23:51 +02:00
|
|
|
#include <aws/transfer/TransferManager.h>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-27 22:56:34 +02:00
|
|
|
#include "libstore/download.hh"
|
|
|
|
#include "libstore/globals.hh"
|
|
|
|
#include "libstore/nar-info-disk-cache.hh"
|
|
|
|
#include "libstore/nar-info.hh"
|
|
|
|
#include "libstore/s3.hh"
|
|
|
|
#include "libutil/compression.hh"
|
|
|
|
#include "libutil/istringstream_nocopy.hh"
|
2018-05-07 15:23:51 +02:00
|
|
|
|
|
|
|
using namespace Aws::Transfer;
|
2016-04-21 16:02:48 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
struct S3Error : public Error {
|
|
|
|
Aws::S3::S3Errors err;
|
|
|
|
S3Error(Aws::S3::S3Errors err, const FormatOrString& fs)
|
|
|
|
: Error(fs), err(err){};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper: given an Outcome<R, E>, return R in case of success, or
|
|
|
|
throw an exception in case of an error. */
|
|
|
|
template <typename R, typename E>
|
|
|
|
R&& checkAws(const FormatOrString& fs, Aws::Utils::Outcome<R, E>&& outcome) {
|
|
|
|
if (!outcome.IsSuccess())
|
|
|
|
throw S3Error(outcome.GetError().GetErrorType(),
|
|
|
|
fs.s + ": " + outcome.GetError().GetMessage());
|
|
|
|
return outcome.GetResultWithOwnership();
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:01:01 +02:00
|
|
|
class AwsLogger : public Aws::Utils::Logging::FormattedLogSystem {
|
|
|
|
using Aws::Utils::Logging::FormattedLogSystem::FormattedLogSystem;
|
|
|
|
|
|
|
|
void ProcessFormattedStatement(Aws::String&& statement) override {
|
2020-05-24 02:36:11 +02:00
|
|
|
debug("AWS: %s", absl::StripTrailingAsciiWhitespace(statement));
|
2017-06-19 17:01:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-22 17:39:05 +01:00
|
|
|
static void initAWS() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
Aws::SDKOptions options;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-12-22 17:39:05 +01:00
|
|
|
/* We install our own OpenSSL locking function (see
|
|
|
|
shared.cc), so don't let aws-sdk-cpp override it. */
|
|
|
|
options.cryptoOptions.initAndCleanupOpenSSL = false;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2017-06-19 17:01:01 +02:00
|
|
|
if (verbosity >= lvlDebug) {
|
|
|
|
options.loggingOptions.logLevel =
|
|
|
|
verbosity == lvlDebug ? Aws::Utils::Logging::LogLevel::Debug
|
|
|
|
: Aws::Utils::Logging::LogLevel::Trace;
|
|
|
|
options.loggingOptions.logger_create_fn = [options]() {
|
|
|
|
return std::make_shared<AwsLogger>(options.loggingOptions.logLevel);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-22 17:39:05 +01:00
|
|
|
Aws::InitAPI(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:29:21 +02:00
|
|
|
S3Helper::S3Helper(const std::string& profile, const std::string& region,
|
|
|
|
const std::string& scheme, const std::string& endpoint)
|
2018-12-07 23:38:24 +01:00
|
|
|
: config(makeConfig(region, scheme, endpoint)),
|
2017-11-15 14:16:04 +01:00
|
|
|
client(make_ref<Aws::S3::S3Client>(
|
|
|
|
profile == ""
|
|
|
|
? std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>(
|
|
|
|
std::make_shared<
|
|
|
|
Aws::Auth::DefaultAWSCredentialsProviderChain>())
|
|
|
|
: std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>(
|
|
|
|
std::make_shared<
|
|
|
|
Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(
|
|
|
|
profile.c_str())),
|
2017-12-22 12:05:13 +01:00
|
|
|
*config,
|
|
|
|
// FIXME: https://github.com/aws/aws-sdk-cpp/issues/759
|
|
|
|
#if AWS_VERSION_MAJOR == 1 && AWS_VERSION_MINOR < 3
|
|
|
|
false,
|
|
|
|
#else
|
|
|
|
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
|
|
|
|
#endif
|
2018-07-31 22:45:49 +02:00
|
|
|
endpoint.empty())) {
|
2017-02-14 14:20:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 11:42:38 +01:00
|
|
|
/* Log AWS retries. */
|
|
|
|
class RetryStrategy : public Aws::Client::DefaultRetryStrategy {
|
2017-06-19 18:13:32 +02:00
|
|
|
bool ShouldRetry(const Aws::Client::AWSError<Aws::Client::CoreErrors>& error,
|
|
|
|
long attemptedRetries) const override {
|
|
|
|
auto retry =
|
|
|
|
Aws::Client::DefaultRetryStrategy::ShouldRetry(error, attemptedRetries);
|
|
|
|
if (retry)
|
|
|
|
printError("AWS error '%s' (%s), will retry in %d ms",
|
|
|
|
error.GetExceptionName(), error.GetMessage(),
|
|
|
|
CalculateDelayBeforeNextRetry(error, attemptedRetries));
|
|
|
|
return retry;
|
2017-02-21 11:42:38 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-07 23:38:24 +01:00
|
|
|
ref<Aws::Client::ClientConfiguration> S3Helper::makeConfig(
|
2020-05-24 23:29:21 +02:00
|
|
|
const std::string& region, const std::string& scheme,
|
|
|
|
const std::string& endpoint) {
|
2017-02-14 14:20:00 +01:00
|
|
|
initAWS();
|
|
|
|
auto res = make_ref<Aws::Client::ClientConfiguration>();
|
2017-03-03 22:12:17 +01:00
|
|
|
res->region = region;
|
2018-12-07 23:38:24 +01:00
|
|
|
if (!scheme.empty()) {
|
|
|
|
res->scheme = Aws::Http::SchemeMapper::FromString(scheme.c_str());
|
|
|
|
}
|
2018-07-31 22:45:49 +02:00
|
|
|
if (!endpoint.empty()) {
|
|
|
|
res->endpointOverride = endpoint;
|
|
|
|
}
|
2017-02-14 14:20:00 +01:00
|
|
|
res->requestTimeoutMs = 600 * 1000;
|
2019-03-15 13:16:20 +01:00
|
|
|
res->connectTimeoutMs = 5 * 1000;
|
2017-02-21 11:42:38 +01:00
|
|
|
res->retryStrategy = std::make_shared<RetryStrategy>();
|
2017-03-06 20:30:35 +01:00
|
|
|
res->caFile = settings.caFile;
|
2017-02-14 14:20:00 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
S3Helper::DownloadResult S3Helper::getObject(const std::string& bucketName,
|
|
|
|
const std::string& key) {
|
2017-07-30 13:27:57 +02:00
|
|
|
debug("fetching 's3://%s/%s'...", bucketName, key);
|
2017-02-14 14:20:00 +01:00
|
|
|
|
|
|
|
auto request =
|
|
|
|
Aws::S3::Model::GetObjectRequest().WithBucket(bucketName).WithKey(key);
|
|
|
|
|
|
|
|
request.SetResponseStreamFactory(
|
|
|
|
[&]() { return Aws::New<std::stringstream>("STRINGSTREAM"); });
|
|
|
|
|
|
|
|
DownloadResult res;
|
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
2017-02-14 14:20:00 +01:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
try {
|
2017-07-30 13:27:57 +02:00
|
|
|
auto result = checkAws(fmt("AWS error fetching '%s'", key),
|
2017-02-14 14:20:00 +01:00
|
|
|
client->GetObject(request));
|
|
|
|
|
2018-08-06 15:40:29 +02:00
|
|
|
res.data =
|
|
|
|
decompress(result.GetContentEncoding(),
|
|
|
|
dynamic_cast<std::stringstream&>(result.GetBody()).str());
|
2017-02-14 14:20:00 +01:00
|
|
|
|
|
|
|
} catch (S3Error& e) {
|
|
|
|
if (e.err != Aws::S3::S3Errors::NO_SUCH_KEY) {
|
|
|
|
throw;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2017-02-14 14:20:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
res.durationMs =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1)
|
|
|
|
.count();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore {
|
2017-11-15 14:16:04 +01:00
|
|
|
const Setting<std::string> profile{
|
|
|
|
this, "", "profile", "The name of the AWS configuration profile to use."};
|
2017-04-13 15:55:38 +02:00
|
|
|
const Setting<std::string> region{
|
|
|
|
this, Aws::Region::US_EAST_1, "region", {"aws-region"}};
|
2018-12-07 23:38:24 +01:00
|
|
|
const Setting<std::string> scheme{
|
|
|
|
this, "", "scheme",
|
|
|
|
"The scheme to use for S3 requests, https by default."};
|
2018-07-31 22:45:49 +02:00
|
|
|
const Setting<std::string> endpoint{
|
|
|
|
this, "", "endpoint",
|
|
|
|
"An optional override of the endpoint to use when talking to S3."};
|
2017-04-13 15:55:38 +02:00
|
|
|
const Setting<std::string> narinfoCompression{
|
|
|
|
this, "", "narinfo-compression", "compression method for .narinfo files"};
|
|
|
|
const Setting<std::string> lsCompression{this, "", "ls-compression",
|
|
|
|
"compression method for .ls files"};
|
|
|
|
const Setting<std::string> logCompression{
|
|
|
|
this, "", "log-compression", "compression method for log/* files"};
|
2018-10-30 14:25:00 +01:00
|
|
|
const Setting<bool> multipartUpload{this, false, "multipart-upload",
|
|
|
|
"whether to use multi-part uploads"};
|
2018-05-07 15:23:51 +02:00
|
|
|
const Setting<uint64_t> bufferSize{
|
2018-05-10 12:06:28 +02:00
|
|
|
this, 5 * 1024 * 1024, "buffer-size",
|
|
|
|
"size (in bytes) of each part in multi-part uploads"};
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
std::string bucketName;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
Stats stats;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2017-02-14 14:20:00 +01:00
|
|
|
S3Helper s3Helper;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
S3BinaryCacheStoreImpl(const Params& params, const std::string& bucketName)
|
2016-05-04 20:15:41 +02:00
|
|
|
: S3BinaryCacheStore(params),
|
2016-04-21 16:02:48 +02:00
|
|
|
bucketName(bucketName),
|
2018-12-07 23:38:24 +01:00
|
|
|
s3Helper(profile, region, scheme, endpoint) {
|
2016-04-21 16:02:48 +02:00
|
|
|
diskCache = getNarInfoDiskCache();
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-05-31 13:31:04 +02:00
|
|
|
std::string getUri() override { return "s3://" + bucketName; }
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-05-31 13:31:04 +02:00
|
|
|
void init() override {
|
2016-06-01 15:15:21 +02:00
|
|
|
if (!diskCache->cacheExists(getUri(), wantMassQuery_, priority)) {
|
2016-04-21 16:02:48 +02:00
|
|
|
BinaryCacheStore::init();
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-06-01 14:49:12 +02:00
|
|
|
diskCache->createCache(getUri(), storeDir, wantMassQuery_, priority);
|
2016-04-21 16:02:48 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2016-10-06 16:34:14 +02:00
|
|
|
const Stats& getS3Stats() override { return stats; }
|
2016-04-21 16:02:48 +02:00
|
|
|
|
|
|
|
/* This is a specialisation of isValidPath() that optimistically
|
|
|
|
fetches the .narinfo file, rather than first checking for its
|
|
|
|
existence via a HEAD request. Since .narinfos are small, doing
|
|
|
|
a GET is unlikely to be slower than HEAD. */
|
2016-05-31 13:31:04 +02:00
|
|
|
bool isValidPathUncached(const Path& storePath) override {
|
2016-04-21 16:02:48 +02:00
|
|
|
try {
|
|
|
|
queryPathInfo(storePath);
|
|
|
|
return true;
|
|
|
|
} catch (InvalidPath& e) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 13:31:04 +02:00
|
|
|
bool fileExists(const std::string& path) override {
|
2016-04-21 16:02:48 +02:00
|
|
|
stats.head++;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2017-02-14 14:20:00 +01:00
|
|
|
auto res = s3Helper.client->HeadObject(Aws::S3::Model::HeadObjectRequest()
|
2016-04-21 16:02:48 +02:00
|
|
|
.WithBucket(bucketName)
|
|
|
|
.WithKey(path));
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
auto& error = res.GetError();
|
2017-06-19 18:15:23 +02:00
|
|
|
if (error.GetErrorType() == Aws::S3::S3Errors::RESOURCE_NOT_FOUND ||
|
|
|
|
error.GetErrorType() == Aws::S3::S3Errors::NO_SUCH_KEY
|
2017-11-01 18:33:31 +01:00
|
|
|
// If bucket listing is disabled, 404s turn into 403s
|
|
|
|
|| error.GetErrorType() == Aws::S3::S3Errors::ACCESS_DENIED)
|
2016-04-21 16:02:48 +02:00
|
|
|
return false;
|
2017-07-30 13:27:57 +02:00
|
|
|
throw Error(format("AWS error fetching '%s': %s") % path %
|
|
|
|
error.GetMessage());
|
2016-04-21 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-07 15:23:51 +02:00
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
std::shared_ptr<TransferManager> transferManager;
|
2018-08-09 20:44:18 +02:00
|
|
|
std::once_flag transferManagerCreated;
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
void uploadFile(const std::string& path, const std::string& data,
|
|
|
|
const std::string& mimeType,
|
|
|
|
const std::string& contentEncoding) {
|
|
|
|
auto stream = std::make_shared<istringstream_nocopy>(data);
|
|
|
|
|
|
|
|
auto maxThreads = std::thread::hardware_concurrency();
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
static std::shared_ptr<Aws::Utils::Threading::PooledThreadExecutor>
|
|
|
|
executor =
|
2018-05-07 16:07:00 +02:00
|
|
|
std::make_shared<Aws::Utils::Threading::PooledThreadExecutor>(
|
2018-05-07 15:23:51 +02:00
|
|
|
maxThreads);
|
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
std::call_once(transferManagerCreated, [&]() {
|
|
|
|
if (multipartUpload) {
|
|
|
|
TransferManagerConfiguration transferConfig(executor.get());
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
transferConfig.s3Client = s3Helper.client;
|
|
|
|
transferConfig.bufferSize = bufferSize;
|
2018-08-08 21:39:11 +02:00
|
|
|
|
2018-10-30 14:25:00 +01:00
|
|
|
transferConfig.uploadProgressCallback =
|
|
|
|
[](const TransferManager* transferManager,
|
|
|
|
const std::shared_ptr<const TransferHandle>& transferHandle) {
|
|
|
|
// FIXME: find a way to properly abort the multipart upload.
|
|
|
|
// checkInterrupt();
|
|
|
|
debug("upload progress ('%s'): '%d' of '%d' bytes",
|
|
|
|
transferHandle->GetKey(),
|
|
|
|
transferHandle->GetBytesTransferred(),
|
|
|
|
transferHandle->GetBytesTotalSize());
|
2020-05-17 17:31:57 +02:00
|
|
|
};
|
2018-10-30 14:25:00 +01:00
|
|
|
|
|
|
|
transferManager = TransferManager::Create(transferConfig);
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
});
|
2018-10-30 14:25:00 +01:00
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
if (transferManager) {
|
|
|
|
if (contentEncoding != "")
|
|
|
|
throw Error(
|
|
|
|
"setting a content encoding is not supported with S3 multi-part "
|
|
|
|
"uploads");
|
2018-08-08 21:39:11 +02:00
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
std::shared_ptr<TransferHandle> transferHandle =
|
|
|
|
transferManager->UploadFile(stream, bucketName, path, mimeType,
|
|
|
|
Aws::Map<Aws::String, Aws::String>(),
|
|
|
|
nullptr /*, contentEncoding */);
|
|
|
|
|
2018-05-07 15:23:51 +02:00
|
|
|
transferHandle->WaitUntilFinished();
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-05-07 15:23:51 +02:00
|
|
|
if (transferHandle->GetStatus() == TransferStatus::FAILED)
|
|
|
|
throw Error("AWS error: failed to upload 's3://%s/%s': %s", bucketName,
|
|
|
|
path, transferHandle->GetLastError().GetMessage());
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-08-08 21:39:11 +02:00
|
|
|
if (transferHandle->GetStatus() != TransferStatus::COMPLETED)
|
2018-11-01 15:17:35 +01:00
|
|
|
throw Error(
|
2018-08-08 21:39:11 +02:00
|
|
|
"AWS error: transfer status of 's3://%s/%s' in unexpected state",
|
|
|
|
bucketName, path);
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2017-03-14 15:26:01 +01:00
|
|
|
} else {
|
|
|
|
auto request = Aws::S3::Model::PutObjectRequest()
|
2017-03-15 17:20:19 +01:00
|
|
|
.WithBucket(bucketName)
|
2017-03-14 15:26:01 +01:00
|
|
|
.WithKey(path);
|
2017-03-14 15:03:53 +01:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
request.SetContentType(mimeType);
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
if (contentEncoding != "") {
|
|
|
|
request.SetContentEncoding(contentEncoding);
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
auto stream = std::make_shared<istringstream_nocopy>(data);
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
request.SetBody(stream);
|
2016-04-21 16:02:48 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
auto result = checkAws(fmt("AWS error uploading '%s'", path),
|
|
|
|
s3Helper.client->PutObject(request));
|
2016-04-21 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
2018-05-07 15:23:51 +02:00
|
|
|
auto duration =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1)
|
|
|
|
.count();
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2018-05-07 15:23:51 +02:00
|
|
|
printInfo(format("uploaded 's3://%1%/%2%' (%3% bytes) in %4% ms") %
|
|
|
|
bucketName % path % data.size() % duration);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 16:02:48 +02:00
|
|
|
stats.putTimeMs += duration;
|
2018-08-08 21:39:11 +02:00
|
|
|
stats.putBytes += data.size();
|
2016-04-21 16:02:48 +02:00
|
|
|
stats.put++;
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 15:26:01 +01:00
|
|
|
void upsertFile(const std::string& path, const std::string& data,
|
|
|
|
const std::string& mimeType) override {
|
2020-05-25 03:19:01 +02:00
|
|
|
if (narinfoCompression != "" && absl::EndsWith(path, ".narinfo"))
|
2017-03-15 17:20:19 +01:00
|
|
|
uploadFile(path, *compress(narinfoCompression, data), mimeType,
|
|
|
|
narinfoCompression);
|
2020-05-25 03:19:01 +02:00
|
|
|
else if (lsCompression != "" && absl::EndsWith(path, ".ls"))
|
2017-03-15 17:20:19 +01:00
|
|
|
uploadFile(path, *compress(lsCompression, data), mimeType, lsCompression);
|
2020-05-25 03:19:01 +02:00
|
|
|
else if (logCompression != "" && absl::StartsWith(path, "log/"))
|
2017-03-14 15:26:01 +01:00
|
|
|
uploadFile(path, *compress(logCompression, data), mimeType,
|
|
|
|
logCompression);
|
2020-05-17 17:31:57 +02:00
|
|
|
else
|
2017-03-15 17:20:19 +01:00
|
|
|
uploadFile(path, data, mimeType, "");
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
void getFile(const std::string& path, Sink& sink) override {
|
|
|
|
stats.get++;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
// FIXME: stream output to sink.
|
|
|
|
auto res = s3Helper.getObject(bucketName, path);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
stats.getBytes += res.data ? res.data->size() : 0;
|
|
|
|
stats.getTimeMs += res.durationMs;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
if (res.data) {
|
|
|
|
printTalkative("downloaded 's3://%s/%s' (%d bytes) in %d ms", bucketName,
|
|
|
|
path, res.data->size(), res.durationMs);
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2018-03-27 23:12:31 +02:00
|
|
|
sink((unsigned char*)res.data->data(), res.data->size());
|
2020-05-17 17:31:57 +02:00
|
|
|
} else
|
2018-03-27 23:12:31 +02:00
|
|
|
throw NoSuchBinaryCacheFile(
|
|
|
|
"file '%s' does not exist in binary cache '%s'", path, getUri());
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
PathSet queryAllValidPaths() override {
|
|
|
|
PathSet paths;
|
|
|
|
std::string marker;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
|
|
|
do {
|
2017-07-30 13:27:57 +02:00
|
|
|
debug(format("listing bucket 's3://%s' from key '%s'...") % bucketName %
|
2020-05-17 17:31:57 +02:00
|
|
|
marker);
|
|
|
|
|
2017-07-30 13:27:57 +02:00
|
|
|
auto res = checkAws(
|
|
|
|
format("AWS error listing bucket '%s'") % bucketName,
|
2016-04-21 17:53:47 +02:00
|
|
|
s3Helper.client->ListObjects(Aws::S3::Model::ListObjectsRequest()
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
.WithDelimiter("/")
|
|
|
|
.WithMarker(marker)));
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
auto& contents = res.GetContents();
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2017-07-30 13:27:57 +02:00
|
|
|
debug(format("got %d keys, next marker '%s'") % contents.size() %
|
2016-04-21 17:53:47 +02:00
|
|
|
res.GetNextMarker());
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
for (auto object : contents) {
|
|
|
|
auto& key = object.GetKey();
|
2020-05-25 03:19:01 +02:00
|
|
|
if (key.size() != 40 || !absl::EndsWith(key, ".narinfo")) {
|
2016-04-29 17:34:31 +02:00
|
|
|
continue;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-06-01 14:49:12 +02:00
|
|
|
paths.insert(storeDir + "/" + key.substr(0, key.size() - 8));
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
marker = res.GetNextMarker();
|
|
|
|
} while (!marker.empty());
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2016-04-21 17:53:47 +02:00
|
|
|
return paths;
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2016-04-21 16:02:48 +02:00
|
|
|
};
|
|
|
|
|
2016-04-29 16:26:16 +02:00
|
|
|
static RegisterStoreImplementation regStore(
|
2016-06-01 14:49:12 +02:00
|
|
|
[](const std::string& uri,
|
|
|
|
const Store::Params& params) -> std::shared_ptr<Store> {
|
2016-04-21 16:02:48 +02:00
|
|
|
if (std::string(uri, 0, 5) != "s3://") {
|
|
|
|
return 0;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2016-05-04 20:15:41 +02:00
|
|
|
auto store =
|
|
|
|
std::make_shared<S3BinaryCacheStoreImpl>(params, std::string(uri, 5));
|
2016-04-21 16:02:48 +02:00
|
|
|
store->init();
|
|
|
|
return store;
|
|
|
|
});
|
|
|
|
|
|
|
|
} // namespace nix
|
2016-05-04 17:16:48 +02:00
|
|
|
|
|
|
|
#endif
|