Idiomatic Ruby client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services:
- Google Cloud BigQuery
- Google Cloud Datastore
- Google Cloud DNS
- Google Cloud Pub/Sub
- Google Cloud Storage
If you need support for other Google APIs, check out the Google API Ruby Client library.
$ gem install gcloud
Gcloud uses Service Account credentials to connect to Google Cloud services. When running on Compute Engine the credentials will be discovered automatically. When running on other environments the Service Account credentials can be specified by providing the path to the JSON file, or the JSON itself, in environment variables. Additionally, Cloud SDK credentials can also be discovered automatically, but this is only recommended during development.
Instructions and configuration options are covered in the Authentication guide. The examples in Quick Start will demonstrate providing the Project ID and Credentials JSON file path in code.
require "gcloud"
gcloud = Gcloud.new "my-todo-project-id",
"/path/to/keyfile.json"
bigquery = gcloud.bigquery
# Create a new table to archive todos
dataset = bigquery.dataset "my-todo-archive"
table = dataset.create_table "todos",
name: "Todos Archive",
description: "Archive for completed TODO records"
# Load data into the table
file = File.open "/archive/todos/completed-todos.csv"
load_job = table.load file
# Run a query for the number of completed todos by owner
count_sql = "SELECT owner, COUNT(*) AS complete_count FROM todos GROUP BY owner"
data = bigquery.query count_sql
data.each do |row|
puts row["name"]
end
Follow the activation instructions to use the Google Cloud Datastore API with your project.
require "gcloud"
gcloud = Gcloud.new "my-todo-project-id",
"/path/to/keyfile.json"
dataset = gcloud.datastore
# Create a new task to demo datastore
demo_task = Gcloud::Datastore::Entity.new
demo_task.key = Gcloud::Datastore::Key.new "Task", "datastore-demo"
demo_task[:description] = "Demonstrate Datastore functionality"
demo_task[:completed] = false
# Save the new task
dataset.save demo_task
# Run a query for all completed tasks
query = Gcloud::Datastore::Query.new.kind("Task").
where("completed", "=", true)
completed_tasks = dataset.run query
require "gcloud"
gcloud = Gcloud.new
dns = gcloud.dns
# Retrieve a zone
zone = dns.zone "example-com"
# Update records in the zone
change = zone.update do |tx|
tx.add "www", "A", 86400, "1.2.3.4"
tx.remove "example.com.", "TXT"
tx.replace "example.com.", "MX", 86400, ["10 mail1.example.com.",
"20 mail2.example.com."]
tx.modify "www.example.com.", "CNAME" do |r|
r.ttl = 86400 # only change the TTL
end
end
require "gcloud"
gcloud = Gcloud.new "my-todo-project-id",
"/path/to/keyfile.json"
pubsub = gcloud.pubsub
# Retrieve a topic
topic = pubsub.topic "my-topic"
# Publish a new message
msg = topic.publish "new-message"
# Retrieve a subscription
sub = pubsub.subscription "my-topic-sub"
# Pull available messages
msgs = sub.pull
require "gcloud"
gcloud = Gcloud.new "my-todo-project-id",
"/path/to/keyfile.json"
storage = gcloud.storage
bucket = storage.bucket "task-attachments"
file = bucket.file "path/to/my-file.ext"
# Download the file to the local file system
file.download "/tasks/attachments/#{file.name}"
# Copy the file to a backup bucket
backup = storage.bucket "task-attachment-backups"
file.copy backup, file.name
gcloud is supported on Ruby 1.9.3+.
This library follows Semantic Versioning.
It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
Please report bugs at the project on Github. Don't hesitate to ask questions about the client or APIs on StackOverflow.