Google Cloud Storage C++ Client Library
The Google Cloud Storage (GCS) C++ Client library offers types and functions access GCS from C++ applications. It offers full access to the GCS API, including operations to list, read, write, and delete GCS objects and buckets. The library also provides functions to modify the IAM permissions on buckets, read and modify the metadata associated with objects and buckets, configure encryption keys, configure notifications via Cloud Pub/Sub, and change the access control list of object or buckets.
Quickstart
The following "Hello World" program should give you a taste of this library. This program is also used to illustrate how to incorporate the library into your project.
#include "google/cloud/storage/client.h"
#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Missing bucket name.\n";
std::cerr << "Usage: quickstart <bucket-name>\n";
return 1;
}
std::string const bucket_name = argv[1];
// Create a client to communicate with Google Cloud Storage. This client
// uses the default configuration for authentication and project id.
auto client = google::cloud::storage::Client();
auto writer = client.WriteObject(bucket_name, "quickstart.txt");
writer << "Hello World!";
writer.Close();
if (!writer.metadata()) {
std::cerr << "Error creating object: " << writer.metadata().status()
<< "\n";
return 1;
}
std::cout << "Successfully created object: " << *writer.metadata() << "\n";
auto reader = client.ReadObject(bucket_name, "quickstart.txt");
if (!reader) {
std::cerr << "Error reading object: " << reader.status() << "\n";
return 1;
}
std::string contents{std::istreambuf_iterator<char>{reader}, {}};
std::cout << contents << "\n";
return 0;
}
More Information
- ReadObject() - the function to read object data.
- WriteObject() - the function to write objects.
- InsertObject() - the function to write small objects.
- Error Handling to learn how the library reports run-time errors.
- Environment Variables for environment variables affecting the library. Some of these environment variables enable logging to the console. This can be an effective approach to diagnose runtime problems.
- Override the Default Endpoint
- Override the authentication configuration
- Override the default retry policies
- Writing Tests with a Mock Client shows how to write tests mocking the Client class
- The Setting up your development environment guide describes how to set up a C++ development environment in various platforms, including the Google Cloud C++ client libraries.