From 7e883c1776ceb939b39618a91483beb9a5125ac8 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Martin Date: Mon, 18 Mar 2019 12:47:00 -0700 Subject: [PATCH 1/3] Better explain how to use explicit credentials This pull request updates the documentation and adds an example. --- .../google-cloud-nio-examples/README.md | 3 +- .../google-cloud-nio/README.md | 3 +- .../nio/CloudStorageFileSystemProvider.java | 7 ++- .../nio/snippets/UseExplicitCredentials.java | 48 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio-examples/README.md b/google-cloud-clients/google-cloud-contrib/google-cloud-nio-examples/README.md index e84f6aba4ade..1e13155e46af 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-nio-examples/README.md +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio-examples/README.md @@ -6,7 +6,8 @@ application that uses Java NIO without the need to recompile. Note that whenever possible, you instead want to recompile the app and use the normal dependency mechanism to add a dependency to google-cloud-nio. You can see examples of -this in the [google-cloud-examples](../../../google-cloud-examples) project. +this in the [google-cloud-examples](../../../google-cloud-examples) project, +[under nio](../../../google-cloud-examples/src/main/java/com/google/cloud/examples/nio). To run this example: diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/README.md b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/README.md index 3e0a3616c3e3..12c1cdebec02 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/README.md +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/README.md @@ -57,7 +57,8 @@ Authentication -------------- See the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) -section in the base directory's README. +section in the base directory's README. This shows how to construct the `StorageOptions` object, +which you can then pass to `CloudStorageFileSystem.forBucket`. About Google Cloud Storage -------------------------- diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java index 4f22c9aeb89e..d505a25c3631 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java @@ -140,7 +140,12 @@ protected Path computeNext() { } } - /** Sets options that are only used by the constructor. */ + /** + * Sets options that are only used by the constructor. + * + * Instead of calling this, when possible use CloudStorageFileSystem.forBucket + * and pass StorageOptions as an argument. + * */ @VisibleForTesting public static void setStorageOptions(@Nullable StorageOptions newStorageOptions) { futureStorageOptions = newStorageOptions; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java new file mode 100644 index 000000000000..c11288d5078c --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Google LLC + * + * 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. + */ + +package com.google.cloud.examples.nio.snippets; + +import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.contrib.nio.CloudStorageConfiguration; +import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; + +import java.io.FileInputStream; +import java.io.IOException; + +/** + * A snippet for Google Cloud Storage NIO that shows how to create a {@link CloudStorageFileSystem} + * using explicitly-provided credentials instead of the default ones. + */ +public class UseExplicitCredentials { + + public static void main(String... args) throws IOException { + // Create a file system for the bucket using the service account credentials + // saved in the file below. + String myCredentials = "/path/to/my/key.json"; + CloudStorageFileSystem fs = + CloudStorageFileSystem.forBucket( + "mybucket", + CloudStorageConfiguration.DEFAULT, + StorageOptions.newBuilder() + .setCredentials(ServiceAccountCredentials.fromStream( + new FileInputStream(myCredentials))) + .build()); + // Can now read and write to the bucket using fs + // (see e.g. ReadAllLines for an example). + } +} From 4c9b5141c458fe779a14405f49f69f7a349a8033 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Martin Date: Mon, 18 Mar 2019 15:33:27 -0700 Subject: [PATCH 2/3] Run auto-formatter mvn com.coveo:fmt-maven-plugin:format --- .../storage/contrib/nio/CloudStorageFileSystemProvider.java | 6 +++--- .../cloud/examples/nio/snippets/UseExplicitCredentials.java | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java index d505a25c3631..ba0e854644fe 100644 --- a/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java +++ b/google-cloud-clients/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java @@ -143,9 +143,9 @@ protected Path computeNext() { /** * Sets options that are only used by the constructor. * - * Instead of calling this, when possible use CloudStorageFileSystem.forBucket - * and pass StorageOptions as an argument. - * */ + *

Instead of calling this, when possible use CloudStorageFileSystem.forBucket and pass + * StorageOptions as an argument. + */ @VisibleForTesting public static void setStorageOptions(@Nullable StorageOptions newStorageOptions) { futureStorageOptions = newStorageOptions; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java index c11288d5078c..fff881399598 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java @@ -20,7 +20,6 @@ import com.google.cloud.storage.StorageOptions; import com.google.cloud.storage.contrib.nio.CloudStorageConfiguration; import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; - import java.io.FileInputStream; import java.io.IOException; @@ -39,8 +38,8 @@ public static void main(String... args) throws IOException { "mybucket", CloudStorageConfiguration.DEFAULT, StorageOptions.newBuilder() - .setCredentials(ServiceAccountCredentials.fromStream( - new FileInputStream(myCredentials))) + .setCredentials( + ServiceAccountCredentials.fromStream(new FileInputStream(myCredentials))) .build()); // Can now read and write to the bucket using fs // (see e.g. ReadAllLines for an example). From 959522e68fc5001678f745458d223dd7b86bbf29 Mon Sep 17 00:00:00 2001 From: Solomon Duskis Date: Tue, 19 Mar 2019 12:19:12 -0400 Subject: [PATCH 3/3] Updating Copyright year on UseExplicitCredentials --- .../cloud/examples/nio/snippets/UseExplicitCredentials.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java index fff881399598..62d08a5d1a19 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.