2016-05-04 15:16:48 +00:00
|
|
|
#if ENABLE_S3
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
#include "s3.hh"
|
2016-04-21 14:02:48 +00:00
|
|
|
#include "s3-binary-cache-store.hh"
|
|
|
|
#include "nar-info.hh"
|
|
|
|
#include "nar-info-disk-cache.hh"
|
|
|
|
#include "globals.hh"
|
2017-03-14 14:03:53 +00:00
|
|
|
#include "compression.hh"
|
|
|
|
#include "download.hh"
|
2017-03-21 13:43:03 +00:00
|
|
|
#include "istringstream_nocopy.hh"
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2016-12-22 16:39:05 +00:00
|
|
|
#include <aws/core/Aws.h>
|
2017-12-22 11:05:13 +00:00
|
|
|
#include <aws/core/VersionConfig.h>
|
2017-11-15 13:16:04 +00:00
|
|
|
#include <aws/core/auth/AWSCredentialsProvider.h>
|
|
|
|
#include <aws/core/auth/AWSCredentialsProviderChain.h>
|
2016-04-21 14:02:48 +00:00
|
|
|
#include <aws/core/client/ClientConfiguration.h>
|
2017-02-21 10:42:38 +00:00
|
|
|
#include <aws/core/client/DefaultRetryStrategy.h>
|
2017-06-19 15:01:01 +00:00
|
|
|
#include <aws/core/utils/logging/FormattedLogSystem.h>
|
|
|
|
#include <aws/core/utils/logging/LogMacros.h>
|
2018-05-07 13:23:51 +00:00
|
|
|
#include <aws/core/utils/threading/Executor.h>
|
2016-04-21 14:02:48 +00:00
|
|
|
#include <aws/s3/S3Client.h>
|
|
|
|
#include <aws/s3/model/CreateBucketRequest.h>
|
|
|
|
#include <aws/s3/model/GetBucketLocationRequest.h>
|
|
|
|
#include <aws/s3/model/GetObjectRequest.h>
|
|
|
|
#include <aws/s3/model/HeadObjectRequest.h>
|
2016-04-21 15:53:47 +00:00
|
|
|
#include <aws/s3/model/ListObjectsRequest.h>
|
2016-12-22 16:39:05 +00:00
|
|
|
#include <aws/s3/model/PutObjectRequest.h>
|
2018-05-07 13:23:51 +00:00
|
|
|
#include <aws/transfer/TransferManager.h>
|
|
|
|
|
|
|
|
using namespace Aws::Transfer;
|
2016-04-21 14:02:48 +00: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 15:01:01 +00: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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-22 16:39:05 +00: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 15:01:01 +00: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 16:39:05 +00:00
|
|
|
Aws::InitAPI(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:16:04 +00:00
|
|
|
S3Helper::S3Helper(const std::string & profile, const std::string & region)
|
2017-03-03 21:12:17 +00:00
|
|
|
: config(makeConfig(region))
|
2017-11-15 13:16:04 +00: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 11:05:13 +00: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
|
|
|
|
false))
|
2017-02-14 13:20:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-21 10:42:38 +00:00
|
|
|
/* Log AWS retries. */
|
|
|
|
class RetryStrategy : public Aws::Client::DefaultRetryStrategy
|
|
|
|
{
|
2017-06-19 16:13:32 +00:00
|
|
|
bool ShouldRetry(const Aws::Client::AWSError<Aws::Client::CoreErrors>& error, long attemptedRetries) const override
|
2017-02-21 10:42:38 +00:00
|
|
|
{
|
2017-06-19 16:13:32 +00:00
|
|
|
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 10:42:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-03 21:12:17 +00:00
|
|
|
ref<Aws::Client::ClientConfiguration> S3Helper::makeConfig(const string & region)
|
2017-02-14 13:20:00 +00:00
|
|
|
{
|
|
|
|
initAWS();
|
|
|
|
auto res = make_ref<Aws::Client::ClientConfiguration>();
|
2017-03-03 21:12:17 +00:00
|
|
|
res->region = region;
|
2017-02-14 13:20:00 +00:00
|
|
|
res->requestTimeoutMs = 600 * 1000;
|
2017-02-21 10:42:38 +00:00
|
|
|
res->retryStrategy = std::make_shared<RetryStrategy>();
|
2017-03-06 19:30:35 +00:00
|
|
|
res->caFile = settings.caFile;
|
2017-02-14 13:20:00 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
S3Helper::DownloadResult S3Helper::getObject(
|
|
|
|
const std::string & bucketName, const std::string & key)
|
|
|
|
{
|
2017-07-30 11:27:57 +00:00
|
|
|
debug("fetching 's3://%s/%s'...", bucketName, key);
|
2017-02-14 13:20:00 +00:00
|
|
|
|
|
|
|
auto request =
|
|
|
|
Aws::S3::Model::GetObjectRequest()
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
.WithKey(key);
|
|
|
|
|
|
|
|
request.SetResponseStreamFactory([&]() {
|
|
|
|
return Aws::New<std::stringstream>("STRINGSTREAM");
|
|
|
|
});
|
|
|
|
|
|
|
|
DownloadResult res;
|
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
auto result = checkAws(fmt("AWS error fetching '%s'", key),
|
2017-02-14 13:20:00 +00:00
|
|
|
client->GetObject(request));
|
|
|
|
|
2017-03-14 14:03:53 +00:00
|
|
|
res.data = decodeContent(
|
|
|
|
result.GetContentEncoding(),
|
|
|
|
make_ref<std::string>(
|
|
|
|
dynamic_cast<std::stringstream &>(result.GetBody()).str()));
|
2017-02-14 13:20:00 +00:00
|
|
|
|
|
|
|
} catch (S3Error & e) {
|
|
|
|
if (e.err != Aws::S3::S3Errors::NO_SUCH_KEY) throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
res.durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1).count();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
struct S3BinaryCacheStoreImpl : public S3BinaryCacheStore
|
|
|
|
{
|
2017-11-15 13:16:04 +00:00
|
|
|
const Setting<std::string> profile{this, "", "profile", "The name of the AWS configuration profile to use."};
|
2017-04-13 13:55:38 +00:00
|
|
|
const Setting<std::string> region{this, Aws::Region::US_EAST_1, "region", {"aws-region"}};
|
|
|
|
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-05-07 13:23:51 +00:00
|
|
|
const Setting<uint64_t> bufferSize{
|
2018-05-10 10:06:28 +00:00
|
|
|
this, 5 * 1024 * 1024, "buffer-size", "size (in bytes) of each part in multi-part uploads"};
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
std::string bucketName;
|
|
|
|
|
|
|
|
Stats stats;
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
S3Helper s3Helper;
|
|
|
|
|
2016-05-04 18:15:41 +00:00
|
|
|
S3BinaryCacheStoreImpl(
|
2016-06-01 12:49:12 +00:00
|
|
|
const Params & params, const std::string & bucketName)
|
2016-05-04 18:15:41 +00:00
|
|
|
: S3BinaryCacheStore(params)
|
2016-04-21 14:02:48 +00:00
|
|
|
, bucketName(bucketName)
|
2017-11-15 13:16:04 +00:00
|
|
|
, s3Helper(profile, region)
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
|
|
|
diskCache = getNarInfoDiskCache();
|
|
|
|
}
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
std::string getUri() override
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
|
|
|
return "s3://" + bucketName;
|
|
|
|
}
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
void init() override
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
2016-06-01 13:15:21 +00:00
|
|
|
if (!diskCache->cacheExists(getUri(), wantMassQuery_, priority)) {
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
/* Create the bucket if it doesn't already exists. */
|
|
|
|
// FIXME: HeadBucket would be more appropriate, but doesn't return
|
|
|
|
// an easily parsed 404 message.
|
2017-02-14 13:20:00 +00:00
|
|
|
auto res = s3Helper.client->GetBucketLocation(
|
2016-04-21 14:02:48 +00:00
|
|
|
Aws::S3::Model::GetBucketLocationRequest().WithBucket(bucketName));
|
|
|
|
|
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
if (res.GetError().GetErrorType() != Aws::S3::S3Errors::NO_SUCH_BUCKET)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("AWS error checking bucket '%s': %s") % bucketName % res.GetError().GetMessage());
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
printInfo("creating S3 bucket '%s'...", bucketName);
|
2017-06-19 16:39:57 +00:00
|
|
|
|
|
|
|
// Stupid S3 bucket locations.
|
|
|
|
auto bucketConfig = Aws::S3::Model::CreateBucketConfiguration();
|
|
|
|
if (s3Helper.config->region != "us-east-1")
|
|
|
|
bucketConfig.SetLocationConstraint(
|
|
|
|
Aws::S3::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName(
|
|
|
|
s3Helper.config->region));
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
checkAws(format("AWS error creating bucket '%s'") % bucketName,
|
2017-02-14 13:20:00 +00:00
|
|
|
s3Helper.client->CreateBucket(
|
2016-04-21 14:02:48 +00:00
|
|
|
Aws::S3::Model::CreateBucketRequest()
|
|
|
|
.WithBucket(bucketName)
|
2017-06-19 16:39:57 +00:00
|
|
|
.WithCreateBucketConfiguration(bucketConfig)));
|
2016-04-21 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryCacheStore::init();
|
|
|
|
|
2016-06-01 12:49:12 +00:00
|
|
|
diskCache->createCache(getUri(), storeDir, wantMassQuery_, priority);
|
2016-04-21 14:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 14:34:14 +00:00
|
|
|
const Stats & getS3Stats() override
|
2016-04-21 14:02:48 +00: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. */
|
2016-05-31 11:31:04 +00:00
|
|
|
bool isValidPathUncached(const Path & storePath) override
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
queryPathInfo(storePath);
|
|
|
|
return true;
|
|
|
|
} catch (InvalidPath & e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 11:31:04 +00:00
|
|
|
bool fileExists(const std::string & path) override
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
|
|
|
stats.head++;
|
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
auto res = s3Helper.client->HeadObject(
|
2016-04-21 14:02:48 +00:00
|
|
|
Aws::S3::Model::HeadObjectRequest()
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
.WithKey(path));
|
|
|
|
|
|
|
|
if (!res.IsSuccess()) {
|
|
|
|
auto & error = res.GetError();
|
2017-06-19 16:15:23 +00:00
|
|
|
if (error.GetErrorType() == Aws::S3::S3Errors::RESOURCE_NOT_FOUND
|
|
|
|
|| error.GetErrorType() == Aws::S3::S3Errors::NO_SUCH_KEY
|
2017-11-01 17:33:31 +00:00
|
|
|
// If bucket listing is disabled, 404s turn into 403s
|
|
|
|
|| error.GetErrorType() == Aws::S3::S3Errors::ACCESS_DENIED)
|
2016-04-21 14:02:48 +00:00
|
|
|
return false;
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("AWS error fetching '%s': %s") % path % error.GetMessage());
|
2016-04-21 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:03:53 +00:00
|
|
|
void uploadFile(const std::string & path, const std::string & data,
|
2017-03-14 14:26:01 +00:00
|
|
|
const std::string & mimeType,
|
2017-03-14 14:03:53 +00:00
|
|
|
const std::string & contentEncoding)
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
2018-05-07 13:23:51 +00:00
|
|
|
auto stream = std::make_shared<istringstream_nocopy>(data);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
auto maxThreads = std::thread::hardware_concurrency();
|
2017-03-14 14:26:01 +00:00
|
|
|
|
2018-05-07 14:07:00 +00:00
|
|
|
static std::shared_ptr<Aws::Utils::Threading::PooledThreadExecutor>
|
|
|
|
executor = std::make_shared<Aws::Utils::Threading::PooledThreadExecutor>(maxThreads);
|
2017-03-14 14:03:53 +00:00
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
TransferManagerConfiguration transferConfig(executor.get());
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
transferConfig.s3Client = s3Helper.client;
|
|
|
|
transferConfig.bufferSize = bufferSize;
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
if (contentEncoding != "")
|
|
|
|
transferConfig.createMultipartUploadTemplate.SetContentEncoding(
|
|
|
|
contentEncoding);
|
|
|
|
|
|
|
|
transferConfig.uploadProgressCallback =
|
|
|
|
[&](const TransferManager *transferManager,
|
|
|
|
const std::shared_ptr<const TransferHandle>
|
2018-05-07 13:27:53 +00:00
|
|
|
&transferHandle) {
|
|
|
|
//FIXME: find a way to properly abort the multipart upload.
|
2018-05-07 13:23:51 +00:00
|
|
|
checkInterrupt();
|
|
|
|
printTalkative("upload progress ('%s'): '%d' of '%d' bytes",
|
|
|
|
path,
|
|
|
|
transferHandle->GetBytesTransferred(),
|
|
|
|
transferHandle->GetBytesTotalSize());
|
|
|
|
};
|
|
|
|
|
|
|
|
transferConfig.transferStatusUpdatedCallback =
|
|
|
|
[&](const TransferManager *,
|
|
|
|
const std::shared_ptr<const TransferHandle>
|
|
|
|
&transferHandle) {
|
|
|
|
switch (transferHandle->GetStatus()) {
|
|
|
|
case TransferStatus::COMPLETED:
|
|
|
|
printTalkative("upload of '%s' completed", path);
|
|
|
|
stats.put++;
|
|
|
|
stats.putBytes += data.size();
|
|
|
|
break;
|
|
|
|
case TransferStatus::IN_PROGRESS:
|
|
|
|
break;
|
|
|
|
case TransferStatus::FAILED:
|
|
|
|
throw Error("AWS error: failed to upload 's3://%s/%s'",
|
|
|
|
bucketName, path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Error("AWS error: transfer status of 's3://%s/%s' "
|
|
|
|
"in unexpected state",
|
|
|
|
bucketName, path);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<TransferManager> transferManager =
|
|
|
|
TransferManager::Create(transferConfig);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
auto now1 = std::chrono::steady_clock::now();
|
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
std::shared_ptr<TransferHandle> transferHandle =
|
|
|
|
transferManager->UploadFile(stream, bucketName, path, mimeType,
|
|
|
|
Aws::Map<Aws::String, Aws::String>());
|
|
|
|
|
|
|
|
transferHandle->WaitUntilFinished();
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
auto now2 = std::chrono::steady_clock::now();
|
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
auto duration =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now2 - now1)
|
|
|
|
.count();
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2018-05-07 13:23:51 +00:00
|
|
|
printInfo(format("uploaded 's3://%1%/%2%' (%3% bytes) in %4% ms") %
|
|
|
|
bucketName % path % data.size() % duration);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
|
|
|
stats.putTimeMs += duration;
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:26:01 +00:00
|
|
|
void upsertFile(const std::string & path, const std::string & data,
|
|
|
|
const std::string & mimeType) override
|
2017-03-14 14:03:53 +00:00
|
|
|
{
|
2017-03-15 16:20:19 +00:00
|
|
|
if (narinfoCompression != "" && hasSuffix(path, ".narinfo"))
|
|
|
|
uploadFile(path, *compress(narinfoCompression, data), mimeType, narinfoCompression);
|
|
|
|
else if (lsCompression != "" && hasSuffix(path, ".ls"))
|
|
|
|
uploadFile(path, *compress(lsCompression, data), mimeType, lsCompression);
|
2017-03-14 14:55:02 +00:00
|
|
|
else if (logCompression != "" && hasPrefix(path, "log/"))
|
2017-03-14 14:26:01 +00:00
|
|
|
uploadFile(path, *compress(logCompression, data), mimeType, logCompression);
|
2017-03-14 14:03:53 +00:00
|
|
|
else
|
2017-03-14 14:26:01 +00:00
|
|
|
uploadFile(path, data, mimeType, "");
|
2017-03-14 14:03:53 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 16:54:14 +00:00
|
|
|
void getFile(const std::string & path,
|
2018-03-27 20:16:01 +00:00
|
|
|
Callback<std::shared_ptr<std::string>> callback) override
|
2016-04-21 14:02:48 +00:00
|
|
|
{
|
2018-03-27 20:16:01 +00:00
|
|
|
try {
|
2016-09-16 16:54:14 +00:00
|
|
|
stats.get++;
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
auto res = s3Helper.getObject(bucketName, path);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
stats.getBytes += res.data ? res.data->size() : 0;
|
|
|
|
stats.getTimeMs += res.durationMs;
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2017-02-14 13:20:00 +00:00
|
|
|
if (res.data)
|
2017-07-30 11:27:57 +00:00
|
|
|
printTalkative("downloaded 's3://%s/%s' (%d bytes) in %d ms",
|
2017-02-14 13:20:00 +00:00
|
|
|
bucketName, path, res.data->size(), res.durationMs);
|
2016-04-21 14:02:48 +00:00
|
|
|
|
2018-03-27 20:16:01 +00:00
|
|
|
callback(std::move(res.data));
|
|
|
|
} catch (...) { callback.rethrow(); }
|
2016-04-21 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 15:53:47 +00:00
|
|
|
PathSet queryAllValidPaths() override
|
|
|
|
{
|
|
|
|
PathSet paths;
|
|
|
|
std::string marker;
|
|
|
|
|
|
|
|
do {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("listing bucket 's3://%s' from key '%s'...") % bucketName % marker);
|
2016-04-21 15:53:47 +00:00
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
auto res = checkAws(format("AWS error listing bucket '%s'") % bucketName,
|
2017-02-14 13:20:00 +00:00
|
|
|
s3Helper.client->ListObjects(
|
2016-04-21 15:53:47 +00:00
|
|
|
Aws::S3::Model::ListObjectsRequest()
|
|
|
|
.WithBucket(bucketName)
|
|
|
|
.WithDelimiter("/")
|
|
|
|
.WithMarker(marker)));
|
|
|
|
|
|
|
|
auto & contents = res.GetContents();
|
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("got %d keys, next marker '%s'")
|
2016-04-21 15:53:47 +00:00
|
|
|
% contents.size() % res.GetNextMarker());
|
|
|
|
|
|
|
|
for (auto object : contents) {
|
|
|
|
auto & key = object.GetKey();
|
2016-04-29 15:34:31 +00:00
|
|
|
if (key.size() != 40 || !hasSuffix(key, ".narinfo")) continue;
|
2016-06-01 12:49:12 +00:00
|
|
|
paths.insert(storeDir + "/" + key.substr(0, key.size() - 8));
|
2016-04-21 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
marker = res.GetNextMarker();
|
|
|
|
} while (!marker.empty());
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2016-04-21 14:02:48 +00:00
|
|
|
};
|
|
|
|
|
2016-04-29 14:26:16 +00:00
|
|
|
static RegisterStoreImplementation regStore([](
|
2016-06-01 12:49:12 +00:00
|
|
|
const std::string & uri, const Store::Params & params)
|
2016-04-29 14:26:16 +00:00
|
|
|
-> std::shared_ptr<Store>
|
|
|
|
{
|
2016-04-21 14:02:48 +00:00
|
|
|
if (std::string(uri, 0, 5) != "s3://") return 0;
|
2016-05-04 18:15:41 +00:00
|
|
|
auto store = std::make_shared<S3BinaryCacheStoreImpl>(params, std::string(uri, 5));
|
2016-04-21 14:02:48 +00:00
|
|
|
store->init();
|
|
|
|
return store;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2016-05-04 15:16:48 +00:00
|
|
|
|
|
|
|
#endif
|