Created
October 15, 2015 20:13
-
-
Save adamv/6bffce5310a3ce647b1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.common.base.Strings; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| /** | |
| * S3Location represents an S3 bucket and a key. | |
| */ | |
| public class S3Location { | |
| /** | |
| * Pattern for an S3 URL that captures the bucket and optionally a key | |
| */ | |
| private static final Pattern S3_URL = | |
| Pattern.compile("^s3://([^/]+)(?:/(.+))?$"); | |
| private final String bucket; | |
| private final String key; | |
| public S3Location(String bucket, String key) { | |
| if (bucket == null) { | |
| throw new IllegalArgumentException("bucket cannot be null"); | |
| } | |
| if (key == null) { | |
| throw new IllegalArgumentException("key cannot be null"); | |
| } | |
| this.bucket = bucket; | |
| this.key = key; | |
| } | |
| /** | |
| * @return the s3 bucket | |
| */ | |
| public String getBucket() { | |
| return bucket; | |
| } | |
| /** | |
| * @return the key | |
| */ | |
| public String getKey() { | |
| return key; | |
| } | |
| /** | |
| * Parses an S3Location from an s3 style URL | |
| */ | |
| public static S3Location fromURL(final String s3url) { | |
| if (s3url == null) { | |
| throw new IllegalArgumentException("s3url cannot be null"); | |
| } | |
| Matcher m = S3_URL.matcher(s3url); | |
| if (!m.matches()) { | |
| throw new IllegalArgumentException("s3url is not formatted as an S3 URL"); | |
| } | |
| return new S3Location(m.group(1), Strings.nullToEmpty(m.group(2))); | |
| } | |
| /** | |
| * @return this location formatted as an s3 url. | |
| */ | |
| public String asUrl() { | |
| return "s3://" + bucket + "/" + key; | |
| } | |
| /** | |
| * @return this location formatted as an s3 url with an additional suffix. | |
| */ | |
| public String asUrlWithSuffix(String suffix) { | |
| return asUrl() + "/" + suffix; | |
| } | |
| /** | |
| * @return a new S3Location with an additional suffix added to the key | |
| */ | |
| public S3Location withSuffix(String suffix) { | |
| if (suffix == null) { | |
| throw new IllegalArgumentException("suffix cannot be null"); | |
| } | |
| if (key.isEmpty()) { | |
| return new S3Location(this.bucket, suffix); | |
| } else { | |
| return new S3Location(this.bucket, this.key + "/" + suffix); | |
| } | |
| } | |
| @Override | |
| public String toString() { | |
| return "S3Location{" + | |
| "bucket='" + bucket + '\'' + | |
| ", key='" + key + '\'' + | |
| '}'; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) return true; | |
| if (o == null || getClass() != o.getClass()) return false; | |
| S3Location that = (S3Location) o; | |
| return bucket.equals(that.bucket) && key.equals(that.key); | |
| } | |
| @Override | |
| public int hashCode() { | |
| return 31 * bucket.hashCode() + key.hashCode(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment