Skip to content

Commit

Permalink
Add gcptest
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Gouache committed Apr 19, 2024
1 parent 15d73c1 commit dc75333
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ target_link_libraries(Demo
DLib
)

add_executable(GCPTest src/gcptest.cpp)
target_link_libraries(GCPTest
PRIVATE CURL::libcurl google-cloud-cpp::storage google-cloud-cpp::storage_protos google-cloud-cpp::experimental-storage-grpc spdlog::spdlog_header_only)
52 changes: 52 additions & 0 deletions src/gcptest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "google/cloud/storage/client.h"
#include <iostream>

bool ParseGcsUri(const std::string& gcs_uri, std::string& bucket_name, std::string& object_name) {
const std::string prefix = "gs://";
if (gcs_uri.compare(0, prefix.size(), prefix) != 0) {
std::cerr << "Invalid GCS URI: " << gcs_uri << "\n";
return false;
}

std::size_t pos = gcs_uri.find('/', prefix.size());
if (pos == std::string::npos) {
std::cerr << "Invalid GCS URI, missing object name: " << gcs_uri << "\n";
return false;
}

bucket_name = gcs_uri.substr(prefix.size(), pos - prefix.size());
object_name = gcs_uri.substr(pos + 1);

return true;
}


int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Missing URI.\n";
std::cerr << "Usage: GCPTest URI\n";
return 1;
}
std::string const uri = argv[1];
std::string bucket_name, object_name;
ParseGcsUri(uri, bucket_name, object_name);

// Create aliases to make the code easier to read.
namespace gcs = ::google::cloud::storage;

// Create a client to communicate with Google Cloud Storage. This client
// uses the default configuration for authentication and project id.
auto client = gcs::Client();

auto object_metadata = client.GetObjectMetadata(bucket_name, object_name);
if (object_metadata) {
std::cout << "Object size: " << object_metadata->size() << "\n";
} else if (object_metadata.status().code() == google::cloud::StatusCode::kNotFound) {
std::cout << "Object not found!\n";

} else {
std::cerr << "Error checking object: " << object_metadata.status().message() << "\n";
}

return 0;
}

0 comments on commit dc75333

Please sign in to comment.