Class S3Utilities

java.lang.Object
software.amazon.awssdk.services.s3.S3Utilities

@Immutable @ThreadSafe public final class S3Utilities extends Object
Utilities for working with Amazon S3 objects. An instance of this class can be created by:

1) Directly using the builder() method. You have to manually specify the configuration params like region, s3Configuration on the builder.

 S3Utilities utilities = S3Utilities.builder().region(Region.US_WEST_2).build()
 GetUrlRequest request = GetUrlRequest.builder().bucket("foo-bucket").key("key-without-spaces").build();
 URL url = utilities.getUrl(request);
 

2) Using the low-level client S3Client.utilities() method. This is recommended as SDK will use the same configuration from the S3Client object to create the S3Utilities object.

 S3Client s3client = S3Client.create();
 S3Utilities utilities = s3client.utilities();
 GetUrlRequest request = GetUrlRequest.builder().bucket("foo-bucket").key("key-without-spaces").build();
 URL url = utilities.getUrl(request);
 
Note: This class does not make network calls.
  • Method Details

    • builder

      public static S3Utilities.Builder builder()
      Creates a builder for S3Utilities.
    • getUrl

      public URL getUrl(Consumer<GetUrlRequest.Builder> getUrlRequest)
      Returns the URL for an object stored in Amazon S3. If the object identified by the given bucket and key has public read permissions, then this URL can be directly accessed to retrieve the object's data.

      If same configuration options are set on both #GetUrlRequest and #S3Utilities objects (for example: region), the configuration set on the #GetUrlRequest takes precedence.

      This is a convenience which creates an instance of the GetUrlRequest.Builder avoiding the need to create one manually via GetUrlRequest.builder()

      Parameters:
      getUrlRequest - A Consumer that will call methods on GetUrlRequest.Builder to create a request.
      Returns:
      A URL for an object stored in Amazon S3.
      Throws:
      SdkException - Generated Url is malformed
    • getUrl

      public URL getUrl(GetUrlRequest getUrlRequest)
      Returns the URL for an object stored in Amazon S3. If the object identified by the given bucket and key has public read permissions, then this URL can be directly accessed to retrieve the object's data.

      If same configuration options are set on both #GetUrlRequest and #S3Utilities objects (for example: region), the configuration set on the #GetUrlRequest takes precedence.

      Parameters:
      getUrlRequest - request to construct url
      Returns:
      A URL for an object stored in Amazon S3.
      Throws:
      SdkException - Generated Url is malformed
    • parseUri

      public S3Uri parseUri(URI uri)
      Returns a parsed S3Uri with which a user can easily retrieve the bucket, key, region, style, and query parameters of the URI. Only path-style and virtual-hosted-style URI parsing is supported, including CLI-style URIs, e.g., "s3://bucket/key". AccessPoints and Outposts URI parsing is not supported. If you work with object keys and/or query parameters with special characters, they must be URL-encoded, e.g., replace " " with "%20". If you work with virtual-hosted-style URIs with bucket names that contain a dot, i.e., ".", the dot must not be URL-encoded. Encoded buckets, keys, and query parameters will be returned decoded.

      For more information on path-style and virtual-hosted-style URIs, see Methods for accessing a bucket.

      Parameters:
      uri - The URI to be parsed
      Returns:
      Parsed S3Uri

      Example Usage

           S3Client s3Client = S3Client.create();
           S3Utilities s3Utilities = s3Client.utilities();
           String uriString = "https://myBucket.s3.us-west-1.amazonaws.com/doc.txt?versionId=abc123";
           URI uri = URI.create(uriString);
           S3Uri s3Uri = s3Utilities.parseUri(uri);
      
           String bucket = s3Uri.bucket().orElse(null); // "myBucket"
           String key = s3Uri.key().orElse(null); // "doc.txt"
           Region region = s3Uri.region().orElse(null); // Region.US_WEST_1
           boolean isPathStyle = s3Uri.isPathStyle(); // false
           String versionId = s3Uri.firstMatchingRawQueryParameter("versionId").orElse(null); // "abc123"