@@ -146,6 +146,18 @@ 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.
150+ try {
151+ if (storage .get (path .getBucket ()) != null ) {
152+ LOG .trace ("Bucket {} already exists, skipping creation." , path .getBucket ());
153+ return ;
154+ }
155+ } catch (StorageException e ) {
156+ // do not throw error if unable to access bucket for backward compatibility.
157+ LOG .warn ("Getting unexpected error code {}: {} when checking if bucket {} exists. Attempting to create bucket." ,
158+ e .getCode (), e .getMessage (), path .getBucket (), e );
159+ }
160+ // Fallback to bucket creations when get returns null or throws exception.
149161 try {
150162 GCPUtils .createBucket (storage , path .getBucket (), location , cmekKeyName );
151163 LOG .info ("Bucket {} has been created successfully" , path .getBucket ());
@@ -157,7 +169,7 @@ public void createBucketIfNotExists(GCSPath path, @Nullable String location, @Nu
157169 e .getMessage (), path .getUri ());
158170 } else {
159171 String errorReason =
160- String .format ("Unable to create bucket %s. Ensure you entered the correct bucket path and " +
172+ String .format ("Unable to create or get bucket %s. Ensure you entered the correct bucket path and " +
161173 "have permissions for it." , path .getBucket ());
162174 throw GCPErrorDetailsProviderUtil .getHttpResponseExceptionDetailsFromChain (e , errorReason , ErrorType .UNKNOWN ,
163175 true , GCPUtils .GCS_SUPPORTED_DOC_URL );
0 commit comments