ELIXIR ON GOOGLE CLOUD PLATFORM

A community-supported project

QuickStart Community Tutorials

Build, Deploy, Debug, and Monitor Highly Scalable Elixir Apps

  • Access GCP services while using Elixir as a programming language to build your apps
  • Build planet-scale fault-tolerant apps
  • Use Google’s APIs with native library support
  • Run containerized applications
  • Unlock the power of Google’s Machine Learning APIs
BEAUTIFUL HEX PACKAGES
Upload files to Google Cloud Storage
Analyze Audio with the Cloud Speech API
Using Google BigQuery
Upload Files to Google Cloud Storage
1
Install
Add google_cloud_storage as a dependency in mix.exs
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Storage API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

3
Write your code
@doc """
List storage buckets for a project.

## Examples

    iex> GoogleApi.Storage.Samples.upload_file("bucket_id", "test/file.txt")
    Uploaded file.text to  https://www.googleapis.com/storage/v1/b/bucket_id/o/file.txt
    your_project_bucket2

"""
def upload_file(bucket_id, file_path) do
  # Authenticate.
  {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
  conn = GoogleApi.Storage.V1.Connection.new(token.token)

  # Make the API request.
  {:ok, object} = GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple(
    conn,
    bucket_id,
    "multipart",
    %{name: Path.basename(file_path)},
    file_path
  )

  # Print the object.
  IO.puts("Uploaded #{object.name} to #{object.selfLink}")
end
Analyze Audio with the Cloud Speech API
1
Install
Add google_cloud_speech as a dependency in mix.exs
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Speech API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

3
Write your code
@doc """
Transcribe text from an audio file.

## Examples

    iex> GoogleApi.Speech.Samples.recognize("gs://elixir-samples/audio.raw", :LINEAR16, 16000)
    how old is the Brooklyn Bridge
    :ok

"""
def recognize(audio_uri, encoding, sampleRateHertz, languageCode \\ "en_US") do
  # Build the RecognizeRequest struct
  request = %GoogleApi.Speech.V1.Model.RecognizeRequest{
    audio: %GoogleApi.Speech.V1.Model.RecognitionAudio{uri: audio_uri},
    config: %GoogleApi.Speech.V1.Model.RecognitionConfig{
      encoding:  encoding,
      sampleRateHertz: sampleRateHertz,
      languageCode: languageCode
    }
  }

  # Authenticate
  {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
  conn = GoogleApi.Speech.V1.Connection.new(token.token)

  # Make the API request.
  {:ok, response} = GoogleApi.Speech.V1.Api.Speech.speech_speech_recognize(
    conn,
    [body: request]
  )

  # Print the results.
  Enum.each(response.results, fn(result) ->
    Enum.each(result.alternatives, fn(alt) ->
      IO.puts(alt.transcript)
    end)
  end)
end
Using Google BigQuery
1
Install
Add google_cloud_big_query as a dependency in mix.exs
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the BigQuery API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key.

3
Write your code
@doc """
List BigQuery buckets for a project.

## Examples

    iex> sql = "SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words
                FROM [publicdata:samples.shakespeare]"
    iex> GoogleApi.BigQuery.Samples.sync_query(project_id, sql)

"""
def sync_query(project_id, sql) do
  # Fetch access token
  {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
  conn = GoogleApi.BigQuery.V2.Connection.new(token.token)

  # Make the API request
  {:ok, response} = GoogleApi.BigQuery.V2.Api.Jobs.bigquery_jobs_query(
    conn,
    project_id,
    [body: %GoogleApi.BigQuery.V2.Model.QueryRequest{ query: sql }]
  )
  response.rows
  |> Enum.each(fn(row) ->
    row.f
    |> Enum.with_index
    |> Enum.each(fn({cell, i}) ->
      IO.puts "#{Enum.at(response.schema.fields, i).name}: #{cell.v}"
    end)
  end)
end
ELIXIR QUICK STARTS
QUICKLY FIND AND DEBUG ISSUES

Google Stackdriver provides powerful monitoring, logging, and diagnostics. It equips you with insight into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues faster.

Google Stackdriver
Unified monitoring, logging, and diagnostics for applications on Google Cloud Platform and AWS.
Stackdriver Error Reporting
A walk through of getting an error alert and investigating the error in the Google Cloud Console.
Stackdriver Monitor, diagnose, and fix
In this video, Aja Hammerly uses Stackdriver to find and fix some subtle errors in an example app, and you'll learn how to use Stackdriver on your own projects.
Learn more
ELIXIR ON GCP COMMUNITIES

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build the support for Elixir on Google Cloud Platform.