1919import com .google .common .collect .ImmutableMap ;
2020import com .google .gcloud .AuthCredentials ;
2121import com .google .gcloud .storage .BlobInfo ;
22+ import com .google .gcloud .RetryParams ;
2223import com .google .gcloud .storage .Storage ;
2324import com .google .gcloud .storage .StorageException ;
2425import com .google .gcloud .storage .StorageOptions ;
@@ -96,50 +97,37 @@ public static String generateBucketName() {
9697 }
9798
9899 /**
99- * Creates a {@code RemoteGcsHelper} object.
100+ * Creates a {@code RemoteGcsHelper} object for the given project id and JSON key path .
100101 *
102+ * @param projectId id of the project to be used for running the tests
103+ * @param keyPath path to the JSON key to be used for running the tests
101104 * @param options creation options
102105 * @return A {@code RemoteGcsHelper} object for the provided options.
103- * @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
104- * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
105- * pointed by {@code GCLOUD_TESTS_KEY} does not exist
106+ * @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if the file pointed by
107+ * {@code keyPath} does not exist
106108 */
107- public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
109+ public static RemoteGcsHelper create (String projectId , String keyPath , Option ... options )
110+ throws GcsHelperException {
108111 boolean keyFromClassPath = false ;
109112 Map <Class <? extends Option >, Option > optionsMap = Option .asImmutableMap (options );
110113 if (optionsMap .containsKey (KeyFromClasspath .class )) {
111114 keyFromClassPath =
112115 ((KeyFromClasspath ) optionsMap .get (KeyFromClasspath .class )).keyFromClasspath ();
113116 }
114- String projectId = System .getenv (PROJECT_ID_ENV_VAR );
115- String stringKeyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
116- if (projectId == null ) {
117- String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
118- if (log .isLoggable (Level .WARNING )) {
119- log .log (Level .WARNING , message );
120- }
121- throw new GcsHelperException (message );
122- }
123- if (stringKeyPath == null ) {
124- String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
125- if (log .isLoggable (Level .WARNING )) {
126- log .log (Level .WARNING , message );
127- }
128- throw new GcsHelperException (message );
129- }
130117 try {
131118 InputStream keyFileStream ;
132119 if (keyFromClassPath ) {
133- keyFileStream = RemoteGcsHelper .class .getResourceAsStream (stringKeyPath );
120+ keyFileStream = RemoteGcsHelper .class .getResourceAsStream (keyPath );
134121 if (keyFileStream == null ) {
135- throw new FileNotFoundException (stringKeyPath + " not found in classpath" );
122+ throw new FileNotFoundException (keyPath + " not found in classpath" );
136123 }
137124 } else {
138- keyFileStream = new FileInputStream (stringKeyPath );
125+ keyFileStream = new FileInputStream (keyPath );
139126 }
140127 StorageOptions storageOptions = StorageOptions .builder ()
141128 .authCredentials (AuthCredentials .createForJson (keyFileStream ))
142129 .projectId (projectId )
130+ .retryParams (RetryParams .getDefaultInstance ())
143131 .build ();
144132 return new RemoteGcsHelper (storageOptions );
145133 } catch (FileNotFoundException ex ) {
@@ -155,6 +143,36 @@ public static RemoteGcsHelper create(Option... options) throws GcsHelperExceptio
155143 }
156144 }
157145
146+ /**
147+ * Creates a {@code RemoteGcsHelper} object. Project id and path to JSON key are read from two
148+ * environment variables: {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY}.
149+ *
150+ * @param options creation options
151+ * @return A {@code RemoteGcsHelper} object for the provided options.
152+ * @throws com.google.gcloud.storage.testing.RemoteGcsHelper.GcsHelperException if environment variables
153+ * {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
154+ * pointed by {@code GCLOUD_TESTS_KEY} does not exist
155+ */
156+ public static RemoteGcsHelper create (Option ... options ) throws GcsHelperException {
157+ String projectId = System .getenv (PROJECT_ID_ENV_VAR );
158+ String keyPath = System .getenv (PRIVATE_KEY_ENV_VAR );
159+ if (projectId == null ) {
160+ String message = "Environment variable " + PROJECT_ID_ENV_VAR + " not set" ;
161+ if (log .isLoggable (Level .WARNING )) {
162+ log .log (Level .WARNING , message );
163+ }
164+ throw new GcsHelperException (message );
165+ }
166+ if (keyPath == null ) {
167+ String message = "Environment variable " + PRIVATE_KEY_ENV_VAR + " not set" ;
168+ if (log .isLoggable (Level .WARNING )) {
169+ log .log (Level .WARNING , message );
170+ }
171+ throw new GcsHelperException (message );
172+ }
173+ return create (projectId , keyPath , options );
174+ }
175+
158176 private static class DeleteBucketTask implements Callable <Boolean > {
159177
160178 private Storage storage ;
0 commit comments