Skip to content

Commit 646a7da

Browse files
author
psainics
committed
Improve createBucketIfNotExists to handle StorageException gracefully
- Add try-catch around bucket existence check to handle permission errors - Log warning messages for 403 Forbidden and other unexpected errors - Fallback to bucket creation when existence check fails - Separate bucket check and creation into distinct try-catch blocks
1 parent 22a2c5f commit 646a7da

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/main/java/io/cdap/plugin/gcp/gcs/StorageClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,25 @@ public void mapMetaDataForAllBlobs(String path, Consumer<Map<String, String>> fu
146146
* @param cmekKeyName the name of the cmek key
147147
*/
148148
public void createBucketIfNotExists(GCSPath path, @Nullable String location, @Nullable CryptoKeyName cmekKeyName) {
149+
// Skip bucket creation if bucket already exists.
149150
try {
150151
if (storage.get(path.getBucket()) != null) {
151152
LOG.info("Bucket {} already exists, skipping creation.", path.getBucket());
152153
return;
153154
}
155+
} catch (StorageException e) {
156+
int errorCode = e.getCode();
157+
if (errorCode == 403) {
158+
LOG.warn(
159+
"Getting 403 Forbidden: {} You may not have permission to access bucket {}. Attempting to create bucket.",
160+
e.getMessage(), path.getUri());
161+
} else {
162+
LOG.warn("Getting unexpected error code {}: {} when checking if bucket {} exists. Attempting to create bucket.",
163+
errorCode, e.getMessage(), path.getBucket());
164+
}
165+
}
166+
// Fallback to bucket creations when get returns null or throws exception.
167+
try {
154168
GCPUtils.createBucket(storage, path.getBucket(), location, cmekKeyName);
155169
LOG.info("Bucket {} has been created successfully", path.getBucket());
156170
} catch (StorageException e) {

0 commit comments

Comments
 (0)