# Ruby & GCP: Uploading Pictures To Cloud Storage

> Last updated: 2016-09-29

Uploading content to a storage provider is one of the most basic tasks I do with a cloud provider. At least it should be. This is a quick tutorial on uploading files to Google Cloud Storage with Ruby. Code for this tutorial is [here](https://github.com/thagomizer/examples/tree/master/CloudMinute/Ruby).

### Setup

1. To run this tutorial, you’ll need a Google Cloud Platform account and a project. You can create an account and project at [http://cloud.google.com/free-trial](http://cloud.google.com/free-trial).

2. You’ll also need the google-cloud-storage gem. `gem install google-cloud-storage`

3. Enable the Storage APIs in the [API Manager](https://console.cloud.google.com/apis). You want Google Cloud Storage and Google Cloud Storage JSON API to be enabled.

4. Finally, you’ll need a service account.

### The Code

The code to upload a file is simple.

```
require "google/cloud/storage"

gcloud = Google::Cloud.new "Your Project", "Path To Your Key"

storage = gcloud.storage

bucket = storage.create_bucket "my_goat_pictures"

bucket.create_file "goat.jpg", "uploaded_goat.jpg"
```

First, require the library and create a new gcloud object using your project id and the service account key. You can get the project id from cloud console. It may not be the same as your project name.

Then use the gcloud object to create a storage object.

After that, create a cloud storage bucket. Cloud storage bucket names must be unique within all of Google Cloud. Bucket names are also potentially globally visible so they should not contain proprietary information. For more information see [bucket naming requirements](https://cloud.google.com/storage/docs/naming#requirements) and [bucket naming best practices](https://cloud.google.com/storage/docs/best-practices).

In the example, the bucket is called `my_goat_pictures`.

Once you create the bucket, you can upload a file using the `create_file` method. In the example, I uploaded a picture called `goat.jpg` and it was saved as `uploaded_goat.jpg` in the cloud storage bucket.

You can verify that the file was uploaded correctly by using the storage browser in the web console. [https://console.cloud.google.com/storage/browser](https://console.cloud.google.com/storage/browse)

### Advanced Options

When you create a bucket, you can specify an access control list (ACL), the location of the bucket, and what class of storage to use. You can also turn on object versioning for the entire bucket. If you are using the bucket to serve a static website (perhaps one generated with Jekyll) you can set up the index/main page for the site and the 404 page when creating the bucket.

Likewise, there are many options that can be specified when creating a file. Cache-control information, content metadata (encoding, language, disposition), custom metadata, and encryption keys can be specified. You can also include a checksum and the file will only be created if the checksum matches the value calculated by Cloud Storage.

For more information about the Cloud Storage API check out the [documentation](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-storage/master/google/cloud/storage).

Please enable JavaScript to view the [comments powered by Disqus.](https://disqus.com/?ref_noscript)

 ![Aja Hammerly](/img/traditionalheadshot.jpg)

Aja Hammerly

Seattle, WA

### library\_books Series Index 

- [Developer Relations](/series#developer-relations)4
- [Machine Learning Basics](/series#machine-learning-basics)3
- [Battleship](/series#battleship)5
- [Talks I Love](/series#talks-i-love)6
- [Algorithms with Auntie Aja](/series#algorithms-with-auntie-aja)3
- [Ruby and GCP](/series#ruby-and-gcp)5
- [Playing with Data](/series#playing-with-data)3
- [Kubernetes & Rails](/series#kubernetes-rails)3
- [Learn To Code Resources](/series#learn-to-code-resources)3
- [Testing Anti-Patterns](/series#testing-anti-patterns)5
