Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Java Example

This page discusses how you can use the Google APIs Client Library for Java to send requests to the Google Cloud Storage JSON API.

Specifically, the sample shown here gets metadata about a bucket you specify and lists the objects in the bucket. You can use the general steps shown here to run your own code.

This document assumes that you have Java 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

Set up a directory structure for the project that looks like the following:

.
└── src
│   └── main
│       ├── java
│       └── resources

The name of the containing folder ( . ) doesn't matter. The java directory will contain the Java code, and the resources directory will contain client secret information used for authentication.

If you clone the repository that contains the Java sample, the directory structure is created for you:

git clone https://github.com/GoogleCloudPlatform/cloud-storage-docs-json-api-examples.git

2. Create a client secrets file

In the example code, you will use a client secrets file to authenticate with the Google Cloud Storage JSON API. You can create and download a client secrets (JSON) file from the Google Developers Console. Rename the file client_secrets.json , and put in the ./src/main/resources folder.

To generate a client secrets file:

  1. Go to the Google Developers Console .
  2. Select a project to which the client ID will be associated.
  3. In the left sidebar, under APIs & AUTH , select Credentials .
  4. Click Create new Client ID .
  5. In the Create Client ID window, choose Installed application .
  6. Click Create Client ID .

    The resulting client ID will look like the following example:

    Public/Private Key Pair
  7. Click Download JSON .

3. Create your code file

In this step, you will create the Java code file called StorageSample.java , and put it in the ./src/main/java directory. Copy the code from below and make the following changes:

  • Set APPLICATION_NAME to that identifies your application.
  • Set BUCKET_NAME to the name of a bucket in the same project as client ID you created.

StorageSample.java

4. Run the sample

Your directory structure at this point should look like this:

.
└── src
│   └── main
│       ├── java
│       │   └── StorageSample.java
│       └── resources
│           └── client_secrets.json

There are a number of ways you can run your sample, for example, with Eclipse , Gradle , or Maven . Here, we'll use a simple Maven setup to run the sample.

To run the sample using Maven:

  1. Create a pom.xml file in the root directory and with the contents. If you cloned the respository with git then this file already exists.

  2. Run the following commands from the root directory.

    $ mvn compile install
    $ mvn exec:java
    

Output from the sample code will be similar to this:

name: example-bucket
location: US

Contents:
timeCreated: 2013-09-03T18:10:46.889Z
owner: {"entity":"group-00b4903a97e991ba23f3f296d7f3e048648a0185d9abfa1e5ce8b0151cdb3353","entityId":"00b4903a97e991ba23f3f296d7f3e048648a0185d9abfa1e5ce8b0151cdb3353"}
object1 (77242 bytes)
object2 (76991 bytes)
...

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.