Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Using Cloud Storage

You can use Cloud Storage to store and serve files, such as movies or images or other static content.

This document describes how to use the Google Cloud Client Library in your app to store data in and retrieve data from Cloud Storage.

Before you begin

  • Follow the instructions in "Hello, World!" for Ruby on App Engine to set up your environment and project, and to understand how Ruby apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.

  • Make sure you create a Cloud Storage bucket for your application by invoking the following command:

    gsutil mb gs://[YOUR_BUCKET_NAME]
  • Make the bucket publicly readable so it can serve files:

    gsutil defacl set public-read gs://[YOUR_BUCKET_NAME]

Download the sample

To clone the repository:

git clone
cd ruby-docs-samples/appengine/storage

Edit project configuration and install dependencies

In app.yaml, set the GCLOUD_STORAGE_BUCKET to the Cloud Storage you previously created for your project.

runtime: ruby
env: flex
entrypoint: bundle exec ruby app.rb

  GOOGLE_CLOUD_STORAGE_BUCKET: <your-bucket-name>

Note that you must include the gcloud library in Gemfile to use Cloud Storage functions.

source ""

gem "google-cloud-storage"
gem "sinatra"

Application code

The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.

Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.

require "sinatra"
require "google/cloud/storage"

storage =
bucket  = storage.bucket ENV["GOOGLE_CLOUD_STORAGE_BUCKET"]

get "/" do
  # Present the user with an upload form
    <form method="POST" action="/upload" enctype="multipart/form-data">
      <input type="file" name="file">
      <input type="submit" value="Upload">

post "/upload" do
  file_path = params[:file][:tempfile].path
  file_name = params[:file][:filename]

  # Upload file to Google Cloud Storage bucket
  file = bucket.create_file file_path, file_name, acl: "public"

  # The public URL can be used to directly access the uploaded file via HTTP

For more information

For complete information on Cloud Storage, see the Cloud Storage documentation.