2016-05-04 18:16:48 +03:00
|
|
|
|
#if ENABLE_S3
|
|
|
|
|
|
2017-02-14 15:20:00 +02:00
|
|
|
|
#include "s3.hh"
|
2016-04-21 17:02:48 +03:00
|
|
|
|
#include "s3-binary-cache-store.hh"
|
|
|
|
|
#include "nar-info.hh"
|
|
|
|
|
#include "nar-info-disk-cache.hh"
|
|
|
|
|
#include "globals.hh"
|
2017-03-14 16:03:53 +02:00
|
|
|
|
#include "compression.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
|
#include "filetransfer.hh"
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2016-12-22 18:39:05 +02:00
|
|
|
|
#include <aws/core/Aws.h>
|
2017-12-22 13:05:13 +02:00
|
|
|
|
#include <aws/core/VersionConfig.h>
|
2017-11-15 15:16:04 +02:00
|
|
|
|
#include <aws/core/auth/AWSCredentialsProvider.h>
|
|
|
|
|
#include <aws/core/auth/AWSCredentialsProviderChain.h>
|
2016-04-21 17:02:48 +03:00
|
|
|
|
#include <aws/core/client/ClientConfiguration.h>
|
2017-02-21 12:42:38 +02:00
|
|
|
|
#include <aws/core/client/DefaultRetryStrategy.h>
|
2017-06-19 18:01:01 +03:00
|
|
|
|
#include <aws/core/utils/logging/FormattedLogSystem.h>
|
|
|
|
|
#include <aws/core/utils/logging/LogMacros.h>
|
2018-05-07 16:23:51 +03:00
|
|
|
|
#include <aws/core/utils/threading/Executor.h>
|
2016-04-21 17:02:48 +03:00
|
|
|
|
#include <aws/s3/S3Client.h>
|
|
|
|
|
#include <aws/s3/model/GetObjectRequest.h>
|
|
|
|
|
#include <aws/s3/model/HeadObjectRequest.h>
|
2016-04-21 18:53:47 +03:00
|
|
|
|
#include <aws/s3/model/ListObjectsRequest.h>
|
2016-12-22 18:39:05 +02:00
|
|
|
|
#include <aws/s3/model/PutObjectRequest.h>
|
2018-05-07 16:23:51 +03:00
|
|
|
|
#include <aws/transfer/TransferManager.h>
|
|
|
|
|
|
|
|
|
|
using namespace Aws::Transfer;
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
struct S3Error : public Error
|
|
|
|
|
{
|
|
|
|
|
Aws::S3::S3Errors err;
|
2020-04-22 02:07:07 +03:00
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
|
S3Error(Aws::S3::S3Errors err, const Args & ... args)
|
|
|
|
|
: Error(args...), err(err) { };
|
2016-04-21 17:02:48 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* 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>
|
2023-03-02 16:44:19 +02:00
|
|
|
|
R && checkAws(std::string_view s, Aws::Utils::Outcome<R, E> && outcome)
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
if (!outcome.IsSuccess())
|
|
|
|
|
throw S3Error(
|
|
|
|
|
outcome.GetError().GetErrorType(),
|
2023-03-02 16:44:19 +02:00
|
|
|
|
s + ": " + outcome.GetError().GetMessage());
|
2016-04-21 17:02:48 +03:00
|
|
|
|
return outcome.GetResultWithOwnership();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 18:01:01 +03:00
|
|
|
|
class AwsLogger : public Aws::Utils::Logging::FormattedLogSystem
|
|
|
|
|
{
|
|
|
|
|
using Aws::Utils::Logging::FormattedLogSystem::FormattedLogSystem;
|
|
|
|
|
|
|
|
|
|
void ProcessFormattedStatement(Aws::String && statement) override
|
|
|
|
|
{
|
|
|
|
|
debug("AWS: %s", chomp(statement));
|
|
|
|
|
}
|
2020-12-04 20:32:35 +02:00
|
|
|
|
|
|
|
|
|
#if !(AWS_VERSION_MAJOR <= 1 && AWS_VERSION_MINOR <= 7 && AWS_VERSION_PATCH <= 115)
|
|
|
|
|
void Flush() override {}
|
|
|
|
|
#endif
|
2017-06-19 18:01:01 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-22 18:39:05 +02:00
|
|
|
|
static void initAWS()
|
|
|
|
|
{
|
|
|
|
|
static std::once_flag flag;
|
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
|
Aws::SDKOptions options;
|
|
|
|
|
|
|
|
|
|
/* We install our own OpenSSL locking function (see
|
|
|
|
|
shared.cc), so don't let aws-sdk-cpp override it. */
|
|
|
|
|
options.cryptoOptions.initAndCleanupOpenSSL = false;
|
|
|
|
|
|
2017-06-19 18:01:01 +03: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 18:39:05 +02:00
|
|
|
|
Aws::InitAPI(options);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
|
S3Helper::S3Helper(
|
|
|
|
|
const std::string & profile,
|
|
|
|
|
const std::string & region,
|
|
|
|
|
const std::string & scheme,
|
|
|
|
|
const std::string & endpoint)
|
2018-12-08 00:38:24 +02:00
|
|
|
|
: config(makeConfig(region, scheme, endpoint))
|
2017-11-15 15:16:04 +02: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 13:05:13 +02: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 23:45:49 +03:00
|
|
|
|
endpoint.empty()))
|
2017-02-14 15:20:00 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 12:42:38 +02:00
|
|
|
|
/* Log AWS retries. */
|
|
|
|
|
class RetryStrategy : public Aws::Client::DefaultRetryStrategy
|
|
|
|
|
{
|
2017-06-19 19:13:32 +03:00
|
|
|
|
bool ShouldRetry(const Aws::Client::AWSError<Aws::Client::CoreErrors>& error, long attemptedRetries) const override
|
2017-02-21 12:42:38 +02:00
|
|
|
|
{
|
2017-06-19 19:13:32 +03:00
|
|
|
|
auto retry = Aws::Client::DefaultRetryStrategy::ShouldRetry(error, attemptedRetries);
|
|
|
|
|
if (retry)
|
|
|
|
|
printError("AWS error '%s' (%s), will retry in %d ms",
|
2020-05-03 17:01:25 +03:00
|
|
|
|
error.GetExceptionName(),
|
|
|
|
|
error.GetMessage(),
|
|
|
|
|
CalculateDelayBeforeNextRetry(error, attemptedRetries));
|
2017-06-19 19:13:32 +03:00
|
|
|
|
return retry;
|
2017-02-21 12:42:38 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
|
ref<Aws::Client::ClientConfiguration> S3Helper::makeConfig(
|
|
|
|
|
const std::string & region,
|
|
|
|
|
const std::string & scheme,
|
|
|
|
|
const std::string & endpoint)
|
2017-02-14 15:20:00 +02:00
|
|
|
|
{
|
|
|
|
|
initAWS();
|
|
|
|
|
auto res = make_ref<Aws::Client::ClientConfiguration>();
|
2017-03-03 23:12:17 +02:00
|
|
|
|
res->region = region;
|
2018-12-08 00:38:24 +02:00
|
|
|
|
if (!scheme.empty()) {
|
|
|
|
|
res->scheme = Aws::Http::SchemeMapper::FromString(scheme.c_str());
|
|
|
|
|
}
|
2018-07-31 23:45:49 +03:00
|
|
|
|
if (!endpoint.empty()) {
|
|
|
|
|
res->endpointOverride = endpoint;
|
|
|
|
|
}
|
2017-02-14 15:20:00 +02:00
|
|
|
|
res->requestTimeoutMs = 600 * 1000;
|
2019-03-15 14:16:20 +02:00
|
|
|
|
res->connectTimeoutMs = 5 * 1000;
|
2017-02-21 12:42:38 +02:00
|
|
|
|
res->retryStrategy = std::make_shared<RetryStrategy>();
|
2017-03-06 21:30:35 +02:00
|
|
|
|
res->caFile = settings.caFile;
|
2017-02-14 15:20:00 +02:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
|
S3Helper::FileTransferResult S3Helper::getObject(
|
2017-02-14 15:20:00 +02:00
|
|
|
|
const std::string & bucketName, const std::string & key)
|
|
|
|
|
{
|
2017-07-30 14:27:57 +03:00
|
|
|
|
debug("fetching 's3://%s/%s'...", bucketName, key);
|
2017-02-14 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
auto request =
|
|
|
|
|
Aws::S3::Model::GetObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(key);
|
|
|
|
|
|
|
|
|
|
request.SetResponseStreamFactory([&]() {
|
|
|
|
|
return Aws::New<std::stringstream>("STRINGSTREAM");
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
|
FileTransferResult res;
|
2017-02-14 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
2017-07-30 14:27:57 +03:00
|
|
|
|
auto result = checkAws(fmt("AWS error fetching '%s'", key),
|
2017-02-14 15:20:00 +02:00
|
|
|
|
client->GetObject(request));
|
|
|
|
|
|
2018-08-06 16:40:29 +03:00
|
|
|
|
res.data = decompress(result.GetContentEncoding(),
|
|
|
|
|
dynamic_cast<std::stringstream &>(result.GetBody()).str());
|
2017-02-14 15:20:00 +02:00
|
|
|
|
|
|
|
|
|
} catch (S3Error & e) {
|
2020-08-28 16:28:35 +03:00
|
|
|
|
if ((e.err != Aws::S3::S3Errors::NO_SUCH_KEY) &&
|
|
|
|
|
(e.err != Aws::S3::S3Errors::ACCESS_DENIED)) throw;
|
2017-02-14 15:20:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
res.durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 17:33:12 +02:00
|
|
|
|
S3BinaryCacheStore::S3BinaryCacheStore(const Params & params)
|
|
|
|
|
: BinaryCacheStoreConfig(params)
|
|
|
|
|
, BinaryCacheStore(params)
|
|
|
|
|
{ }
|
|
|
|
|
|
2020-09-10 11:55:51 +03:00
|
|
|
|
struct S3BinaryCacheStoreConfig : virtual BinaryCacheStoreConfig
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
2020-09-10 11:55:51 +03:00
|
|
|
|
using BinaryCacheStoreConfig::BinaryCacheStoreConfig;
|
2023-03-22 15:23:36 +02:00
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> profile{this, "", "profile",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
R"(
|
|
|
|
|
The name of the AWS configuration profile to use. By default
|
|
|
|
|
Nix will use the `default` profile.
|
|
|
|
|
)"};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> region{this, Aws::Region::US_EAST_1, "region",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
R"(
|
|
|
|
|
The region of the S3 bucket. If your bucket is not in
|
|
|
|
|
`us–east-1`, you should always explicitly specify the region
|
|
|
|
|
parameter.
|
|
|
|
|
)"};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> scheme{this, "", "scheme",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
R"(
|
|
|
|
|
The scheme used for S3 requests, `https` (default) or `http`. This
|
|
|
|
|
option allows you to disable HTTPS for binary caches which don't
|
|
|
|
|
support it.
|
|
|
|
|
|
|
|
|
|
> **Note**
|
|
|
|
|
>
|
|
|
|
|
> HTTPS should be used if the cache might contain sensitive
|
|
|
|
|
> information.
|
|
|
|
|
)"};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> endpoint{this, "", "endpoint",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
R"(
|
|
|
|
|
The URL of the endpoint of an S3-compatible service such as MinIO.
|
|
|
|
|
Do not specify this setting if you're using Amazon S3.
|
|
|
|
|
|
|
|
|
|
> **Note**
|
|
|
|
|
>
|
|
|
|
|
> This endpoint must support HTTPS and will use path-based
|
|
|
|
|
> addressing instead of virtual host based addressing.
|
|
|
|
|
)"};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> narinfoCompression{this, "", "narinfo-compression",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
"Compression method for `.narinfo` files."};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> lsCompression{this, "", "ls-compression",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
"Compression method for `.ls` files."};
|
|
|
|
|
|
2023-10-31 00:12:37 +02:00
|
|
|
|
const Setting<std::string> logCompression{this, "", "log-compression",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
R"(
|
|
|
|
|
Compression method for `log/*` files. It is recommended to
|
|
|
|
|
use a compression method supported by most web browsers
|
|
|
|
|
(e.g. `brotli`).
|
|
|
|
|
)"};
|
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
const Setting<bool> multipartUpload{
|
2023-10-31 00:12:37 +02:00
|
|
|
|
this, false, "multipart-upload",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
"Whether to use multi-part uploads."};
|
|
|
|
|
|
2018-05-07 16:23:51 +03:00
|
|
|
|
const Setting<uint64_t> bufferSize{
|
2023-10-31 00:12:37 +02:00
|
|
|
|
this, 5 * 1024 * 1024, "buffer-size",
|
2023-03-22 15:23:36 +02:00
|
|
|
|
"Size (in bytes) of each part in multi-part uploads."};
|
2020-09-14 15:04:02 +03:00
|
|
|
|
|
|
|
|
|
const std::string name() override { return "S3 Binary Cache Store"; }
|
2023-03-21 15:03:05 +02:00
|
|
|
|
|
|
|
|
|
std::string doc() override
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
#include "s3-binary-cache-store.md"
|
|
|
|
|
;
|
|
|
|
|
}
|
2020-09-10 11:55:51 +03:00
|
|
|
|
};
|
2017-04-13 16:55:38 +03:00
|
|
|
|
|
2020-12-20 17:33:12 +02:00
|
|
|
|
struct S3BinaryCacheStoreImpl : virtual S3BinaryCacheStoreConfig, public virtual S3BinaryCacheStore
|
2020-09-10 11:55:51 +03:00
|
|
|
|
{
|
2016-04-21 17:02:48 +03:00
|
|
|
|
std::string bucketName;
|
|
|
|
|
|
|
|
|
|
Stats stats;
|
|
|
|
|
|
2017-02-14 15:20:00 +02:00
|
|
|
|
S3Helper s3Helper;
|
|
|
|
|
|
2016-05-04 21:15:41 +03:00
|
|
|
|
S3BinaryCacheStoreImpl(
|
2021-09-17 11:45:19 +03:00
|
|
|
|
const std::string & uriScheme,
|
2020-09-08 15:50:23 +03:00
|
|
|
|
const std::string & bucketName,
|
|
|
|
|
const Params & params)
|
2020-09-11 12:06:18 +03:00
|
|
|
|
: StoreConfig(params)
|
2020-12-20 17:33:12 +02:00
|
|
|
|
, BinaryCacheStoreConfig(params)
|
|
|
|
|
, S3BinaryCacheStoreConfig(params)
|
|
|
|
|
, Store(params)
|
|
|
|
|
, BinaryCacheStore(params)
|
2020-09-10 11:55:51 +03:00
|
|
|
|
, S3BinaryCacheStore(params)
|
2016-04-21 17:02:48 +03:00
|
|
|
|
, bucketName(bucketName)
|
2018-12-08 00:38:24 +02:00
|
|
|
|
, s3Helper(profile, region, scheme, endpoint)
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
diskCache = getNarInfoDiskCache();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 14:31:04 +03:00
|
|
|
|
std::string getUri() override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
return "s3://" + bucketName;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 14:31:04 +03:00
|
|
|
|
void init() override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
2023-01-17 20:54:47 +02:00
|
|
|
|
if (auto cacheInfo = diskCache->upToDateCacheExists(getUri())) {
|
2021-09-22 15:15:35 +03:00
|
|
|
|
wantMassQuery.setDefault(cacheInfo->wantMassQuery);
|
|
|
|
|
priority.setDefault(cacheInfo->priority);
|
2019-12-17 18:17:53 +02:00
|
|
|
|
} else {
|
2016-04-21 17:02:48 +03:00
|
|
|
|
BinaryCacheStore::init();
|
2019-12-17 18:17:53 +02:00
|
|
|
|
diskCache->createCache(getUri(), storeDir, wantMassQuery, priority);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 17:34:14 +03:00
|
|
|
|
const Stats & getS3Stats() override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
return stats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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. */
|
2019-12-05 20:11:09 +02:00
|
|
|
|
bool isValidPathUncached(const StorePath & storePath) override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
queryPathInfo(storePath);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (InvalidPath & e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 14:31:04 +03:00
|
|
|
|
bool fileExists(const std::string & path) override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
|
|
|
|
stats.head++;
|
|
|
|
|
|
2017-02-14 15:20:00 +02:00
|
|
|
|
auto res = s3Helper.client->HeadObject(
|
2016-04-21 17:02:48 +03:00
|
|
|
|
Aws::S3::Model::HeadObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(path));
|
|
|
|
|
|
|
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
|
auto & error = res.GetError();
|
2017-06-19 19:15:23 +03:00
|
|
|
|
if (error.GetErrorType() == Aws::S3::S3Errors::RESOURCE_NOT_FOUND
|
|
|
|
|
|| error.GetErrorType() == Aws::S3::S3Errors::NO_SUCH_KEY
|
2017-11-01 19:33:31 +02:00
|
|
|
|
// If bucket listing is disabled, 404s turn into 403s
|
|
|
|
|
|| error.GetErrorType() == Aws::S3::S3Errors::ACCESS_DENIED)
|
2016-04-21 17:02:48 +03:00
|
|
|
|
return false;
|
2020-04-22 02:07:07 +03:00
|
|
|
|
throw Error("AWS error fetching '%s': %s", path, error.GetMessage());
|
2016-04-21 17:02:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 21:44:18 +03:00
|
|
|
|
std::shared_ptr<TransferManager> transferManager;
|
|
|
|
|
std::once_flag transferManagerCreated;
|
|
|
|
|
|
2020-07-13 21:07:19 +03:00
|
|
|
|
void uploadFile(const std::string & path,
|
|
|
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
2017-03-14 16:26:01 +02:00
|
|
|
|
const std::string & mimeType,
|
2017-03-14 16:03:53 +02:00
|
|
|
|
const std::string & contentEncoding)
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
2020-08-04 17:00:59 +03:00
|
|
|
|
istream->seekg(0, istream->end);
|
|
|
|
|
auto size = istream->tellg();
|
|
|
|
|
istream->seekg(0, istream->beg);
|
|
|
|
|
|
2018-05-07 16:23:51 +03:00
|
|
|
|
auto maxThreads = std::thread::hardware_concurrency();
|
2017-03-14 16:26:01 +02:00
|
|
|
|
|
2018-05-07 17:07:00 +03:00
|
|
|
|
static std::shared_ptr<Aws::Utils::Threading::PooledThreadExecutor>
|
|
|
|
|
executor = std::make_shared<Aws::Utils::Threading::PooledThreadExecutor>(maxThreads);
|
2017-03-14 16:03:53 +02:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
std::call_once(transferManagerCreated, [&]()
|
|
|
|
|
{
|
|
|
|
|
if (multipartUpload) {
|
|
|
|
|
TransferManagerConfiguration transferConfig(executor.get());
|
|
|
|
|
|
|
|
|
|
transferConfig.s3Client = s3Helper.client;
|
|
|
|
|
transferConfig.bufferSize = bufferSize;
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
transferManager = TransferManager::Create(transferConfig);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-08-09 21:44:18 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
2018-08-09 21:44:18 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
if (transferManager) {
|
2018-08-09 21:44:18 +03:00
|
|
|
|
|
2018-11-01 16:17:35 +02:00
|
|
|
|
if (contentEncoding != "")
|
|
|
|
|
throw Error("setting a content encoding is not supported with S3 multi-part uploads");
|
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
std::shared_ptr<TransferHandle> transferHandle =
|
|
|
|
|
transferManager->UploadFile(
|
2020-07-13 21:07:19 +03:00
|
|
|
|
istream, bucketName, path, mimeType,
|
2018-10-30 15:25:00 +02:00
|
|
|
|
Aws::Map<Aws::String, Aws::String>(),
|
2018-11-01 16:17:35 +02:00
|
|
|
|
nullptr /*, contentEncoding */);
|
2018-05-07 16:23:51 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
transferHandle->WaitUntilFinished();
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
if (transferHandle->GetStatus() == TransferStatus::FAILED)
|
|
|
|
|
throw Error("AWS error: failed to upload 's3://%s/%s': %s",
|
|
|
|
|
bucketName, path, transferHandle->GetLastError().GetMessage());
|
|
|
|
|
|
|
|
|
|
if (transferHandle->GetStatus() != TransferStatus::COMPLETED)
|
|
|
|
|
throw Error("AWS error: transfer status of 's3://%s/%s' in unexpected state",
|
|
|
|
|
bucketName, path);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
} else {
|
2018-05-07 16:23:51 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
auto request =
|
|
|
|
|
Aws::S3::Model::PutObjectRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithKey(path);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
request.SetContentType(mimeType);
|
2018-08-08 22:39:11 +03:00
|
|
|
|
|
2018-10-30 15:25:00 +02:00
|
|
|
|
if (contentEncoding != "")
|
|
|
|
|
request.SetContentEncoding(contentEncoding);
|
|
|
|
|
|
2020-07-13 21:07:19 +03:00
|
|
|
|
request.SetBody(istream);
|
2018-10-30 15:25:00 +02:00
|
|
|
|
|
|
|
|
|
auto result = checkAws(fmt("AWS error uploading '%s'", path),
|
|
|
|
|
s3Helper.client->PutObject(request));
|
|
|
|
|
}
|
2018-08-08 22:39:11 +03:00
|
|
|
|
|
2016-04-21 17:02:48 +03:00
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
2018-05-07 16:23:51 +03:00
|
|
|
|
auto duration =
|
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1)
|
|
|
|
|
.count();
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2020-08-04 16:56:10 +03:00
|
|
|
|
printInfo("uploaded 's3://%s/%s' (%d bytes) in %d ms",
|
|
|
|
|
bucketName, path, size, duration);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
|
|
|
|
stats.putTimeMs += duration;
|
2020-08-04 17:00:59 +03:00
|
|
|
|
stats.putBytes += std::max(size, (decltype(size)) 0);
|
2018-08-08 22:39:11 +03:00
|
|
|
|
stats.put++;
|
2016-04-21 17:02:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 21:07:19 +03:00
|
|
|
|
void upsertFile(const std::string & path,
|
|
|
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
2017-03-14 16:26:01 +02:00
|
|
|
|
const std::string & mimeType) override
|
2017-03-14 16:03:53 +02:00
|
|
|
|
{
|
2020-07-13 21:07:19 +03:00
|
|
|
|
auto compress = [&](std::string compression)
|
|
|
|
|
{
|
|
|
|
|
auto compressed = nix::compress(compression, StreamToSourceAdapter(istream).drain());
|
2022-01-17 23:20:05 +02:00
|
|
|
|
return std::make_shared<std::stringstream>(std::move(compressed));
|
2020-07-13 21:07:19 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-03-15 18:20:19 +02:00
|
|
|
|
if (narinfoCompression != "" && hasSuffix(path, ".narinfo"))
|
2020-07-13 21:07:19 +03:00
|
|
|
|
uploadFile(path, compress(narinfoCompression), mimeType, narinfoCompression);
|
2017-03-15 18:20:19 +02:00
|
|
|
|
else if (lsCompression != "" && hasSuffix(path, ".ls"))
|
2020-07-13 21:07:19 +03:00
|
|
|
|
uploadFile(path, compress(lsCompression), mimeType, lsCompression);
|
2017-03-14 16:55:02 +02:00
|
|
|
|
else if (logCompression != "" && hasPrefix(path, "log/"))
|
2020-07-13 21:07:19 +03:00
|
|
|
|
uploadFile(path, compress(logCompression), mimeType, logCompression);
|
2017-03-14 16:03:53 +02:00
|
|
|
|
else
|
2020-07-13 21:07:19 +03:00
|
|
|
|
uploadFile(path, istream, mimeType, "");
|
2017-03-14 16:03:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 00:12:31 +03:00
|
|
|
|
void getFile(const std::string & path, Sink & sink) override
|
2016-04-21 17:02:48 +03:00
|
|
|
|
{
|
2018-03-28 00:12:31 +03:00
|
|
|
|
stats.get++;
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-03-28 00:12:31 +03:00
|
|
|
|
// FIXME: stream output to sink.
|
|
|
|
|
auto res = s3Helper.getObject(bucketName, path);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-03-28 00:12:31 +03:00
|
|
|
|
stats.getBytes += res.data ? res.data->size() : 0;
|
|
|
|
|
stats.getTimeMs += res.durationMs;
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2018-03-28 00:12:31 +03:00
|
|
|
|
if (res.data) {
|
|
|
|
|
printTalkative("downloaded 's3://%s/%s' (%d bytes) in %d ms",
|
|
|
|
|
bucketName, path, res.data->size(), res.durationMs);
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
2020-12-02 15:00:43 +02:00
|
|
|
|
sink(*res.data);
|
2018-03-28 00:12:31 +03:00
|
|
|
|
} else
|
|
|
|
|
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache '%s'", path, getUri());
|
2016-04-21 17:02:48 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
|
StorePathSet queryAllValidPaths() override
|
2016-04-21 18:53:47 +03:00
|
|
|
|
{
|
2019-12-05 20:11:09 +02:00
|
|
|
|
StorePathSet paths;
|
2016-04-21 18:53:47 +03:00
|
|
|
|
std::string marker;
|
|
|
|
|
|
|
|
|
|
do {
|
2023-03-02 16:44:19 +02:00
|
|
|
|
debug("listing bucket 's3://%s' from key '%s'...", bucketName, marker);
|
2016-04-21 18:53:47 +03:00
|
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
|
auto res = checkAws(fmt("AWS error listing bucket '%s'", bucketName),
|
2017-02-14 15:20:00 +02:00
|
|
|
|
s3Helper.client->ListObjects(
|
2016-04-21 18:53:47 +03:00
|
|
|
|
Aws::S3::Model::ListObjectsRequest()
|
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
|
.WithDelimiter("/")
|
|
|
|
|
.WithMarker(marker)));
|
|
|
|
|
|
|
|
|
|
auto & contents = res.GetContents();
|
|
|
|
|
|
2023-03-02 16:44:19 +02:00
|
|
|
|
debug("got %d keys, next marker '%s'",
|
|
|
|
|
contents.size(), res.GetNextMarker());
|
2016-04-21 18:53:47 +03:00
|
|
|
|
|
|
|
|
|
for (auto object : contents) {
|
|
|
|
|
auto & key = object.GetKey();
|
2016-04-29 18:34:31 +03:00
|
|
|
|
if (key.size() != 40 || !hasSuffix(key, ".narinfo")) continue;
|
2020-07-13 15:35:01 +03:00
|
|
|
|
paths.insert(parseStorePath(storeDir + "/" + key.substr(0, key.size() - 8) + "-" + MissingName));
|
2016-04-21 18:53:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marker = res.GetNextMarker();
|
|
|
|
|
} while (!marker.empty());
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 22:21:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* For now, we conservatively say we don't know.
|
|
|
|
|
*
|
|
|
|
|
* \todo try to expose our S3 authentication status.
|
|
|
|
|
*/
|
|
|
|
|
std::optional<TrustedFlag> isTrustedClient() override
|
|
|
|
|
{
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 12:11:05 +03:00
|
|
|
|
static std::set<std::string> uriSchemes() { return {"s3"}; }
|
2020-09-08 15:50:23 +03:00
|
|
|
|
|
2016-04-21 17:02:48 +03:00
|
|
|
|
};
|
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
|
static RegisterStoreImplementation<S3BinaryCacheStoreImpl, S3BinaryCacheStoreConfig> regS3BinaryCacheStore;
|
2016-04-21 17:02:48 +03:00
|
|
|
|
|
|
|
|
|
}
|
2016-05-04 18:16:48 +03:00
|
|
|
|
|
|
|
|
|
#endif
|