AWS specific components for managing endpoints in Smithy
The S3 virtual hosting functionality provides utilities for determining whether S3 bucket names can use virtual hosted-style URLs and validating bucket names for DNS compatibility.
The core library function for validating S3 bucket names for virtual hosting compatibility.
/**
* An AWS rule-set function for determining whether a given string can be promoted to an S3 virtual bucket host label.
*/
public final class IsVirtualHostableS3Bucket extends LibraryFunction {
/** Function identifier used in endpoint rules */
public static final String ID = "aws.isVirtualHostableS3Bucket";
/**
* Gets the function definition for use in the rules engine
* @return Function definition instance
*/
public static Definition getDefinition();
/**
* Creates a virtual hostable S3 bucket function from the given expressions
* @param bucketName Expression that evaluates to the bucket name to check
* @param allowSubdomains Expression that evaluates to whether to allow subdomains
* @return IsVirtualHostableS3Bucket function instance
*/
public static IsVirtualHostableS3Bucket ofExpressions(ToExpression bucketName, ToExpression allowSubdomains);
/**
* Creates a virtual hostable S3 bucket function from the given expressions
* @param bucketName Expression that evaluates to the bucket name to check
* @param allowSubdomains Expression that evaluates to whether to allow subdomains
* @return IsVirtualHostableS3Bucket function instance
*/
public static IsVirtualHostableS3Bucket ofExpressions(ToExpression bucketName, ToExpression allowSubdomains);
}Usage Examples:
import software.amazon.smithy.rulesengine.aws.language.functions.IsVirtualHostableS3Bucket;
import software.amazon.smithy.rulesengine.language.syntax.expressions.Expression;
// Create function to check if bucket supports virtual hosting
IsVirtualHostableS3Bucket hostableFunction = IsVirtualHostableS3Bucket.ofExpressions(
Expression.of("my-bucket-name"), // bucket name to check
Expression.of(true) // allow subdomains
);
// The function evaluates bucket names according to DNS hostname rules
// Returns true if the bucket name can be used as a virtual host subdomainS3 supports two URL styles for accessing buckets and objects:
https://s3.region.amazonaws.com/bucket-name/object-keyhttps://bucket-name.s3.region.amazonaws.com/object-keyFor a bucket to support virtual hosted-style URLs, the bucket name must:
// Valid virtual hosting bucket names:
"my-bucket" // ✓ Simple alphanumeric with hyphens
"example.bucket" // ✓ Contains periods (but not consecutive)
"test123" // ✓ Alphanumeric only
"my-app-logs-2024" // ✓ Descriptive with year
// Invalid virtual hosting bucket names:
"MyBucket" // ✗ Contains uppercase letters
"my..bucket" // ✗ Consecutive periods
"-my-bucket" // ✗ Starts with hyphen
"my-bucket-" // ✗ Ends with hyphen
"192.168.1.1" // ✗ Formatted as IP address
"ab" // ✗ Too short (less than 3 characters)The aws.isVirtualHostableS3Bucket function is used in endpoint rules to determine the appropriate URL style:
# Endpoint rule example (conceptual)
if aws.isVirtualHostableS3Bucket(bucketName, true):
# Use virtual hosted-style URL
endpoint = "https://{bucketName}.s3.{region}.amazonaws.com"
else:
# Use path-style URL
endpoint = "https://s3.{region}.amazonaws.com/{bucketName}"The bucket name to validate for virtual hosting compatibility. Should evaluate to a string containing the S3 bucket name.
Whether to allow subdomain-style bucket names. Should evaluate to a boolean:
true: Allow bucket names that can be used as DNS subdomainsfalse: Apply stricter validation rulesThis function works in conjunction with S3-specific built-in parameters:
// Related S3 built-ins from AwsBuiltIns:
AwsBuiltIns.S3_FORCE_PATH_STYLE // Forces path-style URLs
AwsBuiltIns.S3_USE_GLOBAL_ENDPOINT // Uses global endpoint for us-east-1
AwsBuiltIns.S3_ACCELERATE // Uses S3 Transfer Acceleration// Conceptual endpoint rule logic:
if (S3_FORCE_PATH_STYLE) {
// Always use path-style regardless of bucket name
usePathStyle = true;
} else if (aws.isVirtualHostableS3Bucket(bucketName, true)) {
// Bucket supports virtual hosting
useVirtualHosting = true;
} else {
// Fall back to path-style for non-compliant bucket names
usePathStyle = true;
}The function implements DNS hostname validation rules to ensure bucket names can be safely used as DNS subdomain labels. This is critical for:
// Recommended patterns for virtual hosting compatibility:
"company-app-logs" // Company and purpose
"user-uploads-prod" // Environment-specific
"backup.2024.january" // Date-based with periods
"static-assets-cdn" // CDN-friendly naming// Patterns that break virtual hosting:
"Company_App_Logs" // Underscores and uppercase
"my--special--bucket" // Consecutive hyphens/periods
"bucket.with..dots" // Consecutive periods
"10.0.0.1" // IP address formatInstall with Tessl CLI
npx tessl i tessl/maven-software-amazon-smithy--smithy-aws-endpoints