Connect to Google Cloud Storage Data in JRuby

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Google Cloud Storage JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Google Cloud Storage.



Create a simple JRuby app with access to live Google Cloud Storage data.

JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for Google Cloud Storage makes it easy to integrate connectivity to live Google Cloud Storage data in JRuby. This article shows how to create a simple JRuby app that connects to Google Cloud Storage data, executes a query, and displays the results.

Configure a JDBC Connection to Google Cloud Storage Data

Before creating the app, note the installation location for the JAR file for the JDBC Driver (typically C:\Program Files\CData\CData JDBC Driver for Google Cloud Storage\lib).

JRuby natively supports JDBC, so you can easily connect to Google Cloud Storage and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.

Authenticate with a User Account

You can connect without setting any connection properties for your user credentials. After setting InitiateOAuth to GETANDREFRESH, you are ready to connect.

When you connect, the Google Cloud Storage OAuth endpoint opens in your default browser. Log in and grant permissions, then the OAuth process completes

Authenticate with a Service Account

Service accounts have silent authentication, without user authentication in the browser. You can also use a service account to delegate enterprise-wide access scopes.

You need to create an OAuth application in this flow. See the Help documentation for more information. After setting the following connection properties, you are ready to connect:

  • InitiateOAuth: Set this to GETANDREFRESH.
  • OAuthJWTCertType: Set this to "PFXFILE".
  • OAuthJWTCert: Set this to the path to the .p12 file you generated.
  • OAuthJWTCertPassword: Set this to the password of the .p12 file.
  • OAuthJWTCertSubject: Set this to "*" to pick the first certificate in the certificate store.
  • OAuthJWTIssuer: In the service accounts section, click Manage Service Accounts and set this field to the email address displayed in the service account Id field.
  • OAuthJWTSubject: Set this to your enterprise Id if your subject type is set to "enterprise" or your app user Id if your subject type is set to "user".
  • ProjectId: Set this to the Id of the project you want to connect to.

The OAuth flow for a service account then completes.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Google Cloud Storage JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.googlecloudstorage.jar

Fill in the connection properties and copy the connection string to the clipboard.

Below is a typical JDBC connection string for Google Cloud Storage:

jdbc:googlecloudstorage:ProjectId='project1';InitiateOAuth=GETANDREFRESH

Create a JRuby App with Connectivity to Google Cloud Storage Data

Create a new Ruby file (for example: GoogleCloudStorageSelect.rb) and open it in a text editor. Copy the following code into your file:

require 'java' require 'rubygems' require 'C:/Program Files/CData/CData JDBC Driver for Google Cloud Storage 2018/lib/cdata.jdbc.googlecloudstorage.jar' url = "jdbc:googlecloudstorage:ProjectId='project1';InitiateOAuth=GETANDREFRESH" conn = java.sql.DriverManager.getConnection(url) stmt = conn.createStatement rs = stmt.executeQuery("SELECT Name, OwnerId FROM Buckets") while (rs.next) do puts rs.getString(1) + ' ' + rs.getString(2) end

With the file completed, you are ready to display your Google Cloud Storage data with JRuby. To do so, simply run your file from the command line:

jruby -S GoogleCloudStorageSelect.rb

Writing SQL-92 queries to Google Cloud Storage allows you to quickly and easily incorporate Google Cloud Storage data into your own JRuby applications. Download a free trial today!