Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/io/cdap/plugin/gcp/gcs/StorageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public void mapMetaDataForAllBlobs(String path, Consumer<Map<String, String>> fu
* @param cmekKeyName the name of the cmek key
*/
public void createBucketIfNotExists(GCSPath path, @Nullable String location, @Nullable CryptoKeyName cmekKeyName) {
// Skip bucket creation if bucket already exists.
try {
if (storage.get(path.getBucket()) != null) {
LOG.trace("Bucket {} already exists, skipping creation.", path.getBucket());
return;
}
} catch (StorageException e) {
// do not throw error if unable to access bucket for backward compatibility.
LOG.warn("Getting unexpected error code {}: {} when checking if bucket {} exists. Attempting to create bucket.",
e.getCode(), e.getMessage(), path.getBucket(), e);
}
// Fallback to bucket creations when get returns null or throws exception.
try {
GCPUtils.createBucket(storage, path.getBucket(), location, cmekKeyName);
LOG.info("Bucket {} has been created successfully", path.getBucket());
Expand All @@ -157,7 +169,7 @@ public void createBucketIfNotExists(GCSPath path, @Nullable String location, @Nu
e.getMessage(), path.getUri());
} else {
String errorReason =
String.format("Unable to create bucket %s. Ensure you entered the correct bucket path and " +
String.format("Unable to create or get bucket %s. Ensure you entered the correct bucket path and " +
"have permissions for it.", path.getBucket());
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
Expand Down
Loading