mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
@@ -15,10 +15,14 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-alicloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
|
||||
/**
|
||||
* {@link ConfigurationProperties} for configuring OSS.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@ConfigurationProperties(prefix = OSSConstants.PREFIX)
|
||||
public class OSSProperties {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OSSProperties.class);
|
||||
|
||||
public static final Map<String, String> endpointMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
endpointMap.put("cn-beijing", "http://oss-cn-beijing.aliyuncs.com");
|
||||
endpointMap.put("cn-qingdao", "http://oss-cn-qingdao.aliyuncs.com");
|
||||
endpointMap.put("cn-hangzhou", "http://oss-cn-hangzhou.aliyuncs.com");
|
||||
endpointMap.put("cn-hongkong", "http://oss-cn-hongkong.aliyuncs.com");
|
||||
endpointMap.put("cn-shenzhen", "http://oss-cn-shenzhen.aliyuncs.com");
|
||||
endpointMap.put("us-west-1", "http://oss-us-west-1.aliyuncs.com");
|
||||
endpointMap.put("ap-southeast-1", "http://oss-ap-southeast-1.aliyuncs.com");
|
||||
}
|
||||
|
||||
private ClientBuilderConfiguration configuration;
|
||||
|
||||
private String accessKeyId;
|
||||
|
||||
private String secretAccessKey;
|
||||
|
||||
private String region;
|
||||
|
||||
private String endpoint;
|
||||
|
||||
// support ram sts
|
||||
private String securityToken;
|
||||
|
||||
public ClientBuilderConfiguration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public void setConfiguration(ClientBuilderConfiguration configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public String getAccessKeyId() {
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
public void setAccessKeyId(String accessKeyId) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
}
|
||||
|
||||
public String getSecretAccessKey() {
|
||||
return secretAccessKey;
|
||||
}
|
||||
|
||||
public void setSecretAccessKey(String secretAccessKey) {
|
||||
this.secretAccessKey = secretAccessKey;
|
||||
}
|
||||
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
public String getSecurityToken() {
|
||||
return securityToken;
|
||||
}
|
||||
|
||||
public void setSecurityToken(String securityToken) {
|
||||
this.securityToken = securityToken;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
if (!endpointMap.containsKey(region)) {
|
||||
String errorStr = "error region: " + region + ", please choose from "
|
||||
+ Arrays.toString(endpointMap.keySet().toArray());
|
||||
logger.error(errorStr);
|
||||
throw new IllegalArgumentException(errorStr);
|
||||
}
|
||||
this.region = region;
|
||||
this.setEndpoint(endpointMap.get(region));
|
||||
}
|
||||
}
|
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.endpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
/**
|
||||
* Actuator {@link Endpoint} to expose OSS Meta Data
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@Endpoint(id = "oss")
|
||||
public class OSSEndpoint {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> invoke() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
Map<String, OSSClient> ossClientMap = applicationContext
|
||||
.getBeansOfType(OSSClient.class);
|
||||
|
||||
int size = ossClientMap.size();
|
||||
|
||||
List<Object> ossClientList = new ArrayList<>();
|
||||
|
||||
ossClientMap.keySet().forEach(beanName -> {
|
||||
Map<String, Object> ossProperties = new HashMap<>();
|
||||
OSSClient client = ossClientMap.get(beanName);
|
||||
ossProperties.put("beanName", beanName);
|
||||
ossProperties.put("endpoint", client.getEndpoint().toString());
|
||||
ossProperties.put("clientConfiguration", client.getClientConfiguration());
|
||||
ossProperties.put("credentials",
|
||||
client.getCredentialsProvider().getCredentials());
|
||||
ossProperties.put("bucketList", client.listBuckets().stream()
|
||||
.map(bucket -> bucket.getName()).toArray());
|
||||
ossClientList.add(ossProperties);
|
||||
});
|
||||
|
||||
result.put("size", size);
|
||||
result.put("info", ossClientList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss;
|
||||
package org.springframework.cloud.alicloud.oss;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,10 +30,10 @@ import com.aliyun.oss.OSS;
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSApplicationListener implements ApplicationListener<ContextClosedEvent> {
|
||||
public class OssApplicationListener implements ApplicationListener<ContextClosedEvent> {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OSSApplicationListener.class);
|
||||
.getLogger(OssApplicationListener.class);
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextClosedEvent event) {
|
@@ -14,21 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss;
|
||||
package org.springframework.cloud.alicloud.oss;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.aliyun.oss.OSS;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.alibaba.oss.resource.OSSStorageProtocolResolver;
|
||||
import org.springframework.cloud.alicloud.oss.resource.OssStorageProtocolResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
|
||||
/**
|
||||
* OSS Auto {@link Configuration}
|
||||
*
|
||||
@@ -36,26 +31,13 @@ import com.aliyun.oss.OSSClientBuilder;
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(OSS.class)
|
||||
@ConditionalOnProperty(name = OSSConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(OSSProperties.class)
|
||||
public class OSSAutoConfiguration {
|
||||
@ConditionalOnProperty(name = OssConstants.ENABLED, havingValue = "true", matchIfMissing = true)
|
||||
public class OssAutoConfiguration {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OSSAutoConfiguration.class);
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@Bean
|
||||
public OSS ossClient(OSSProperties ossProperties) {
|
||||
logger.info("construct OSS because it is missing");
|
||||
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
|
||||
ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(),
|
||||
ossProperties.getSecurityToken(), ossProperties.getConfiguration());
|
||||
}
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@Bean
|
||||
public OSSStorageProtocolResolver ossStorageProtocolResolver() {
|
||||
return new OSSStorageProtocolResolver();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public OssStorageProtocolResolver ossStorageProtocolResolver() {
|
||||
return new OssStorageProtocolResolver();
|
||||
}
|
||||
|
||||
}
|
@@ -14,14 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss;
|
||||
package org.springframework.cloud.alicloud.oss;
|
||||
|
||||
/**
|
||||
* OSS constants
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public interface OSSConstants {
|
||||
public interface OssConstants {
|
||||
|
||||
String PREFIX = "spring.cloud.alibaba.oss";
|
||||
String ENABLED = PREFIX + ".enabled";
|
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alicloud.oss.endpoint;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Actuator {@link Endpoint} to expose OSS Meta Data
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@Endpoint(id = "oss")
|
||||
public class OssEndpoint {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> invoke() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
Map<String, OSSClient> ossClientMap = applicationContext
|
||||
.getBeansOfType(OSSClient.class);
|
||||
|
||||
int size = ossClientMap.size();
|
||||
|
||||
List<Object> ossClientList = new ArrayList<>();
|
||||
|
||||
ossClientMap.keySet().forEach(beanName -> {
|
||||
Map<String, Object> ossProperties = new HashMap<>();
|
||||
OSSClient client = ossClientMap.get(beanName);
|
||||
ossProperties.put("beanName", beanName);
|
||||
ossProperties.put("endpoint", client.getEndpoint().toString());
|
||||
ossProperties.put("clientConfiguration", client.getClientConfiguration());
|
||||
ossProperties.put("credentials",
|
||||
client.getCredentialsProvider().getCredentials());
|
||||
ossProperties.put("bucketList", client.listBuckets().stream()
|
||||
.map(bucket -> bucket.getName()).toArray());
|
||||
ossClientList.add(ossProperties);
|
||||
});
|
||||
|
||||
result.put("size", size);
|
||||
result.put("info", ossClientList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.endpoint;
|
||||
package org.springframework.cloud.alicloud.oss.endpoint;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
@@ -29,13 +29,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@ConditionalOnClass(Endpoint.class)
|
||||
public class OSSEndpointAutoConfiguration {
|
||||
public class OssEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
public OSSEndpoint sentinelEndPoint() {
|
||||
return new OSSEndpoint();
|
||||
public OssEndpoint sentinelEndPoint() {
|
||||
return new OssEndpoint();
|
||||
}
|
||||
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.resource;
|
||||
package org.springframework.cloud.alicloud.oss.resource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -34,13 +34,13 @@ import com.aliyun.oss.OSS;
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSStorageProtocolResolver
|
||||
public class OssStorageProtocolResolver
|
||||
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
|
||||
|
||||
public static final String PROTOCOL = "oss://";
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OSSStorageProtocolResolver.class);
|
||||
.getLogger(OssStorageProtocolResolver.class);
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@@ -62,7 +62,7 @@ public class OSSStorageProtocolResolver
|
||||
if (!location.startsWith(PROTOCOL)) {
|
||||
return null;
|
||||
}
|
||||
return new OSSStorageResource(getOSS(), location);
|
||||
return new OssStorageResource(getOSS(), location);
|
||||
}
|
||||
|
||||
@Override
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.resource;
|
||||
package org.springframework.cloud.alicloud.oss.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -42,17 +42,17 @@ import com.aliyun.oss.model.OSSObject;
|
||||
* @see Bucket
|
||||
* @see OSSObject
|
||||
*/
|
||||
public class OSSStorageResource implements Resource {
|
||||
public class OssStorageResource implements Resource {
|
||||
|
||||
private final OSS oss;
|
||||
private final String bucketName;
|
||||
private final String objectKey;
|
||||
private final URI location;
|
||||
|
||||
public OSSStorageResource(OSS oss, String location) {
|
||||
public OssStorageResource(OSS oss, String location) {
|
||||
Assert.notNull(oss, "Object Storage Service can not be null");
|
||||
Assert.isTrue(location.startsWith(OSSStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OSSStorageProtocolResolver.PROTOCOL);
|
||||
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
|
||||
this.oss = oss;
|
||||
try {
|
||||
URI locationUri = new URI(location);
|
||||
@@ -123,7 +123,7 @@ public class OSSStorageResource implements Resource {
|
||||
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return new OSSStorageResource(this.oss,
|
||||
return new OssStorageResource(this.oss,
|
||||
this.location.resolve(relativePath).toString());
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.alibaba.oss.OSSAutoConfiguration,\
|
||||
org.springframework.cloud.alibaba.oss.endpoint.OSSEndpointAutoConfiguration
|
||||
org.springframework.cloud.alicloud.oss.OssAutoConfiguration,\
|
||||
org.springframework.cloud.alicloud.oss.endpoint.OssEndpointAutoConfiguration
|
||||
org.springframework.context.ApplicationListener=\
|
||||
org.springframework.cloud.alibaba.oss.OSSApplicationListener
|
||||
org.springframework.cloud.alicloud.oss.OssApplicationListener
|
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.cloud.alibaba.oss.OSSAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.oss.OSSProperties;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
/**
|
||||
* {@link OSS} {@link OSSProperties} Test
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSAutoConfigurationTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(OSSAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.alibaba.oss.accessKeyId=your-ak")
|
||||
.withPropertyValues("spring.cloud.alibaba.oss.secretAccessKey=your-sk")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss.configuration.userAgent=alibaba");
|
||||
|
||||
@Test
|
||||
public void testOSSProperties() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context.getBeansOfType(OSSProperties.class).size() == 1).isTrue();
|
||||
OSSProperties ossProperties = context.getBean(OSSProperties.class);
|
||||
assertThat(ossProperties.getAccessKeyId()).isEqualTo("your-ak");
|
||||
assertThat(ossProperties.getSecretAccessKey()).isEqualTo("your-sk");
|
||||
assertThat(ossProperties.getEndpoint())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossProperties.getConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOSSClient() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context.getBeansOfType(OSS.class).size() == 1).isTrue();
|
||||
assertThat(context.getBeanNamesForType(OSS.class)[0]).isEqualTo("ossClient");
|
||||
OSSClient ossClient = (OSSClient) context.getBean(OSS.class);
|
||||
assertThat(ossClient.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba");
|
||||
assertThat(
|
||||
ossClient.getCredentialsProvider().getCredentials().getAccessKeyId())
|
||||
.isEqualTo("your-ak");
|
||||
assertThat(ossClient.getCredentialsProvider().getCredentials()
|
||||
.getSecretAccessKey()).isEqualTo("your-sk");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@@ -1,116 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.alibaba.oss.test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.cloud.alibaba.oss.OSSAutoConfiguration;
|
||||
import org.springframework.cloud.alibaba.oss.OSSProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
|
||||
/**
|
||||
* Multi {@link OSS} {@link OSSProperties} Test
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSMultiClientAutoConfigurationTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(OSSAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.alibaba.oss.accessKeyId=your-ak")
|
||||
.withPropertyValues("spring.cloud.alibaba.oss.secretAccessKey=your-sk")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss.configuration.userAgent=alibaba")
|
||||
.withPropertyValues("spring.cloud.alibaba.oss1.accessKeyId=your-ak1")
|
||||
.withPropertyValues("spring.cloud.alibaba.oss1.secretAccessKey=your-sk1")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss1.endpoint=http://oss-cn-beijing.aliyuncs.com")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.alibaba.oss1.configuration.userAgent=alibaba1");
|
||||
|
||||
@Test
|
||||
public void testOSSClient() {
|
||||
this.contextRunner.withUserConfiguration(MultiClientConfiguration.class)
|
||||
.run(context -> {
|
||||
assertThat(context.getBeansOfType(OSS.class).size() == 2).isTrue();
|
||||
OSSClient ossClient = (OSSClient) context.getBean("ossClient1",
|
||||
OSS.class);
|
||||
assertThat(ossClient.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba");
|
||||
assertThat(ossClient.getCredentialsProvider().getCredentials()
|
||||
.getAccessKeyId()).isEqualTo("your-ak");
|
||||
assertThat(ossClient.getCredentialsProvider().getCredentials()
|
||||
.getSecretAccessKey()).isEqualTo("your-sk");
|
||||
OSSClient ossClient1 = (OSSClient) context.getBean("ossClient2",
|
||||
OSS.class);
|
||||
assertThat(ossClient1.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient1.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba1");
|
||||
assertThat(ossClient1.getCredentialsProvider().getCredentials()
|
||||
.getAccessKeyId()).isEqualTo("your-ak1");
|
||||
assertThat(ossClient1.getCredentialsProvider().getCredentials()
|
||||
.getSecretAccessKey()).isEqualTo("your-sk1");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class MultiClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.cloud.alibaba.oss")
|
||||
public OSSProperties ossProperties1() {
|
||||
return new OSSProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OSS ossClient1(@Qualifier("ossProperties1") OSSProperties ossProperties) {
|
||||
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
|
||||
ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(),
|
||||
ossProperties.getSecurityToken(), ossProperties.getConfiguration());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.cloud.alibaba.oss1")
|
||||
public OSSProperties ossProperties2() {
|
||||
return new OSSProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OSS ossClient2(@Qualifier("ossProperties2") OSSProperties ossProperties) {
|
||||
return new OSSClientBuilder().build(ossProperties.getEndpoint(),
|
||||
ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(),
|
||||
ossProperties.getSecurityToken(), ossProperties.getConfiguration());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user