Skip to content

Commit 0a033b3

Browse files
authored
feat: handle generation numbers in BlobId#{to,from}GsUtilUri (#1929)
1 parent dce670b commit 0a033b3

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/BlobId.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.base.MoreObjects;
2222
import java.io.Serializable;
2323
import java.util.Objects;
24+
import java.util.regex.Matcher;
2425
import java.util.regex.Pattern;
2526

2627
/**
@@ -31,6 +32,7 @@
3132
public final class BlobId implements Serializable {
3233

3334
private static final long serialVersionUID = 8201580858265557469L;
35+
private static final Pattern gsUtilUriPattern = Pattern.compile("^gs://(.+?)/(.+?)(?:#(\\d+))?$");
3436
private final String bucket;
3537
private final String name;
3638
private final Long generation;
@@ -58,7 +60,7 @@ public Long getGeneration() {
5860

5961
/** Returns this blob's Storage url which can be used with gsutil */
6062
public String toGsUtilUri() {
61-
return "gs://" + bucket + "/" + name;
63+
return "gs://" + bucket + "/" + name + (generation == null ? "" : ("#" + generation));
6264
}
6365

6466
@Override
@@ -117,14 +119,18 @@ public static BlobId of(String bucket, String name, Long generation) {
117119
* @param gsUtilUri the Storage url to create the blob from
118120
*/
119121
public static BlobId fromGsUtilUri(String gsUtilUri) {
120-
if (!Pattern.matches("gs://.*/.*", gsUtilUri)) {
122+
Matcher m = gsUtilUriPattern.matcher(gsUtilUri);
123+
if (!m.matches()) {
121124
throw new IllegalArgumentException(
122-
gsUtilUri + " is not a valid gsutil URI (i.e. \"gs://bucket/blob\")");
125+
gsUtilUri
126+
+ " is not a valid gsutil URI (i.e. \"gs://bucket/blob\" or \"gs://bucket/blob#generation\")");
123127
}
124-
int blobNameStartIndex = gsUtilUri.indexOf('/', 5);
125-
String bucketName = gsUtilUri.substring(5, blobNameStartIndex);
126-
String blobName = gsUtilUri.substring(blobNameStartIndex + 1);
127128

128-
return BlobId.of(bucketName, blobName);
129+
String bucket = m.group(1);
130+
String name = m.group(2);
131+
String generationGroup = m.group(3);
132+
Long generation = generationGroup == null ? null : Long.parseLong(generationGroup);
133+
134+
return BlobId.of(bucket, name, generation);
129135
}
130136
}

google-cloud-storage/src/test/java/com/google/cloud/storage/BlobIdTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public void testToFromGsUtilUri() {
3939
assertEquals("gs://bucket/path/to/blob", blobId.toGsUtilUri());
4040
}
4141

42+
@Test
43+
public void testToFromGsUtilUriWithGeneration() {
44+
BlobId blobId = BlobId.fromGsUtilUri("gs://bucket/path/to/blob#1360887697105000");
45+
assertEquals("bucket", blobId.getBucket());
46+
assertEquals("path/to/blob", blobId.getName());
47+
assertEquals(Long.valueOf(1360887697105000L), blobId.getGeneration());
48+
assertEquals("gs://bucket/path/to/blob#1360887697105000", blobId.toGsUtilUri());
49+
}
50+
4251
@Test
4352
public void testEquals() {
4453
compareBlobIds(BLOB, BlobId.of("b", "n"));

0 commit comments

Comments
 (0)