|
17 | 17 | package io.cdap.plugin.gcp.gcs.source; |
18 | 18 |
|
19 | 19 | import com.google.auth.Credentials; |
| 20 | +import com.google.cloud.storage.Bucket; |
20 | 21 | import com.google.cloud.storage.Storage; |
21 | 22 | import com.google.cloud.storage.StorageException; |
22 | 23 | import com.google.common.base.Strings; |
|
55 | 56 | import java.util.HashMap; |
56 | 57 | import java.util.List; |
57 | 58 | import java.util.Map; |
| 59 | +import java.util.Objects; |
58 | 60 | import java.util.regex.Pattern; |
59 | 61 | import javax.annotation.Nullable; |
60 | 62 |
|
@@ -119,16 +121,24 @@ public void prepareRun(BatchSourceContext context) throws Exception { |
119 | 121 | } |
120 | 122 |
|
121 | 123 | Storage storage = GCPUtils.getStorage(config.connection.getProject(), credentials); |
| 124 | + Bucket bucket = storage.get(bucketName); |
| 125 | + if (Objects.isNull(bucket)) { |
| 126 | + String errorReason = String.format("Unable to access GCS bucket '%s'.", |
| 127 | + bucketName); |
| 128 | + collector.addFailure(String.format("%s Ensure you entered the correct bucket path.", errorReason), |
| 129 | + null); |
| 130 | + collector.getOrThrowException(); |
| 131 | + } |
122 | 132 | String location = null; |
123 | 133 | try { |
124 | 134 | // Get location of the source for lineage |
125 | | - location = storage.get(bucketName).getLocation(); |
| 135 | + location = bucket.getLocation(); |
126 | 136 | } catch (StorageException e) { |
127 | 137 | String errorReason = String.format("Error code: %s, Unable to access GCS bucket '%s'. ", |
128 | | - e.getCode(), bucketName); |
| 138 | + e.getCode(), bucketName); |
129 | 139 | collector.addFailure(String.format("%s %s", errorReason, e.getMessage()), |
130 | | - "Ensure you entered the correct bucket path and have permissions for it.") |
131 | | - .withStacktrace(e.getStackTrace()); |
| 140 | + "Ensure you entered the correct bucket path and have permissions for it.") |
| 141 | + .withStacktrace(e.getStackTrace()); |
132 | 142 | collector.getOrThrowException(); |
133 | 143 | } |
134 | 144 |
|
|
0 commit comments