gcs_client: add object_exists helper

Introduce `object_exists` to the GCS client to check whether an object
exists. This is primarily useful for test scenarios.
This commit is contained in:
Ernest Zaslavsky
2026-03-04 18:27:14 +02:00
parent 8c0920202b
commit 016b344a8a
2 changed files with 23 additions and 1 deletions

View File

@@ -1156,6 +1156,25 @@ seekable_data_source utils::gcp::storage::client::create_download_source(std::st
return seekable_data_source(std::make_unique<object_data_source>(_impl, bucket, object_name, as));
}
future<bool> storage::client::object_exists(std::string_view bucket, std::string_view object_name, seastar::abort_source* as) const {
gcp_storage.debug("Get object metadata {}:{}", bucket, object_name);
auto path = fmt::format("/storage/v1/b/{}/o/{}", bucket, seastar::http::internal::url_encode(object_name));
try {
auto res = co_await _impl->send_with_retry(path, GCP_OBJECT_SCOPE_READ_ONLY, ""s, ""s, httpclient::method_type::GET, {}, as);
if (res.result() != status_type::ok) {
throw failed_operation(
fmt::format("Could not retrieve object metadata {}:{}: {} ({})", bucket, object_name, res.result(), get_gcp_error_message(res.body())));
}
} catch (const storage_io_error& e) {
if (e.code().value() == ENOENT) {
co_return false;
}
throw;
}
co_return true;
}
future<> utils::gcp::storage::client::close() {
return _impl->close();
}

View File

@@ -154,7 +154,10 @@ namespace utils::gcp::storage {
* Creates a data_source for reading from a named object.
*/
seekable_data_source create_download_source(std::string_view bucket, std::string_view object_name, seastar::abort_source* = nullptr) const;
/**
* Checks if an object exists.
*/
future<bool> object_exists(std::string_view bucket, std::string_view object_name, seastar::abort_source* as = nullptr) const;
/**
* Destroys resources. Must be called before releasing object
*/