-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better explain how to use explicit credentials (#4694)
* Better explain how to use explicit credentials This pull request updates the documentation and adds an example. * Run auto-formatter mvn com.coveo:fmt-maven-plugin:format * Updating Copyright year on UseExplicitCredentials
- Loading branch information
1 parent
bd8d2ce
commit f574484
Showing
4 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...examples/src/main/java/com/google/cloud/examples/nio/snippets/UseExplicitCredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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. | ||
* 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). | ||
} | ||
} |