S3ProviderUtil { public static AmazonS3 getAmazonS3Client(String accessKey, String secretKey, Map<String, String> options) { AmazonS3 client = s3Clients.get(key(accessKey, secretKey, options)); if (null == client) { Region region = null; URL endpoint = null; String signer = null; if (options != null) { if (options.get(OPTS.SWIFT_S3_ENDPOINT.name()) != null) { try { endpoint = new URL(options.get(OPTS.SWIFT_S3_ENDPOINT.name())); } catch (MalformedURLException e) { String err = "The provided Swift S3 Endpoint URL is invalid: " + e.getMessage(); throw new StorageException(err, e); } if (options.get(OPTS.SWIFT_S3_SIGNER_TYPE.name()) != null) { signer = options.get(OPTS.SWIFT_S3_SIGNER_TYPE.name()); } } else if (options.get(OPTS.AWS_REGION.name()) != null) { region = com.amazonaws.services.s3.model.Region.fromValue( options.get(OPTS.AWS_REGION.name())).toAWSRegion(); } } client = newS3Client(accessKey, secretKey, region, endpoint, signer); s3Clients.put(key(accessKey, secretKey, options), client); } return client; } 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 testGetDifferentAmazonS3Clients() { String accessKey = "access-key"; String privateKey = "private-key"; Map<String, String> optionsA = new HashMap<>(); optionsA.put(OPTS.AWS_REGION.name(), "us-east-1"); Map<String, String> optionsB = new HashMap<>(); optionsB.put(OPTS.AWS_REGION.name(), "us-west-2"); AmazonS3 s3ClientA = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsA); assertEquals("us-east-1", s3ClientA.getRegionName()); AmazonS3 s3ClientB = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsB); assertEquals("us-west-2", s3ClientB.getRegionName()); assertNotSame(s3ClientA, s3ClientB); }
@Test public void testGetSameAmazonS3Clients() { String accessKey = "access-key"; String privateKey = "private-key"; Map<String, String> optionsA = new HashMap<>(); optionsA.put(OPTS.AWS_REGION.name(), "us-east-1"); Map<String, String> optionsB = new HashMap<>(); optionsB.put(OPTS.AWS_REGION.name(), "us-east-1"); AmazonS3 s3ClientA = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsA); assertEquals("us-east-1", s3ClientA.getRegionName()); AmazonS3 s3ClientB = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsB); assertEquals("us-east-1", s3ClientB.getRegionName()); assertSame(s3ClientA, s3ClientB); }
@Test public void testGetDifferentSwiftS3Clients() { String accessKey = "access-key"; String privateKey = "private-key"; Map<String, String> optionsA = new HashMap<>(); optionsA.put(OPTS.SWIFT_S3_ENDPOINT.name(), "https: Map<String, String> optionsB = new HashMap<>(); optionsB.put(OPTS.SWIFT_S3_ENDPOINT.name(), "https: AmazonS3 s3ClientA = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsA); AmazonS3 s3ClientB = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsB); assertNotSame(s3ClientA, s3ClientB); }
@Test public void testGetSameSwiftS3Clients() { String accessKey = "access-key"; String privateKey = "private-key"; Map<String, String> optionsA = new HashMap<>(); optionsA.put(OPTS.SWIFT_S3_ENDPOINT.name(), "https: Map<String, String> optionsB = new HashMap<>(); optionsB.put(OPTS.SWIFT_S3_ENDPOINT.name(), "https: AmazonS3 s3ClientA = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsA); AmazonS3 s3ClientB = S3ProviderUtil.getAmazonS3Client(accessKey, privateKey, optionsB); assertSame(s3ClientA, s3ClientB); }