|
| 1 | +# Google Cloud Platform C++ Client Libraries: Core Concepts |
| 2 | + |
| 3 | +This documentation covers essential patterns and usage for the Google Cloud C++ |
| 4 | +Client Library, focusing on performance, data handling (`StatusOr`), and flow |
| 5 | +control (Pagination, Futures, Streaming). |
| 6 | + |
| 7 | +## 1. Installation & Setup |
| 8 | + |
| 9 | +The C++ libraries are typically installed via **vcpkg** or **Conda**, or |
| 10 | +compiled from source using **CMake**. |
| 11 | + |
| 12 | +**Example using vcpkg:** |
| 13 | + |
| 14 | +``` |
| 15 | +vcpkg install google-cloud-cpp |
| 16 | +``` |
| 17 | + |
| 18 | +**CMakeLists.txt:** |
| 19 | + |
| 20 | +```c |
| 21 | +find_package(google_cloud_cpp_pubsub REQUIRED) |
| 22 | +add_executable(my_app main.cc) |
| 23 | +target_link_libraries(my_app google-cloud-cpp::pubsub) |
| 24 | +``` |
| 25 | +
|
| 26 | +## 2. StatusOr\<T> and Error Handling |
| 27 | +
|
| 28 | +C++ does not use exceptions for API errors by default. Instead, it uses |
| 29 | +`google::cloud::StatusOr<T>`. |
| 30 | +
|
| 31 | +- **Success:** The object contains the requested value. |
| 32 | +- **Failure:** The object contains a `Status` (error code and message). |
| 33 | +
|
| 34 | +```c |
| 35 | +void HandleResponse(google::cloud::StatusOr<std::string> response) { |
| 36 | + if (!response) { |
| 37 | + // Handle error |
| 38 | + std::cerr << "RPC failed: " << response.status().message() << "\n"; |
| 39 | + return; |
| 40 | + } |
| 41 | + // Access value |
| 42 | + std::cout << "Success: " << *response << "\n"; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## 3. Pagination (StreamRange) |
| 47 | + |
| 48 | +List methods in C++ return a `google::cloud::StreamRange<T>`. This works like |
| 49 | +standard C++ input iterator. The library automatically fetches new pages in the |
| 50 | +background as you iterate. |
| 51 | + |
| 52 | +```c |
| 53 | +#include "google/cloud/secretmanager/secret_manager_client.h" |
| 54 | + |
| 55 | +void ListSecrets(google::cloud::secretmanager::SecretManagerServiceClient client) { |
| 56 | + // Call the API |
| 57 | + // Returns StreamRange<google::cloud::secretmanager::v1::Secret> |
| 58 | + auto range = client.ListSecrets("projects/my-project"); |
| 59 | + |
| 60 | + for (auto const& secret : range) { |
| 61 | + if (!secret) { |
| 62 | + // StreamRange returns StatusOr<T> on dereference to handle failures mid-stream |
| 63 | + std::cerr << "Error listing secret: " << secret.status() << "\n"; |
| 64 | + break; |
| 65 | + } |
| 66 | + std::cout << "Secret: " << secret->name() << "\n"; |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +
|
| 71 | +## 4. Long Running Operations (LROs) |
| 72 | +
|
| 73 | +LROs in C++ return a `std::future<StatusOr<T>>`. |
| 74 | +
|
| 75 | +### Blocking Wait |
| 76 | +
|
| 77 | +```c |
| 78 | +#include "google/cloud/compute/instances_client.h" |
| 79 | +
|
| 80 | +void CreateInstance(google::cloud::compute::InstancesClient client) { |
| 81 | + google::cloud::compute::v1::InsertInstanceRequest request; |
| 82 | + // ... set request fields ... |
| 83 | +
|
| 84 | + // Start the operation |
| 85 | + // Returns future<StatusOr<Operation>> |
| 86 | + auto future = client.InsertInstance(request); |
| 87 | +
|
| 88 | + // Block until complete |
| 89 | + auto result = future.get(); |
| 90 | +
|
| 91 | + if (!result) { |
| 92 | + std::cerr << "Creation failed: " << result.status() << "\n"; |
| 93 | + } else { |
| 94 | + std::cout << "Instance created successfully\n"; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Async / Non-Blocking |
| 100 | + |
| 101 | +You can use standard C++ `future` capabilities, such as polling `wait_for` or |
| 102 | +attaching continuations (via `.then` if using the library's future extension, |
| 103 | +though standard `std::future` is strictly blocking/polling). |
| 104 | + |
| 105 | +## 5. Update Masks |
| 106 | + |
| 107 | +The C++ libraries use `google::protobuf::FieldMask`. |
| 108 | + |
| 109 | +```c |
| 110 | +#include "google/cloud/secretmanager/secret_manager_client.h" |
| 111 | +#include <google/protobuf/field_mask.pb.h> |
| 112 | + |
| 113 | +void UpdateSecret(google::cloud::secretmanager::SecretManagerServiceClient client) { |
| 114 | + namespace secretmanager = ::google::cloud::secretmanager::v1; |
| 115 | + |
| 116 | + secretmanager::Secret secret; |
| 117 | + secret.set_name("projects/my-project/secrets/my-secret"); |
| 118 | + (*secret.mutable_labels())["env"] = "production"; |
| 119 | + |
| 120 | + google::protobuf::FieldMask update_mask; |
| 121 | + update_mask.add_paths("labels"); |
| 122 | + |
| 123 | + secretmanager::UpdateSecretRequest request; |
| 124 | + *request.mutable_secret() = secret; |
| 125 | + *request.mutable_update_mask() = update_mask; |
| 126 | + |
| 127 | + auto result = client.UpdateSecret(request); |
| 128 | +} |
| 129 | +``` |
| 130 | +
|
| 131 | +## 6. gRPC Streaming |
| 132 | +
|
| 133 | +### Server-Side Streaming |
| 134 | +
|
| 135 | +Similar to pagination, Server-Side streaming usually returns a `StreamRange` or |
| 136 | +a specialized reader object. |
| 137 | +
|
| 138 | +```c |
| 139 | +#include "google/cloud/bigquery/storage/bigquery_read_client.h" |
| 140 | +
|
| 141 | +void ReadRows(google::cloud::bigquery_storage::BigQueryReadClient client) { |
| 142 | + google::cloud::bigquery::storage::v1::ReadRowsRequest request; |
| 143 | + request.set_read_stream("projects/.../streams/..."); |
| 144 | +
|
| 145 | + // Returns a StreamRange of ReadRowsResponse |
| 146 | + auto stream = client.ReadRows(request); |
| 147 | +
|
| 148 | + for (auto const& response : stream) { |
| 149 | + if (!response) { |
| 150 | + std::cerr << "Error reading row: " << response.status() << "\n"; |
| 151 | + break; |
| 152 | + } |
| 153 | + // Process response->avro_rows() |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Bidirectional Streaming |
| 159 | + |
| 160 | +Bidirectional streaming uses a `AsyncReaderWriter` pattern (or synchronous |
| 161 | +`ReaderWriter`). |
| 162 | + |
| 163 | +```c |
| 164 | +#include "google/cloud/speech/speech_client.h" |
| 165 | + |
| 166 | +void StreamingRecognize(google::cloud::speech::SpeechClient client) { |
| 167 | + // Start the stream |
| 168 | + auto stream = client.StreamingRecognize(); |
| 169 | + |
| 170 | + // 1. Send Config |
| 171 | + google::cloud::speech::v1::StreamingRecognizeRequest config_request; |
| 172 | + // ... configure ... |
| 173 | + stream->Write(config_request); |
| 174 | + |
| 175 | + // 2. Send Audio |
| 176 | + google::cloud::speech::v1::StreamingRecognizeRequest audio_request; |
| 177 | + // ... load audio data ... |
| 178 | + stream->Write(audio_request); |
| 179 | + |
| 180 | + // 3. Close writing to signal we are done sending |
| 181 | + stream->WritesDone(); |
| 182 | + |
| 183 | + // 4. Read responses |
| 184 | + google::cloud::speech::v1::StreamingRecognizeResponse response; |
| 185 | + while (stream->Read(&response)) { |
| 186 | + for (const auto& result : response.results()) { |
| 187 | + std::cout << "Transcript: " << result.alternatives(0).transcript() << "\n"; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Check final status |
| 192 | + auto status = stream->Finish(); |
| 193 | +} |
| 194 | +``` |
0 commit comments