1818
1919import com .google .common .collect .ImmutableMap ;
2020import com .google .gcloud .AuthCredentials ;
21- import com .google .gcloud .RetryParams ;
2221import com .google .gcloud .storage .RemoteGcsHelper .Option .KeyFromClasspath ;
2322
2423import java .io .FileInputStream ;
@@ -93,37 +92,50 @@ public static String generateBucketName() {
9392 }
9493
9594 /**
96- * Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path .
95+ * Creates a {@code RemoteGcsHelper} object.
9796 *
98- * @param projectId id of the project to be used for running the tests
99- * @param keyPath path to the JSON key to be used for running the tests
10097 * @param options creation options
10198 * @return A {@code RemoteGcsHelper} object for the provided options.
102- * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if the file pointed by
103- * {@code keyPath} does not exist
99+ * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
100+ * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
101+ * pointed by {@code GCLOUD_TESTS_KEY} does not exist
104102 */
105- public static RemoteGcsHelper create (String projectId , String keyPath , Option ... options )
106- throws GcsHelperException {
103+ public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
107104 boolean keyFromClassPath = false ;
108105 Map <Class <? extends Option >, Option > optionsMap = Option .asImmutableMap (options );
109106 if (optionsMap .containsKey (KeyFromClasspath .class )) {
110107 keyFromClassPath =
111108 ((KeyFromClasspath ) optionsMap .get (KeyFromClasspath .class )).keyFromClasspath ();
112109 }
110+ String projectId = System .getenv (PROJECT_ID_ENV_VAR );
111+ String stringKeyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
112+ if (projectId == null ) {
113+ String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
114+ if (log .isLoggable (Level .WARNING )) {
115+ log .log (Level .WARNING , message );
116+ }
117+ throw new GcsHelperException (message );
118+ }
119+ if (stringKeyPath == null ) {
120+ String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
121+ if (log .isLoggable (Level .WARNING )) {
122+ log .log (Level .WARNING , message );
123+ }
124+ throw new GcsHelperException (message );
125+ }
113126 try {
114127 InputStream keyFileStream ;
115128 if (keyFromClassPath ) {
116- keyFileStream = RemoteGcsHelper .class .getResourceAsStream (keyPath );
129+ keyFileStream = RemoteGcsHelper .class .getResourceAsStream (stringKeyPath );
117130 if (keyFileStream == null ) {
118- throw new FileNotFoundException (keyPath + " not found in classpath" );
131+ throw new FileNotFoundException (stringKeyPath + " not found in classpath" );
119132 }
120133 } else {
121- keyFileStream = new FileInputStream (keyPath );
134+ keyFileStream = new FileInputStream (stringKeyPath );
122135 }
123136 StorageOptions storageOptions = StorageOptions .builder ()
124137 .authCredentials (AuthCredentials .createForJson (keyFileStream ))
125138 .projectId (projectId )
126- .retryParams (RetryParams .getDefaultInstance ())
127139 .build ();
128140 return new RemoteGcsHelper (storageOptions );
129141 } catch (FileNotFoundException ex ) {
@@ -139,36 +151,6 @@ public static RemoteGcsHelper create(String projectId, String keyPath, Option...
139151 }
140152 }
141153
142- /**
143- * Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
144- * environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
145- *
146- * @param options creation options
147- * @return A {@code RemoteGcsHelper} object for the provided options.
148- * @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
149- * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
150- * pointed by {@code GCLOUD_TESTS_KEY} does not exist
151- */
152- public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
153- String projectId = System .getenv (PROJECT_ID_ENV_VAR );
154- String keyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
155- if (projectId == null ) {
156- String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
157- if (log .isLoggable (Level .WARNING )) {
158- log .log (Level .WARNING , message );
159- }
160- throw new GcsHelperException (message );
161- }
162- if (keyPath == null ) {
163- String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
164- if (log .isLoggable (Level .WARNING )) {
165- log .log (Level .WARNING , message );
166- }
167- throw new GcsHelperException (message );
168- }
169- return create (projectId , keyPath , options );
170- }
171-
172154 private static class DeleteBucketTask implements Callable <Boolean > {
173155
174156 private Storage storage ;
0 commit comments