29
29
/**
30
30
* This class manages the details of creating a Storage service, including auth.
31
31
*/
32
+ // [START authentication_application_default_credentials]
32
33
public class StorageFactory {
33
-
34
34
private static Storage instance = null ;
35
35
36
36
public static synchronized Storage getService () throws IOException , GeneralSecurityException {
@@ -45,13 +45,18 @@ private static Storage buildService() throws IOException, GeneralSecurityExcepti
45
45
JsonFactory jsonFactory = new JacksonFactory ();
46
46
GoogleCredential credential = GoogleCredential .getApplicationDefault (transport , jsonFactory );
47
47
48
+ // Depending on the environment that provides the default credentials (for
49
+ // example: Compute Engine, App Engine), the credentials may require us to
50
+ // specify the scopes we need explicitly. Check for this case, and inject
51
+ // the Cloud Storage scope if required.
48
52
if (credential .createScopedRequired ()) {
49
- Collection <String > bigqueryScopes = StorageScopes .all ();
50
- credential = credential .createScoped (bigqueryScopes );
53
+ Collection <String > scopes = StorageScopes .all ();
54
+ credential = credential .createScoped (scopes );
51
55
}
52
56
53
57
return new Storage .Builder (transport , jsonFactory , credential )
54
58
.setApplicationName ("GCS Samples" )
55
59
.build ();
56
60
}
57
61
}
62
+ // [END authentication_application_default_credentials]
0 commit comments