你能在 C++ 中隐式地将选项传递给 ListObjects 吗?

问题描述 投票:0回答:1

我需要能够将各种选项传递给

ListObjects()
,例如
MaxResults()
Prefix()
StartOffset()

每次

ListObjects()
调用时选项的组合可能有所不同,因此是否有一个结构或对象可以初始化选项并传递给 ListObjects?

例如:

struct/object opts;
opts.MaxResults = 100;
opts.Prefix = "test-";
opts.StartOffset = "test-1";

client->ListObjects(bucket, opts);

编辑:

我有一个选项结构:

struct list_objects_options {
    std::optional<gcs::MaxResults> count;
    std::optional<gcs::Delimiter> filter;
};

我有许多针对不同选项输入的重载函数:

list_objects(std::size_t count, std::string filter) {
    list_objects_options opts;
    opts.count = gcs::MaxResults(count);
    opts.filter = gcs::Delimiter(filter);
    return list_objects(opts);
}
list_objects(std::string filter) {
    ...
} 

哪个电话进入:

list_objects(list_objects_options opts) {
    google::cloud::StatusOr<gcs::ListObjectsReader> response = client->ListObjects(m_bucket, opts);
}

但是,使用 CMake 构建时出现错误 没有匹配的函数调用‘google::cloud::storage::v2_14::internal::GenericRequest<...>::set_option(list_objects_options&)’

c++ google-cloud-platform google-cloud-storage
1个回答
0
投票

在 Google Cloud Storage C++ SDK 中,任何默认初始化的“选项”都无效。像这样的东西应该有效:

namespace gcs = google::cloud::storage;

struct list_object_options {
  gcs::Prefix prefix;
  gcs::Delimiter delimiter;
  gcs::MaxResults max_results; // rarely useful.
};

void F(gcs::Client c, std::string const& bucket, list_object_options o) {
  for (auto&& object : c.ListObjects(bucket, o.prefix, o.delimiter, o.max_results)) {
     // Do stuff with `object`
   }
}

void G() {
  auto client = gcs::Client();
  list_object_options o; // default initialized options have no effect
  o.prefix = "my-prefix";
  F(client, "my-bucket", o);
}

© www.soinside.com 2019 - 2024. All rights reserved.