What is the expected behavior for ListObjects in GCP with a non existent path? (C++)

Viewed 36

I expected the following C++/GCP code to return an empty list for a path which doesn't exist, but it doesn't seem to do so in practice:

for (auto&& object_metadata :
                 cli->ListObjects(bucket, path))
{
    // how many time will this be hit?
}

Does anyone know what is the expected behavior? I couldn't find this in the documentation (perhaps I didn't look in the right place)

1 Answers

TL;DR; the expected behavior is to return an empty set if the request is successful but there are no objects with that path, and to return a single element (with the error) if the request fails.

Loosely speaking, ListObjects() returns an input range of google::cloud::StatusOr<google::cloud::storage::ObjectMetadata>. If (for example) the bucket does not exist, then you get a single element, and the element contains the error. A bit off-topic: the request may fail even after returning some objects, under the hood there is a pagination request, and fetching a new page may fail.

for (auto o : cli->ListObjects(bucket, gcs::Prefix(path))) {
  if (!o) throw std::move(o).status();
  // this is reached only if the request is successful
  // *and* there are objects in `path`
}
Related