Java Examples

This page shows you how to send requests to the Google Cloud Storage using the XML API and application default credentials to authenticate.

The first example shows how to create a stand-alone Java program that accesses Google Cloud Storage. The second example shows how to access Cloud Storage from within Google App Engine. You can find the code for both samples on GitHub in GoogleCloudPlatform/java-docs-samples under storage/xml-api.

Prerequisites

This document assumes:

  • You are familiar with Java and the Cloud Storage concepts.
  • You are familiar with git and cloning GitHub repositories.
  • You have Java and Maven installed.
  • You've created a project in the Google Cloud Platform Console, with a bucket you'd like to access.

Command-line example

1. Download the source code

Clone the repository and go to the root of the sample with the following commands:

git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/storage/xml-api/cmdline-sample

2. Authenticate with the Google Cloud SDK

This sample uses application default credentials to infer authentication information from its environment. If you're running it on Google Compute Engine, the environment is already authenticated as a service account, so you can skip this step.

The easiest way to authenticate is to use the Google Cloud SDK. On Linux/Mac OS X, you can install via:

curl https://sdk.cloud.google.com | bash

Once installed, authenticate as yourself:

gcloud auth application-default login

3. (optional) Customize the code

The key part of the code file is shown below along with a button that links to the full source code on GitHub.

StorageSample.java

// Build an account credential.
GoogleCredential credential = GoogleCredential.getApplicationDefault()
    .createScoped(Collections.singleton(STORAGE_SCOPE));

// Set up and execute a Google Cloud Storage request.
String uri = "https://storage.googleapis.com/"
    + URLEncoder.encode(bucketName, "UTF-8");

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
    credential);
GenericUrl url = new GenericUrl(uri);

HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();

4. Run the sample

There are a number of ways you can run this sample, for example, with Eclipse, Gradle, or Maven. Here, we'll use Maven. Maven uses the pom.xml file in the root directory of the sample to build the sample and resolve dependencies.

Run the following commands from the root directory, where example-bucket is the name of a bucket in your project:

mvn compile
mvn exec:java -Dexec.args="example-bucket"

The sample code outputs the contents of a bucket similar to the following output:

Bucket listing for example-bucket:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ListBucketResult SYSTEM "testing.dtd">
<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
  <Name>example-bucket</Name>
  <Prefix/>
  <Marker/>
  <IsTruncated>false</IsTruncated>
  <Contents>
    <Key>object</Key>
    <Generation>1389398350649000</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2014-01-10T23:59:10.569Z</LastModified>
    <ETag>"a5870178133170a8783aaef58ff7c1be"</ETag>
    <Size>77242</Size>
    <Owner>
      <ID>00b4903a972faa8bcce9382576c9129676f1cd6e5def1f5663abbc2ba4625490</ID>
      <DisplayName>user name</DisplayName>
    </Owner>
  </Contents>
  ...
</ListBucketResult>

App Engine example

1. Set up your project

Clone the repository, change directory to the sample root, and package it with the following commands:

git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/storage/xml-api/serviceaccount-appengine-sample
mvn clean package

2. Edit the project permissions

This example uses the OAuth 2.0 credentials capabilities of App Engine to generate credentials. You must add the App Engine Service Account Name to the project so that your App Engine application can access your buckets.

To find your App Engine Service Account Name:

  1. Go to App Engine Console and select your project.
  2. In the left sidebar, select Application Settings.
  3. Copy the email address under Service Account Name. It will have the form "my-application@appspot.gserviceaccount.com"

To add the App Engine Service Account Name to your project:

  1. In the Google Cloud Platform Console, select the project that contains the bucket(s) you want to access.
  2. In the left sidebar, click Permissions.
  3. Click Add Member.
  4. In the Email box, enter the Service Account Name of the App Engine application.
  5. Click Add.

3. Customize the code

Edit the appengine-web.xml file to specify the name of your App Engine application in the <application/> tag.

appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
        <application>bucket-list-java</application>
        <version>1</version>
        <threadsafe>true</threadsafe>
        <!-- Configure java.util.logging -->
        <system-properties>
                <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
        </system-properties>
    <static-files>
        <include path="/xsl/*" />
    </static-files>
</appengine-web-app>

The main class file (StorageSample.java) does not require any changes. The key part of the code file is shown below along with a button that links to the full source code on GitHub.

StorageSample.java

String cleanBucketName = bucketName.replaceAll("/$", "");
String uri = GCS_URI + cleanBucketName;
HttpRequestFactory requestFactory =
    HTTP_TRANSPORT.createRequestFactory(credential);
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();

4. Run the sample

There are a number of ways you can run this sample, for example, with Eclipse, Gradle, or Maven. Here, we'll use Maven. The pom.xml file contains information about the project, including dependencies needed to build and run the code.

Use the following command from the root directory of the sample:

mvn appengine:update

If this is the first time you have run "update" on the project, a browser window will open prompting you to log in. Log in with the same Google account credentials with which the application is registered.

In a browser go to the App Engine instance. For example, if your App Engine application id is "my-application" and the bucket you want to access is named "bucket-name", then go to:

https://my-application.appspot.com/bucket-name

The result will be a table showing the items in the bucket.

Send feedback about...

Cloud Storage Documentation