Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Handle generation numbers in BlobId#{to,from}GsUtilUri #1929

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand All @@ -31,6 +32,7 @@
public final class BlobId implements Serializable {

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

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

@Override
Expand Down Expand Up @@ -117,14 +119,13 @@ public static BlobId of(String bucket, String name, Long generation) {
* @param gsUtilUri the Storage url to create the blob from
*/
public static BlobId fromGsUtilUri(String gsUtilUri) {
if (!Pattern.matches("gs://.*/.*", gsUtilUri)) {
Matcher m = gsUtilUriPattern.matcher(gsUtilUri);
if (!m.matches()) {
throw new IllegalArgumentException(
gsUtilUri + " is not a valid gsutil URI (i.e. \"gs://bucket/blob\")");
gsUtilUri + " is not a valid gsutil URI (i.e. \"gs://bucket/blob\" or \"gs://bucket/blob#generation\")");
}
int blobNameStartIndex = gsUtilUri.indexOf('/', 5);
String bucketName = gsUtilUri.substring(5, blobNameStartIndex);
String blobName = gsUtilUri.substring(blobNameStartIndex + 1);
String generationGroup = m.group(3);

return BlobId.of(bucketName, blobName);
return BlobId.of(m.group(1), m.group(2), generationGroup == null ? null : Long.parseLong(generationGroup));
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public void testToFromGsUtilUri() {
assertEquals("gs://bucket/path/to/blob", blobId.toGsUtilUri());
}

@Test
public void testToFromGsUtilUriWithGeneration() {
BlobId blobId = BlobId.fromGsUtilUri("gs://bucket/path/to/blob#1360887697105000");
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());
}

@Test
public void testEquals() {
compareBlobIds(BLOB, BlobId.of("b", "n"));
Expand Down