Using Cloud Storage with Ruby

This page shows you how to perform basic tasks in Cloud Storage using the Google Cloud Client Library for Ruby, and it shows you the underlying Ruby code used to perform the tasks.

Costs that you incur in Cloud Storage are based on the resources you use. This quickstart typically uses less than $0.01 USD worth of Cloud Storage resources.

Before you begin

  1. Have Ruby version 2.3 or later and the Bundler Ruby gem.

  2. From a terminal window, create and navigate to a directory for your working environment:

    mkdir gcs-programmatic-quickstart
    cd gcs-programmatic-quickstart

  3. Clone the GitHub repository that contains examples of using the Cloud Storage client libraries for Ruby and comes with a CLI to execute the examples:

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git

  4. Prepare your Ruby environment by installing the Ruby storage client library:

    cd ruby-docs-samples/storage
    bundle install

    bundle install installs the google-cloud-storage gem defined in the example Gemfile. This gem is used by the commands in this tutorial.

  5. Sign in to your Google Account.

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

  6. 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.

  7. Set your project as a local environment variable:

    Linux or MacOS

    export GOOGLE_CLOUD_PROJECT=[PROJECT-ID]

    Windows

    With PowerShell:

    $env:GOOGLE_CLOUD_PROJECT=[PROJECT-ID]

    With command prompt:

    set GOOGLE_CLOUD_PROJECT=[PROJECT-ID]

Create a bucket

Buckets are the basic containers that hold your data in Cloud Storage.

To create a bucket:

  1. Use the following command and a unique name to create a bucket:

    bundle exec ruby buckets.rb create my-awesome-bucket

    This command uses a bucket named my-awesome-bucket. You must choose your own, globally-unique, bucket name.

    If successful, the command returns:

    Created bucket: my-awesome-bucket

    You've just created a bucket where you can store your stuff!

Upload an object into your bucket

Image of a kitten.

  1. Right-click on the image above and save it somewhere on your computer, such as on the desktop.

  2. Use the following command to copy the image from the location where you saved it to the bucket you created:

    bundle exec ruby files.rb upload my-awesome-bucket ~/Desktop/kitten.png kitten.png

    If successful, the command returns:

    Uploaded kitten.png

    You've just stored an object in your bucket.

Download an object from your bucket

  1. Use the following command to download the image you stored in your bucket to somewhere on your computer, such as the desktop:

    bundle exec ruby files.rb download my-awesome-bucket kitten.png ~/Desktop/kitten2.png

    If successful, the command returns:

    Downloaded kitten.png

    You've just downloaded something from your bucket.

Copy an object in the bucket

  1. Use the following command to create a copy of your image in the bucket:

    bundle exec ruby files.rb copy my-awesome-bucket kitten.png my-awesome-bucket kitten3.png

    If successful, the command returns:

    kitten.png in my-awesome-bucket copied to kitten3.png in my-awesome-bucket

    You've just copied your object in your bucket.

List contents of a bucket

  1. Use the following command to list the contents of the bucket:

    bundle exec ruby files.rb list my-awesome-bucket

    If successful, the command returns:

    kitten.png
    kitten3.png
    

    You've just seen the contents of your bucket.

Give someone access to your bucket

  1. Use the following command to give a specific email address permission to also control the objects in your bucket:

    bundle exec ruby iam.rb add_bucket_iam_member my-awesome-bucket roles/storage.objectAdmin user:jane@gmail.com

    If successful, the command returns:

    Added user:jane@gmail.com with role roles/storage.objectAdmin to my-awesome-bucket

    Now someone else can put things into and view what's in your bucket.

  2. To remove this permission, use the command:

    bundle exec ruby iam.rb remove_bucket_iam_member my-awesome-bucket roles/storage.objectAdmin user:jane@gmail.com

    If successful, the command returns:

    Removed user:jane@gmail.com with role roles/storage.objectAdmin from my-awesome-bucket

    You have removed the user's access to this bucket.

Delete objects

  1. Use the following command to delete an object:

    bundle exec ruby files.rb delete my-awesome-bucket kitten.png

    If successful, the command returns:

    Deleted kitten.png

    This copy of the object is no longer stored in Cloud Storage (though the copy you named kitten3.png still exists).

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

  1. Use the following command to delete kitten3.png from your bucket:

    bundle exec ruby files.rb delete my-awesome-bucket kitten3.png
  2. Use the following command to delete the bucket:

    bundle exec ruby buckets.rb delete my-awesome-bucket

    If successful, the command returns a message similar to:

    Deleted bucket: my-awesome-bucket

    Your bucket and its contents are deleted.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Storage
Need help? Visit our support page.