Package thredds.crawlabledataset.s3
Class S3URI
- java.lang.Object
-
- thredds.crawlabledataset.s3.S3URI
-
public class S3URI extends Object
An identifier for objects stored in Amazon S3. Ultimately, the identifier is composed solely of a bucket name and a key, and an object of this class can be constructed with those two items. However, this class also supports URIs rendered in a path-like form:s3://<bucket>/<key>
.Instances of this class are immutable.
- Since:
- 2015/08/24
-
-
Field Summary
Fields Modifier and Type Field Description static String
S3_DELIMITER
static String
S3_PREFIX
static File
S3ObjectTempDir
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
String
getBaseName()
Returns the base name of the file or directory denoted by this URI.String
getBucket()
Returns the bucket.S3URI
getChild(String relativePath)
Creates a new URI by resolving the specified path relative tothis
.String
getKey()
Returns the key.String
getKeyWithTrailingDelimiter()
Returns the key, adding a trailingdelimiter
.S3URI
getParent()
Returns the parent URI of this URI.File
getTempFile()
Gets a temporary file to which the content of the S3Object that this URI points to can be downloaded.int
hashCode()
String
toString()
Returns a string representation of the URI in the forms3://<bucket>/<key>
.
-
-
-
Field Detail
-
S3_PREFIX
public static final String S3_PREFIX
- See Also:
- Constant Field Values
-
S3_DELIMITER
public static final String S3_DELIMITER
- See Also:
- Constant Field Values
-
S3ObjectTempDir
public static final File S3ObjectTempDir
-
-
Constructor Detail
-
S3URI
public S3URI(String uri)
Creates a S3URI by extracting the S3 bucket and key names fromuri
. If the key has a trailingdelimiter
, it will be removed.- Parameters:
uri
- an S3 URI in the forms3://<bucket>/<key>
.- Throws:
IllegalArgumentException
- ifuri
is not in the expected form or if the bucket or key are invalid.- See Also:
S3URI(String, String)
-
S3URI
public S3URI(String bucket, String key) throws IllegalArgumentException
Creates a S3URI from the specified bucket and key. If the key has a trailingdelimiter
, it will be removed.- Parameters:
bucket
- a bucket name. Must be non-null
and at least 3 characters.key
- a key. May benull
but cannot be the empty string. Also, it may not contain consecutive delimiters.- Throws:
IllegalArgumentException
- if either argument fails the requirements.
-
-
Method Detail
-
getBucket
public String getBucket()
Returns the bucket.- Returns:
- the bucket.
-
getKey
public String getKey()
Returns the key. May benull
if the URI did not include a key, e.g. "s3://bucket". If not null, any trailing#S3_DELIMITER delimiter
the key had when passed to the constructor will have been stripped.- Returns:
- the key.
-
getKeyWithTrailingDelimiter
public String getKeyWithTrailingDelimiter()
- Returns:
- the key, with a trailing delimiter.
-
getBaseName
public String getBaseName()
Returns the base name of the file or directory denoted by this URI. This is just the last name in the key's name sequence. If the key isnull
,null
is returned.- Returns:
- the base name.
-
getParent
public S3URI getParent()
Returns the parent URI of this URI. The determination is completely text-based, using thedelimiter
. If the key isnull
,null
is returned. If it is non-null
, but doesn't have a logical parent, the returned URI will have anull
key (but the same bucket). For example, the parent ofs3://my-bucket/my-key
(bucket="my-bucket", key="my-key") will bes3://my-bucket
(bucket=="my-bucket", key=null).- Returns:
- the parent URI of this URI.
-
getChild
public S3URI getChild(String relativePath) throws IllegalArgumentException
Creates a new URI by resolving the specified path relative tothis
. Ifkey == null
, the key of the returned URI will simply berelativePath
.- Parameters:
relativePath
- a path relative tothis
. Must be non-null.- Returns:
- the child URI.
- Throws:
IllegalArgumentException
- if the path starts with adelimiter
. The path must be relative.
-
getTempFile
public File getTempFile()
Gets a temporary file to which the content of the S3Object that this URI points to can be downloaded. The path of the file is${java.io.tmpdir}/S3Objects/${hashCode()}/${getBaseName()}
. This method does not cause the file to be created; we're just returning a suitable path.- Returns:
- a temporary file to which the content of the S3Object that this URI points to can be downloaded.
-
toString
public String toString()
Returns a string representation of the URI in the forms3://<bucket>/<key>
.
-
-