Ruby & GCP: Uploading Pictures To Cloud Storage
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.
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.
You’ll also need the google-cloud-storage gem.
gem install google-cloud-storage
Enable the Storage APIs in the API Manager. You want Google Cloud Storage and Google Cloud Storage JSON API to be enabled.
Finally, you’ll need a service account.
- From console.cloud.google.com click “API Manager” in the left-hand menu.
- Once the API Manager page loads click “Credentials”. It may just appear as a key icon.
- Click “Create credentials” and pick “Service account key”.
- You can use an existing service account or create a new one. It should have a Storage Object Admin role. You may need to scroll down in the roles drop down to see the Storage category.
- The key should download automatically. Put it in a secure location. I put it in the root of my project and rename it service-account.json. I also make sure that this file is in
The code to upload a file is simple.
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 and bucket naming best practices.
In the example, the bucket is called
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
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.