|
22 | 22 | import com.google.common.base.MoreObjects; |
23 | 23 | import java.io.Serializable; |
24 | 24 | import java.util.Objects; |
| 25 | +import java.util.regex.Pattern; |
25 | 26 |
|
26 | 27 | /** |
27 | 28 | * Google Storage Object identifier. A {@code BlobId} object includes the name of the containing |
@@ -56,6 +57,11 @@ public Long getGeneration() { |
56 | 57 | return generation; |
57 | 58 | } |
58 | 59 |
|
| 60 | + /** Returns this blob's Storage url which can be used with gsutil */ |
| 61 | + public String toGsUtilUri() { |
| 62 | + return "gs://" + bucket + "/" + name; |
| 63 | + } |
| 64 | + |
59 | 65 | @Override |
60 | 66 | public String toString() { |
61 | 67 | return MoreObjects.toStringHelper(this) |
@@ -114,6 +120,23 @@ public static BlobId of(String bucket, String name, Long generation) { |
114 | 120 | return new BlobId(checkNotNull(bucket), checkNotNull(name), generation); |
115 | 121 | } |
116 | 122 |
|
| 123 | + /** |
| 124 | + * Creates a {@code BlobId} object. |
| 125 | + * |
| 126 | + * @param gsUtilUri the Storage url to create the blob from |
| 127 | + */ |
| 128 | + public static BlobId fromGsUtilUri(String gsUtilUri) { |
| 129 | + if (!Pattern.matches("gs://.*/.*", gsUtilUri)) { |
| 130 | + throw new IllegalArgumentException( |
| 131 | + gsUtilUri + " is not a valid gsutil URI (i.e. \"gs://bucket/blob\")"); |
| 132 | + } |
| 133 | + int blobNameStartIndex = gsUtilUri.indexOf('/', 5); |
| 134 | + String bucketName = gsUtilUri.substring(5, blobNameStartIndex); |
| 135 | + String blobName = gsUtilUri.substring(blobNameStartIndex + 1); |
| 136 | + |
| 137 | + return BlobId.of(bucketName, blobName); |
| 138 | + } |
| 139 | + |
117 | 140 | static BlobId fromPb(StorageObject storageObject) { |
118 | 141 | return BlobId.of( |
119 | 142 | storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); |
|
0 commit comments