-
Notifications
You must be signed in to change notification settings - Fork 259
docs(samples): added auth samples and tests #927
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
Changes from 1 commit
3bd604d
bd1bc55
ece1c56
0c26c33
45449b2
7ef36df
b2f367e
01b57ad
b9dee60
3ed6be0
dd39fd4
a46ccc4
5eb6506
cc6a5a5
7c68634
1baeebe
9fc3a7a
b8c5f3f
680cfdd
1944e5d
a943290
d7d6257
cf11754
b910be5
e23fe35
ada91fb
8573bb3
591d0e6
82d7350
54013b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2022 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.gax.paging.Page; | ||
| import com.google.auth.oauth2.ComputeEngineCredentials; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
|
|
||
| public class AuthWithCredentialsFromMetadataServer { | ||
|
|
||
| public static void main(String[] args) throws IOException, GeneralSecurityException { | ||
| // TODO(Developer): | ||
| // 1. Replace the below variable. | ||
| // 2. Make sure you have the necessary permission to list storage buckets "storage.buckets.list" | ||
| String projectId = "your-google-cloud-project-id"; | ||
|
|
||
| authWithCredentialsFromMetadataServer(projectId); | ||
| } | ||
|
|
||
| // Authenticating using Client libraries can be done in one of the following ways: | ||
| // 1. Implicit authentication with ADC (Application Default Credentials) | ||
| // 2. Explicit authentication by specifying the service account | ||
| // 3. Authentication with service account credentials obtained from a metadata server, like, | ||
| // Compute Engine or App Engine etc., | ||
| // 4. Bring your own (BYO) access token | ||
| // 5. Using API keys (for libraries that support) | ||
| // | ||
| // In this snippet, we demonstrate "Authentication with service account credentials | ||
| // obtained from a metadata server". | ||
| public static void authWithCredentialsFromMetadataServer(String project) { | ||
|
|
||
| // This snippet demonstrates how to initialize Cloud Storage and list buckets. | ||
| // Note that the credentials are requested from the ComputeEngine metadata server. | ||
| Storage storage = initService(project); | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| System.out.println("Buckets:"); | ||
| Page<Bucket> buckets = storage.list(); | ||
| for (Bucket bucket : buckets.iterateAll()) { | ||
| System.out.println(bucket.toString()); | ||
| } | ||
| } | ||
|
|
||
| // Initialize the Storage client by getting the Service account credentials | ||
| // from a Metadata server. | ||
| public static Storage initService(String projectId) { | ||
| // Explicitly request the service account credentials from the ComputeEngine metadata server. | ||
| GoogleCredentials credentials = ComputeEngineCredentials.create(); | ||
|
||
|
|
||
| // Alternately, if executing within AppEngine, you can get credentials as follows: | ||
| // GoogleCredentials credentials = AppEngineCredentials.getApplicationDefault(); | ||
|
|
||
| // Construct the Storage client. | ||
| // Note that, here we explicitly specify the service account to use. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return StorageOptions.newBuilder() | ||
| .setCredentials(credentials) | ||
| .setProjectId(projectId) | ||
| .build() | ||
| .getService(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.