【问题标题】:With c++ S3 SDK, ListObjecsV2 with Minio returning no results使用 c++ S3 SDK,带有 Minio 的 ListObjecsV2 不返回任何结果
【发布时间】:2020-08-23 02:12:22
【问题描述】:

下面是一些代码,它将一个对象放入一个 s3 存储桶,将其取回,然后尝试列出该存储桶。 put 和 get 都可以正常工作,但列出存储桶不会返回任何键(但不会出错)。我可以使用 golang api 和相同的参数(存储桶名称和空前缀)列出存储桶。为什么不列出存储桶?

Aws::SDKOptions opts;

opts.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug;
Aws::InitAPI(opts);

Aws::Auth::AWSCredentials credentials;
Aws::Client::ClientConfiguration config;
Aws::S3::S3Client client;

credentials.SetAWSAccessKeyId("accesskey");
credentials.SetAWSSecretKey("secretkey");
config.endpointOverride = "localhost:5553";
config.scheme = Aws::Http::Scheme::HTTP;
config.verifySSL = false;
client = Aws::S3::S3Client(credentials, config);

const char* bucket = "buck";
const char* key = "buck/key";

// Putting works!
//
Aws::S3::Model::PutObjectRequest pRequest;
pRequest.SetBucket(bucket);
pRequest.SetKey(key);

auto stream = Aws::MakeShared<Aws::StringStream>("tag", "some data");
pRequest.SetBody(stream);

Aws::S3::Model::PutObjectOutcome pOutcome = client.PutObject(pRequest);

if (!pOutcome.IsSuccess())
{
    printf("put failed S3 because %s: %s\n",
           pOutcome.GetError().GetExceptionName().c_str(),
           pOutcome.GetError().GetMessage().c_str());
    printf("\n\n\n\n");
    assert(false);
}

printf("object put\n");

// Getting works!
//
Aws::S3::Model::GetObjectRequest gRequest;
gRequest.SetBucket(bucket);
gRequest.SetKey(key);

Aws::S3::Model::GetObjectOutcome gOutcome = client.GetObject(gRequest);

if (!gOutcome.IsSuccess())
{
    printf("get failed S3 because %s: %s\n",
           gOutcome.GetError().GetExceptionName().c_str(),
           gOutcome.GetError().GetMessage().c_str());
    printf("\n\n\n\n");
    assert(false);
}

printf("object got\n");

char buf[1000];
memset(buf, 0, 1000);
gOutcome.GetResult().GetBody().read(buf, 1000);

printf("object say '%s'\n", buf);

// THIS IS THE PROBLEMATIC CODE
//
Aws::S3::Model::ListObjectsV2Request request;
request.SetBucket(bucket);                
request.SetPrefix("");
request.SetMaxKeys(1000);

Aws::S3::Model::ListObjectsV2Outcome outcome = client.ListObjectsV2(request);

if (!outcome.IsSuccess())
{
    printf("Failed to list files from S3 because %s: %s\n",
           outcome.GetError().GetExceptionName().c_str(),
           outcome.GetError().GetMessage().c_str());
    printf("\n\n\n\n");
    assert(false);
}

printf("num keys returned %d\n", outcome.GetResult().GetKeyCount());

for (const Aws::S3::Model::Object& obj : outcome.GetResult().GetContents())
{
    printf("Callback %s", obj.GetKey().c_str());
}

Aws::Utils::Logging::ShutdownAWSLogging();

exit(0);

我使用 minio 作为我的对象存储,但使用 amazon s3 可以正常工作

【问题讨论】:

    标签: amazon-s3 aws-sdk-cpp


    【解决方案1】:

    查看http请求的区别,阅读代码,看来需要在S3Client中设置m_useVirtualAddressing为true。

    好吧……谁知道呢。

    我的客户说

       client = Aws::S3::S3Client(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2021-08-09
      • 2013-06-15
      • 2017-01-16
      相关资源
      最近更新 更多