Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move examples from the cloud-storage-docs-xml-api-examples repo, and update them to use Application Default Credentials. #14

Merged
merged 27 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bafd8c3
Move from cloud-storagedocs-xml-api-examples
May 22, 2015
fed4ae8
Update READMEs for new dir structure
May 22, 2015
9ebe7fa
Rename directories to be less redundant.
May 22, 2015
dcf91cc
Update to use App Default Creds.
May 22, 2015
ccf5fc8
Remove package, so eclipse isn't confused.
May 22, 2015
dd10b2c
Add Application Default Credentials quirks.
May 22, 2015
309d399
Update: no longer using service accounts.
May 26, 2015
f606873
Merge remote-tracking branch 'origin/master' into jerjou/xml-api
May 28, 2015
014079d
Add checkstyle plugin
May 28, 2015
c2b6935
More check style configuration
May 28, 2015
1b9f309
Fix style on DeferSampleServlet
May 28, 2015
45920e1
More check style changes
May 28, 2015
0e4514f
Finish fixing Async Query and Defer Sample
May 28, 2015
99a7533
Fix style in BigqueryServiceFactory
May 28, 2015
3f58947
Fix style errors on BigqueryUtils
May 28, 2015
8fc1c6d
Fix style on ExportDataCloudStorageSample
May 28, 2015
e514139
Fix style on LoadDataCsvSample
May 28, 2015
d989529
Remove vestigial key.json. README points to doc
May 28, 2015
78daa80
Fix StreamingSample checkstyle
May 28, 2015
005689f
Fix SyncQuerySample
May 28, 2015
69e24dc
Fail on bad style
May 28, 2015
b34b01c
Fix java style violations.
May 28, 2015
5019fb6
Merge branch 'jerjou/xml-api' of github.com:GoogleCloudPlatform/java-…
May 28, 2015
957da6b
Fix java style violations.
May 28, 2015
a0d74ee
Remove unused 'all' tag.
May 28, 2015
3f26d5b
Style: prettier line break.
May 29, 2015
a33bbb0
AsyncQuerySample extends BigqueryUtils, so can't be final
May 29, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cloud-storage/xml-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
java-docs-samples/cloud-storage XML API Examples
===================================

Samples used in Google Cloud Storage documentation (https://developers.google.com/storage/docs/xml-api-java-samples).

- **cmdline-sample** - Uses a [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) to access a specified bucket.

- **serviceaccount-appengine-sample** - Uses Google App Engine credentials to access a specified bucket. You must add the App Engine Service Account Name to the Permissions of the project that contains the bucket.
70 changes: 70 additions & 0 deletions cloud-storage/xml-api/cmdline-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Using the Command Line Sample
==============================================================

Browse Online
--------------

The main file is [StorageSample.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java).


Setup
-----

* [Create](https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets) a Google Cloud Storage bucket
* This module uses [Application Default Credentials](https://developers.google.com/accounts/docs/application-default-credentials). If you are running it outside of [Google Compute Engine](https://cloud.google.com/compute/), you'll need to
* Download the json private key for a [Service Account](https://cloud.google.com/storage/docs/authentication#service_accounts) and have it available.
* Set an environment variable: `export GOOGLE_APPLICATION_CREDENTIALS=path/to/your-key.json`
* You must also be able to work with [GitHub](https://help.github.com/articles/set-up-git) repositories.
* Clone repository.

git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git


Command-line Instructions
-------------------------

* **Prerequisites:**
* Install the latest version of [Java](http//java.com) and [Maven](http://maven.apache.org/download.html).
* Set the environment variable: `export GOOGLE_APPLICATION_CREDENTIALS=your-key-filename.json`
* You may need to set your `JAVA_HOME`.

```bash
cd java-docs-samples/cloud-storage/xml-api/cmdline-sample
# Compile and run
mvn compile install
mvn -q exec:java -Dexec.args="your-bucket-name"
```

To enable logging of HTTP requests and responses (highly recommended when
developing), please take a look at logging.properties.


Eclipse Instructions
--------------------

* **Prerequisites:**
* Install [Eclipse](http://www.eclipse.org/downloads/), the [Maven plugin](http://eclipse.org/m2e/), and optionally the [GitHub plugin](http://eclipse.github.com/).

* Set up Eclipse Preferences

* Window > Preferences... (or on Mac, Eclipse > Preferences...)
* Select Maven

* check on "Download Artifact Sources"
* check on "Download Artifact JavaDoc"

* Create a new project using `cloud-storage/xml-api/cmdline-sample`

* Create a new Java Project.
* Choose the **Location** of the project to be the location of `cmdline-sample`
* Select the project and **Convert to Maven Project** to add Maven Dependencies.
* Click on Run > Run configurations
* Navigate to your **Java Application**'s configuration section
* In the **Arguments** tab, add the name of the bucket you created above as a **Program argument**
* In the **Environment** tab, create a variable `GOOGLE_APPLICATION_CREDENTIALS` and set it to the path to your json private key file.

* Run

* Right-click on project
* Run As > Java Application
* If asked, type "StorageSample" and click OK
3 changes: 3 additions & 0 deletions cloud-storage/xml-api/cmdline-sample/key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Please download key.json file from the Google APIs Console at https://console.developers.google.com.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to have this file? Can you just add it to the README?


For more information, see https://developers.google.com/storage/docs/xml-api-java-samples.
10 changes: 10 additions & 0 deletions cloud-storage/xml-api/cmdline-sample/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Properties file which configures the operation of the JDK logging facility.
# The system will look for this config file to be specified as a system property:
# -Djava.util.logging.config.file=${project_loc:cmdline-sample}/logging.properties

# Set up the console handler (uncomment "level" to show more fine-grained messages)
handlers = java.util.logging.ConsoleHandler
#java.util.logging.ConsoleHandler.level = CONFIG

# Set up logging of HTTP requests and responses (uncomment "level" to show)
#com.google.api.client.http.level = CONFIG
49 changes: 49 additions & 0 deletions cloud-storage/xml-api/cmdline-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.apis-samples</groupId>
<artifactId>storage-cmd-line-sample</artifactId>
<version>1</version>
<name>Example for the Google Cloud Storage JSON API using OAuth 2.0.</name>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>StorageSample</mainClass>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev35-1.20.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
<properties>
<project.http.version>1.20.0</project.http.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//[START all]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a coding standard and the current consensus is to use the sun's one, according to:
https://github.com/GoogleCloudPlatform/Template/wiki/style.html

What do you think?

/*
* Copyright (c) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Preconditions;
import com.google.common.io.Files;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Collections;
import java.net.URLEncoder;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class StorageSample {

/** Global configuration of Google Cloud Storage OAuth 2.0 scope. */
private static final String STORAGE_SCOPE =
"https://www.googleapis.com/auth/devstorage.read_write";

/** Global instance of the HTTP transport. */
private static HttpTransport httpTransport;

public static void main(String[] args) {
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// Check for valid setup.
Preconditions.checkArgument(args.length == 1,
"Please pass in the Google Cloud Storage bucket name to display");
String bucketName = args[0];

//[START snippet]
// 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");
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
//[END snippet]

// Instantiate transformer input.
Source xmlInput = new StreamSource(new StringReader(content));
StreamResult xmlOutput = new StreamResult(new StringWriter());

// Configure transformer.
Transformer transformer = TransformerFactory.newInstance().newTransformer(); // An identity
// transformer
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);

// Pretty print the output XML.
System.out.println("\nBucket listing for " + bucketName + ":\n");
System.out.println(xmlOutput.getWriter().toString());
System.exit(0);

} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
}
//[END all]
54 changes: 54 additions & 0 deletions cloud-storage/xml-api/serviceaccount-appengine-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Using the Service Account App Engine Sample
==============================================

Browse Online
-------------

The main code file is [StorageSample.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/java/StorageServiceAccountSample.java).

Add Your App Engine Service Account Name to the Project Team
------------------------------------------------------------

See the instructions at https://developers.google.com/storage/docs/xml-api-java-samples
for getting your App Engine Service Account Name and adding it to your project team.

Checkout Instructions
---------------------

**Prerequisites:** install the latest version of [Java](http//java.com) and [Maven](http://maven.apache.org/download.html). You may need to set your `JAVA_HOME`.

You must also be able to work with a GitHub repository (see e.g.,
https://help.github.com/articles/set-up-git).

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

To enable logging of HTTP requests and responses (highly recommended when
developing), please take a look at logging.properties.

Running and Deploying Your Application from the Command Line
------------------------------------------------------------

To run your application locally on a development server:

mvn appengine:devserver

To deploy your application to appspot.com:

If this is the first time you are deploying your application to appspot.com, you will to perform the following steps first.

- Go to https://appengine.google.com and create an application.
- Edit src/main/webapp/WEB-INF/appengine-web.xml, and enter the unique application identifier (you chose it in the prior step) between the <application> tags.

If you've done the above, you can deploy at any time:

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 the app is registered with.

Set Up a Project in Eclipse
---------------------------

...coming soon...
Loading