Skip to content

Commit 1a33ad9

Browse files
jscudcopybara-github
authored andcommitted
fix: auto-created GCS staging bucket names are less predictable
PiperOrigin-RevId: 892532325
1 parent f7733ec commit 1a33ad9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

google/cloud/aiplatform/utils/gcs_utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import datetime
1919
import glob
20+
import uuid
2021

2122
# Version detection and compatibility layer for google-cloud-storage v2/v3
2223
from importlib.metadata import version as get_version
@@ -59,6 +60,8 @@
5960
stacklevel=2,
6061
)
6162

63+
_DEFAULT_STAGING_BUCKET_SALT = str(uuid.uuid4())
64+
6265

6366
def blob_from_uri(uri: str, client: storage.Client) -> storage.Blob:
6467
"""Create a Blob from a GCS URI, compatible with v2 and v3.
@@ -201,9 +204,14 @@ def stage_local_data_in_gcs(
201204
# Currently we only do this when staging_gcs_dir is not specified.
202205
# The buckets that we create are regional.
203206
# This prevents errors when some service required regional bucket.
204-
# E.g. "FailedPrecondition: 400 The Cloud Storage bucket of `gs://...` is in location `us`. It must be in the same regional location as the service location `us-central1`."
205-
# We are making the bucket name region-specific since the bucket is regional.
206-
staging_bucket_name = project + "-vertex-staging-" + location
207+
# E.g. "FailedPrecondition: 400 The Cloud Storage bucket of `gs://...`
208+
# is in location `us`. It must be in the same regional location as the
209+
# service location `us-central1`."
210+
# We are making the bucket name region-specific since the bucket is
211+
# regional.
212+
staging_bucket_name = (
213+
project + "-vertex-staging-" + location + "-" + _DEFAULT_STAGING_BUCKET_SALT
214+
)[:63]
207215
client = storage.Client(project=project, credentials=credentials)
208216
staging_bucket = storage.Bucket(client=client, name=staging_bucket_name)
209217
if not staging_bucket.exists():

0 commit comments

Comments
 (0)