From 1876a580f904d095ca6621c1e2f38c3a6e253276 Mon Sep 17 00:00:00 2001 From: JesseLovelace <43148100+JesseLovelace@users.noreply.github.com> Date: Thu, 24 Jun 2021 15:07:45 -0700 Subject: [PATCH] feat: Add from and to storage url options for BlobId (#888) * Add from and to storage url options for BlobId * Better name --- .../java/com/google/cloud/storage/BlobId.java | 23 +++++++++++++++++++ .../com/google/cloud/storage/BlobIdTest.java | 8 +++++++ 2 files changed, 31 insertions(+) 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 6a0953f90..599f45e2a 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 @@ -22,6 +22,7 @@ import com.google.common.base.MoreObjects; import java.io.Serializable; import java.util.Objects; +import java.util.regex.Pattern; /** * Google Storage Object identifier. A {@code BlobId} object includes the name of the containing @@ -56,6 +57,11 @@ public Long getGeneration() { return generation; } + /** Returns this blob's Storage url which can be used with gsutil */ + public String toGsUtilUri() { + return "gs://" + bucket + "/" + name; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) @@ -114,6 +120,23 @@ public static BlobId of(String bucket, String name, Long generation) { return new BlobId(checkNotNull(bucket), checkNotNull(name), generation); } + /** + * Creates a {@code BlobId} object. + * + * @param gsUtilUri the Storage url to create the blob from + */ + public static BlobId fromGsUtilUri(String gsUtilUri) { + if (!Pattern.matches("gs://.*/.*", gsUtilUri)) { + throw new IllegalArgumentException( + gsUtilUri + " is not a valid gsutil URI (i.e. \"gs://bucket/blob\")"); + } + int blobNameStartIndex = gsUtilUri.indexOf('/', 5); + String bucketName = gsUtilUri.substring(5, blobNameStartIndex); + String blobName = gsUtilUri.substring(blobNameStartIndex + 1); + + return BlobId.of(bucketName, blobName); + } + static BlobId fromPb(StorageObject storageObject) { return BlobId.of( storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); 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 d503fb76a..d90760a43 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 @@ -31,6 +31,14 @@ public void testOf() { assertEquals("n", blobId.getName()); } + @Test + public void testToFromGsUtilUri() { + BlobId blobId = BlobId.fromGsUtilUri("gs://bucket/path/to/blob"); + assertEquals("bucket", blobId.getBucket()); + assertEquals("path/to/blob", blobId.getName()); + assertEquals("gs://bucket/path/to/blob", blobId.toGsUtilUri()); + } + @Test public void testEquals() { compareBlobIds(BLOB, BlobId.of("b", "n"));