public static String getBucketName ( URI uri ) { if ( uri . getHost ( ) != null ) { return uri . getHost ( ) ; } if ( uri . getUserInfo ( ) == null ) { return uri . getAuthority ( ) ; } throw new IllegalArgumentException ( "Unable to determine S3 bucket from URI." ) ; }
Helper function used to work around the fact that if you use an S3 bucket with an _ that java . net . URI behaves differently and sets the host value to null whereas S3 buckets without _ have a properly set host field . _ is only allowed in S3 bucket names in us - east - 1 .