Connect to Cloud Storage using gRPC

gRPC is a high performance, open source universal RPC framework developed by Google that you can use to define your services using Protocol Buffers. You can use gRPC to interact with Cloud Storage. gRPC utilizes direct connectivity between Compute Engine instances and Cloud Storage buckets, bypassing Google Front Ends (GFEs).

You can connect to Cloud Storage using gRPC through the following supported clients:

Enable gRPC on a client library

C++

Before you begin

  1. Ensure you have the following versions installed:

    • gRPC version 1.65.1 or later

    • C++ client library version v2.30.0 or later

    • C++ version 14 or later

    For installation instructions, see Setting up a C++ development environment.

  2. Set up authentication.

  3. Ensure each Compute Engine instance has a service account attached to it, even if the service account has no permissions. This service account is used to represent the Compute Engine instance in the Application Layer Transport Security (ALTS) handshake process and is required for direct connectivity.

Configure the C++ client library

  1. Create a gRPC client using gcs::MakeGrpcClient():

      namespace gcs = google::cloud::storage;
      void App() {
       auto client = gcs::MakeGrpcClient();
    
      // application code
    
      }
    

    The C++ client library automatically uses direct connectivity when it detects that the application is running on Google Cloud.

  2. To configure the C++ client library to use gRPC, enable the Cloud Storage gRPC client to update your build system configuration for CMake or Bazel.

    CMake

    1. Enable the Cloud Storage gRPC client plugin at compile-time.

      cmake -DGOOGLE_CLOUD_CPP_ENABLE=storage_grpc [other options here]
      
    2. In your codebase, for the target_link_libraries() command replace google-cloud-cpp::storage with google-cloud-cpp::storage_grpc

      For example, the quickstart program for gRPC uses the following code:

      add_executable(quickstart_grpc quickstart_grpc.cc)
      target_link_libraries(quickstart_grpc google-cloud-cpp::storage_grpc)
      

    Bazel

    Replace the dependencies from @google_cloud_cpp//:storage to @google_cloud_cpp//:storage_grpc.

    For example, the quickstart program for gRPC uses the following code:

      cc_binary(
          name = "quickstart",
          srcs = [
              "quickstart.cc",
          ],
          deps = [
              "@com_github_googleapis_google_cloud_cpp//:storage_grpc",
          ],
      )
    

Java

Before you begin

  1. Ensure you have the following versions installed:

    • Java client libraries:

      • com.google.cloud:google-cloud-storage:2.43.1 or later.
      • com.google.cloud:libraries-bom:26.48 or later.
    • Java 8 or later

    For installation instructions, see Setting up a Java development environment.

  2. Set up authentication.

  3. Ensure each Compute Engine instance has a service account attached to it, even if the service account has no permissions. This service account is used to represent the Compute Engine instance in the Application Layer Transport Security (ALTS) handshake process and is required for direct connectivity.

Update your project to use the BOM

To make sure that your projects have compatible versions of Google Cloud client libraries, use the versions specified in the Google Cloud libraries Bill of Materials (BOM). To update your project to use the BOM, use any of the following methods:

Standalone Cloud Storage

If you are using the Cloud Storage client library independently (without other Google Cloud libraries), use the Cloud Storage client library-specific BOM.

Maven

Import the BOM in the dependencyManagement section of your pom.xml file.

The following example shows how you would import the BOM and include the google-cloud-storage artifact.

<dependencyManagement>
  <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage-bom</artifactId>
   <version>2.43.1</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
  </dependency>
</dependencies>

Gradle

Add a platform dependency on com.google.cloud:google-cloud-storage-bom:

implementation platform('com.google.cloud:google-cloud-storage-bom:2.43.1')
implementation 'com.google.cloud:google-cloud-storage'

Cloud Storage with other Google Cloud libraries

If you are using the Cloud Storage client library along with other Google Cloud libraries, use the Google Cloud client libraries BOM.

Maven

Import the BOM in the dependencyManagement section of your pom.xml file.

The following example shows how to import the BOM and include the libraries-bom artifact.

<dependencyManagement>
  <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>libraries-bom</artifactId>
   <version>26.48.0</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
  </dependency>
</dependencies>

Gradle

Add a platform dependency on com.google.cloud:libraries-bom:

implementation platform('com.google.cloud:libraries-bom:26.48.0')
implementation 'com.google.cloud:google-cloud-storage'

Create a gRPC client

The following sample uses a gRPC centric builder. The gRPC Java client automatically uses direct connectivity when it detects that the application is running on Google Cloud.

// Imports the Google Cloud client library
import com.google.cloud.storage.Bucket