From 37c728e4406331710d576b542b9971593c7cfc87 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Wed, 26 Apr 2023 11:19:02 -0400 Subject: [PATCH] fix: introduce new BlobId#toGsUtilUriWithGeneration When BlobId#toGsUtilUri was updated to output generation if defined, this broken older clients ability to parse the value via BlobId#fromGsUtilUri. This adds the new method toGsUtilUriWithGeneration to explicitly opt in to generation being okay in the output. Related to #1928 Related to #1929 --- .../main/java/com/google/cloud/storage/BlobId.java | 13 ++++++++++++- .../java/com/google/cloud/storage/BlobIdTest.java | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobId.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobId.java index c1914d6a13..a3c6fa6fce 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobId.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobId.java @@ -58,8 +58,19 @@ public Long getGeneration() { return generation; } - /** Returns this blob's Storage url which can be used with gsutil */ + /** + * Returns this blob's Storage url which can be used with gsutil. If {@link #generation} is + * non-null it will not be included in the uri. + */ public String toGsUtilUri() { + return "gs://" + bucket + "/" + name; + } + + /** + * Returns this blob's Storage url which can be used with gsutil. If {@link #generation} is + * non-null it will be included in the uri + */ + public String toGsUtilUriWithGeneration() { return "gs://" + bucket + "/" + name + (generation == null ? "" : ("#" + generation)); } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobIdTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobIdTest.java index 03bf2218f4..f1c37ce1e6 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobIdTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobIdTest.java @@ -45,7 +45,8 @@ public void testToFromGsUtilUriWithGeneration() { assertEquals("bucket", blobId.getBucket()); assertEquals("path/to/blob", blobId.getName()); assertEquals(Long.valueOf(1360887697105000L), blobId.getGeneration()); - assertEquals("gs://bucket/path/to/blob#1360887697105000", blobId.toGsUtilUri()); + assertEquals("gs://bucket/path/to/blob", blobId.toGsUtilUri()); + assertEquals("gs://bucket/path/to/blob#1360887697105000", blobId.toGsUtilUriWithGeneration()); } @Test