This page discusses how you can use the Google APIs Client Library for Go to send requests to the Google Cloud Storage JSON API. Specifically, the GitHub sample storage-getting-started-go is discussed.
This document assumes that you have Go installed, and you are familiar with Java and the Google Cloud Storage concepts and operations presented in the Getting Started: Using the Developers Console guide.
1. Set up your environment
To run this example, you need to have Go and Mercurial installed. Mercurial is used to pull in the repositories you need to run this sample:
$ go get code.google.com/p/goauth2/oauth
$ go get code.google.com/p/google-api-go-client/storage/v1
2. Create a client ID and secret
In the example code, you need to provide a client ID and secret to authenticate with the Google Cloud Storage JSON API. You can get these from the Google Developers Console.
To generate a Client ID and secret:
- Go to the Google Developers Console .
- Select a project to which the client ID will be associated.
- In the left sidebar, under APIs & AUTH , select Credentials .
- Click Create new Client ID .
- In the Create Client ID window, choose Installed application .
-
Click
Create Client ID
.
The resulting client ID will look like the following example:
- Click Download JSON .
3. Create your code file
In this step, you will create the Go code file,
storage-sample.go
. Copy
the code from below and make the following changes:
-
Set
bucketName
to the name of a bucket in the project where you created the client ID. -
Set
projectID
to the project name or ID. -
Set
clientId
to the client ID you created. -
Set
clientSecret
to the client secret associated with the client ID.
storage-sample.go
4. Run the sample
When you run the sample for the first time, you will be asked to grant access to your application by visiting a URL and getting a code (OAuth token), which you then pass as an argument when running the program.
To run the sample for the first time:
-
Generate an authentication URL.
$ go run storage-sample.go
-
Open the authentication URL and click Accept .
-
Copy the code and use it in a new run command:
$ go run storage-sample.go -code=YOUR_CODE
For subsequent runs, you do not need to provide the code.