S3ProviderUtil { public static String createNewBucketName(String accessKeyId, String spaceId) { String bucketName = accessKeyId + "." + spaceId; bucketName = bucketName.toLowerCase(); bucketName = bucketName.replaceAll("[^a-z0-9-.]", "-"); while (bucketName.contains("--") || bucketName.contains("..") || bucketName.contains("-.") || bucketName.contains(".-")) { bucketName = bucketName.replaceAll("[-]+", "-"); bucketName = bucketName.replaceAll("[.]+", "."); bucketName = bucketName.replaceAll("-[.]", "-"); bucketName = bucketName.replaceAll("[.]-", "."); } if (bucketName.length() > 63) { bucketName = bucketName.substring(0, 63); } while (bucketName.endsWith("-") || bucketName.endsWith(".")) { bucketName = bucketName.substring(0, bucketName.length() - 1); } return bucketName; } private  S3ProviderUtil(); static AmazonS3 getAmazonS3Client(String accessKey,
                                             String secretKey,
                                             Map<String, String> options); static AmazonCloudFrontClient getAmazonCloudFrontClient(String accessKey,
                                                                   String secretKey); static String createNewBucketName(String accessKeyId,
                                             String spaceId); static Resource getS3ObjectByUrl(String s3Url);  }
@Test public void testGetBucketName() throws Exception { String accessKey = "abc"; String bucketName = S3ProviderUtil.createNewBucketName(accessKey, "xyz"); assertEquals("abc.xyz", bucketName); bucketName = S3ProviderUtil.createNewBucketName(accessKey, "x~!@#$%^&*(){}:;'\"<>,?/|z"); assertEquals("abc.x-z", bucketName); bucketName = S3ProviderUtil.createNewBucketName(accessKey, "x--..y..--z-."); assertEquals("abc.x-y.z", bucketName); }