Skip to content
Merged
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions storage/json-api/src/main/java/StorageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
/**
* This class manages the details of creating a Storage service, including auth.
*/
// [START authentication_application_default_credentials]
public class StorageFactory {

private static Storage instance = null;

public static synchronized Storage getService() throws IOException, GeneralSecurityException {
Expand All @@ -45,13 +45,18 @@ private static Storage buildService() throws IOException, GeneralSecurityExcepti
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

// Depending on the environment that provides the default credentials (for
// example: Compute Engine, App Engine), the credentials may require us to
// specify the scopes we need explicitly. Check for this case, and inject
// the Cloud Storage scope if required.
if (credential.createScopedRequired()) {
Collection<String> bigqueryScopes = StorageScopes.all();
credential = credential.createScoped(bigqueryScopes);
Collection<String> scopes = StorageScopes.all();
credential = credential.createScoped(scopes);
}

return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("GCS Samples")
.build();
}
}
// [END authentication_application_default_credentials]