Issue
This Content is from Stack Overflow. Question asked by Alon Granot
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)
Solution
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`
}
This Question was asked in StackOverflow by Alon Granot and Answered by coryan It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.