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: Add from and to storage url options for BlobId #888

Merged
merged 2 commits into from Jun 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down Expand Up @@ -56,6 +57,11 @@ public Long getGeneration() {
return generation;
}

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

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down Expand Up @@ -114,6 +120,21 @@ public static BlobId of(String bucket, String name, Long generation) {
return new BlobId(checkNotNull(bucket), checkNotNull(name), generation);
}

/**
* Creates a {@code BlobId} object.
*
* @param storageUrl the Storage url to create the blob from
*/
public static BlobId fromStorageUrl(String storageUrl) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fromGsutilUri instead of storage? To my knowledge, there are many different types of storage URLs

  • https://storage.googleapis.com/bucket/blob
  • https://storage.cloud.google.com/bucket/blob

In the cloud console these format of URI are labeled gsutil URI
image

if(!Pattern.matches("gs://.*/.*", storageUrl)) {
throw new IllegalArgumentException(storageUrl + " is not a valid Storage URL");
}
String bucketName = storageUrl.split("/")[2];
String blobName = storageUrl.split(bucketName + "/")[1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly simplify string allocation and traversal?

Suggested change
String bucketName = storageUrl.split("/")[2];
String blobName = storageUrl.split(bucketName + "/")[1];
int blobNameStartIndex = storageUrl.indexOf('/', 5);
String bucketName = storageUrl.substring(5, blobNameStartIndex);
String blobName = storageUrl.substring(blobNameStartIndex);


return BlobId.of(bucketName, blobName);
}

static BlobId fromPb(StorageObject storageObject) {
return BlobId.of(
storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration());
Expand Down
Expand Up @@ -31,6 +31,14 @@ public void testOf() {
assertEquals("n", blobId.getName());
}

@Test
public void testToFromStorageUrl() {
BlobId blobId = BlobId.fromStorageUrl("gs://bucket/path/to/blob");
assertEquals("bucket", blobId.getBucket());
assertEquals("path/to/blob", blobId.getName());
assertEquals("gs://bucket/path/to/blob", blobId.toStorageUrl());
}

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