Merge pull request #76 from xiaolongzuo/1.x
optimize spring-cloud-alicloud-oss module.pull/80/head^2
commit
0ac96c0c20
@ -1,51 +0,0 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* OSS Application
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class OSSApplication {
|
||||
|
||||
public static final String BUCKET_NAME = "spring-cloud-alibaba";
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
SpringApplication.run(OSSApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AppRunner appRunner() {
|
||||
return new AppRunner();
|
||||
}
|
||||
|
||||
class AppRunner implements ApplicationRunner {
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
try {
|
||||
if (!ossClient.doesBucketExist(BUCKET_NAME)) {
|
||||
ossClient.createBucket(BUCKET_NAME);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("oss handle bucket error: " + e.getMessage());
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.codec.CharEncoding;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.common.utils.IOUtils;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
|
||||
/**
|
||||
* OSS Controller
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RestController
|
||||
public class OSSController {
|
||||
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Value("oss://" + OSSApplication.BUCKET_NAME + "/oss-test")
|
||||
private Resource file;
|
||||
|
||||
private String dir = "custom-dir/";
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String upload() {
|
||||
try {
|
||||
ossClient.putObject(OSSApplication.BUCKET_NAME, dir + "oss-test", this
|
||||
.getClass().getClassLoader().getResourceAsStream("oss-test.json"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "upload fail: " + e.getMessage();
|
||||
}
|
||||
return "upload success";
|
||||
}
|
||||
|
||||
@GetMapping("/file-resource")
|
||||
public String fileResource() {
|
||||
try {
|
||||
return "get file resource success. content: " + StreamUtils.copyToString(
|
||||
file.getInputStream(), Charset.forName(CharEncoding.UTF_8));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "get resource fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
public String download() {
|
||||
try {
|
||||
OSSObject ossObject = ossClient.getObject(OSSApplication.BUCKET_NAME,
|
||||
dir + "oss-test");
|
||||
return "download success, content: " + IOUtils
|
||||
.readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "download fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* OSS Application
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class OssApplication {
|
||||
|
||||
public static final String BUCKET_NAME = "spring-cloud-alibaba-test";
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
SpringApplication.run(OssApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AppRunner appRunner() {
|
||||
return new AppRunner();
|
||||
}
|
||||
|
||||
class AppRunner implements ApplicationRunner {
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
try {
|
||||
if (!ossClient.doesBucketExist(BUCKET_NAME)) {
|
||||
ossClient.createBucket(BUCKET_NAME);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("oss handle bucket error: " + e.getMessage());
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.codec.CharEncoding;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.common.utils.IOUtils;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
|
||||
/**
|
||||
* OSS Controller
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RestController
|
||||
public class OssController {
|
||||
|
||||
@Autowired
|
||||
private OSS ossClient;
|
||||
|
||||
@Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json")
|
||||
private Resource file;
|
||||
|
||||
@GetMapping("/upload")
|
||||
public String upload() {
|
||||
try {
|
||||
ossClient.putObject(OssApplication.BUCKET_NAME, "oss-test.json", this
|
||||
.getClass().getClassLoader().getResourceAsStream("oss-test.json"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "upload fail: " + e.getMessage();
|
||||
}
|
||||
return "upload success";
|
||||
}
|
||||
|
||||
@GetMapping("/file-resource")
|
||||
public String fileResource() {
|
||||
try {
|
||||
return "get file resource success. content: " + StreamUtils.copyToString(
|
||||
file.getInputStream(), Charset.forName(CharEncoding.UTF_8));
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "get resource fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/download")
|
||||
public String download() {
|
||||
try {
|
||||
OSSObject ossObject = ossClient.getObject(OssApplication.BUCKET_NAME,
|
||||
"oss-test.json");
|
||||
return "download success, content: " + IOUtils
|
||||
.readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "download fail: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
spring.application.name=oss-example
|
||||
server.port=18084
|
||||
|
||||
spring.cloud.alibaba.oss.accessKeyId=[your-ak]
|
||||
spring.cloud.alibaba.oss.secretAccessKey=[your-sk]
|
||||
spring.cloud.alibaba.oss.region=[your-region]
|
||||
|
||||
management.security.enabled=false
|
||||
spring.cloud.alicloud.access-key=AK
|
||||
spring.cloud.alicloud.secret-key=SK
|
||||
spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com
|
||||
management.endpoints.web.exposure.include=*
|
||||
|
@ -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,80 +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.AbstractEndpoint;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
/**
|
||||
* Actuator Endpoint to expose OSS Meta Data
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSEndpoint extends AbstractEndpoint<Map<String, Object>> {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
public OSSEndpoint() {
|
||||
super("oss");
|
||||
}
|
||||
|
||||
@Override
|
||||
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<>();
|
||||
|
||||
for(String beanName : ossClientMap.keySet()) {
|
||||
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());
|
||||
|
||||
String[] bucketList = new String[client.listBuckets().size()];
|
||||
|
||||
for(int index = 0; index < bucketList.length; index ++) {
|
||||
bucketList[index] = client.listBuckets().get(index).getName();
|
||||
}
|
||||
|
||||
ossProperties.put("bucketList", bucketList);
|
||||
ossClientList.add(ossProperties);
|
||||
}
|
||||
|
||||
result.put("size", size);
|
||||
result.put("info", ossClientList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,84 +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.resource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ProtocolResolver;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* A {@link ProtocolResolver} implementation for the {@code oss://} protocol.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OSSStorageProtocolResolver
|
||||
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
|
||||
|
||||
public static final String PROTOCOL = "oss://";
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OSSStorageProtocolResolver.class);
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
private OSS oss;
|
||||
|
||||
private OSS getOSS() {
|
||||
if (this.oss == null) {
|
||||
if (this.beanFactory.getBeansOfType(OSS.class).size() > 1) {
|
||||
logger.warn(
|
||||
"There are multiple OSS instances, consider marking one of them as @Primary to resolve oss protocol.");
|
||||
}
|
||||
this.oss = this.beanFactory.getBean(OSS.class);
|
||||
}
|
||||
return this.oss;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource resolve(String location, ResourceLoader resourceLoader) {
|
||||
if (!location.startsWith(PROTOCOL)) {
|
||||
return null;
|
||||
}
|
||||
return new OSSStorageResource(getOSS(), location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
if (DefaultResourceLoader.class.isAssignableFrom(resourceLoader.getClass())) {
|
||||
((DefaultResourceLoader) resourceLoader).addProtocolResolver(this);
|
||||
}
|
||||
else {
|
||||
logger.warn("The provided delegate resource loader is not an implementation "
|
||||
+ "of DefaultResourceLoader. Custom Protocol using oss:// prefix will not be enabled.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||
throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
}
|
@ -1,208 +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.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.Bucket;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
|
||||
/**
|
||||
* Implements {@link Resource} for reading and writing objects in Aliyun Object Storage
|
||||
* Service (OSS). An instance of this class represents a handle to a bucket or an OSSObject.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
* @see OSS
|
||||
* @see Bucket
|
||||
* @see OSSObject
|
||||
*/
|
||||
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) {
|
||||
Assert.notNull(oss, "Object Storage Service can not be null");
|
||||
Assert.isTrue(location.startsWith(OSSStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OSSStorageProtocolResolver.PROTOCOL);
|
||||
this.oss = oss;
|
||||
try {
|
||||
URI locationUri = new URI(location);
|
||||
this.bucketName = locationUri.getAuthority();
|
||||
|
||||
if (locationUri.getPath() != null && locationUri.getPath().length() > 1) {
|
||||
this.objectKey = locationUri.getPath().substring(1);
|
||||
}
|
||||
else {
|
||||
this.objectKey = null;
|
||||
}
|
||||
this.location = locationUri;
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException("Invalid location: " + location, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
try {
|
||||
return isBucket() ? getBucket() != null : getOSSObject() != null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the oss: protocol will normally not have a URL stream handler registered,
|
||||
* this method will always throw a {@link java.net.MalformedURLException}.
|
||||
* @return The URL for the OSS resource, if a URL stream handler is registered for the
|
||||
* oss protocol.
|
||||
*/
|
||||
@Override
|
||||
public URL getURL() throws IOException {
|
||||
return this.location.toURL();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() throws IOException {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFile() throws IOException {
|
||||
throw new UnsupportedOperationException(
|
||||
getDescription() + " cannot be resolved to absolute file path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new FileNotFoundException("OSSObject not existed.");
|
||||
}
|
||||
return getOSSObject().getObjectMetadata().getContentLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long lastModified() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new FileNotFoundException("OSSObject not existed.");
|
||||
}
|
||||
return getOSSObject().getObjectMetadata().getLastModified().getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return new OSSStorageResource(this.oss,
|
||||
this.location.resolve(relativePath).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return isBucket() ? this.bucketName : this.objectKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.location.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot open an input stream to a bucket: '" + this.location + "'");
|
||||
}
|
||||
else {
|
||||
return getOSSObject().getObjectContent();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Bucket} associated with the resource.
|
||||
* @return the bucket if it exists, or null otherwise
|
||||
*/
|
||||
public Bucket getBucket() {
|
||||
for(Bucket bucket : this.oss.listBuckets()) {
|
||||
if(bucket.getName().equals(this.bucketName)) {
|
||||
return bucket;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for the existence of the {@link Bucket} associated with the resource.
|
||||
* @return true if the bucket exists
|
||||
*/
|
||||
public boolean bucketExists() {
|
||||
return getBucket() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the underlying resource object in Aliyun Object Storage Service.
|
||||
* @return The resource object, will be null if it does not exist in Aliyun Object
|
||||
* Storage Service.
|
||||
* @throws OSSException it is thrown upon error when accessing OSS
|
||||
* @throws ClientException it is the one thrown by the client side when accessing OSS
|
||||
*/
|
||||
public OSSObject getOSSObject() {
|
||||
return this.oss.getObject(this.bucketName, this.objectKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this resource references a bucket and not a blob.
|
||||
* @return if the resource is bucket
|
||||
*/
|
||||
public boolean isBucket() {
|
||||
return this.objectKey == null;
|
||||
}
|
||||
|
||||
private void assertExisted() throws FileNotFoundException {
|
||||
if (!exists()) {
|
||||
throw new FileNotFoundException("Bucket or OSSObject not existed.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 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.AbstractEndpoint;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
|
||||
/**
|
||||
* Actuator Endpoint to expose OSS Meta Data
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OssEndpoint extends AbstractEndpoint<Map<String, Object>> {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
public OssEndpoint() {
|
||||
super("oss");
|
||||
}
|
||||
|
||||
@Override
|
||||
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<>();
|
||||
|
||||
for (String beanName : ossClientMap.keySet()) {
|
||||
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());
|
||||
|
||||
String[] bucketList = new String[client.listBuckets().size()];
|
||||
|
||||
for (int index = 0; index < bucketList.length; index++) {
|
||||
bucketList[index] = client.listBuckets().get(index).getName();
|
||||
}
|
||||
|
||||
ossProperties.put("bucketList", bucketList);
|
||||
ossClientList.add(ossProperties);
|
||||
}
|
||||
|
||||
result.put("size", size);
|
||||
result.put("info", ossClientList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.resource;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ProtocolResolver;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
|
||||
/**
|
||||
* A {@link ProtocolResolver} implementation for the {@code oss://} protocol.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class OssStorageProtocolResolver
|
||||
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
|
||||
|
||||
public static final String PROTOCOL = "oss://";
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(OssStorageProtocolResolver.class);
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
private OSS oss;
|
||||
|
||||
private OSS getOSS() {
|
||||
if (this.oss == null) {
|
||||
if (this.beanFactory.getBeansOfType(OSS.class).size() > 1) {
|
||||
logger.warn(
|
||||
"There are multiple OSS instances, consider marking one of them as @Primary to resolve oss protocol.");
|
||||
}
|
||||
this.oss = this.beanFactory.getBean(OSS.class);
|
||||
}
|
||||
return this.oss;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource resolve(String location, ResourceLoader resourceLoader) {
|
||||
if (!location.startsWith(PROTOCOL)) {
|
||||
return null;
|
||||
}
|
||||
return new OssStorageResource(getOSS(), location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
if (DefaultResourceLoader.class.isAssignableFrom(resourceLoader.getClass())) {
|
||||
((DefaultResourceLoader) resourceLoader).addProtocolResolver(this);
|
||||
}
|
||||
else {
|
||||
logger.warn("The provided delegate resource loader is not an implementation "
|
||||
+ "of DefaultResourceLoader. Custom Protocol using oss:// prefix will not be enabled.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||
throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
}
|
@ -0,0 +1,211 @@
|
||||
/*
|
||||
* 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.resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.Bucket;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
|
||||
/**
|
||||
* Implements {@link Resource} for reading and writing objects in Aliyun Object Storage
|
||||
* Service (OSS). An instance of this class represents a handle to a bucket or an
|
||||
* OSSObject.
|
||||
*
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
* @see OSS
|
||||
* @see Bucket
|
||||
* @see OSSObject
|
||||
*/
|
||||
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) {
|
||||
Assert.notNull(oss, "Object Storage Service can not be null");
|
||||
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
|
||||
this.oss = oss;
|
||||
try {
|
||||
URI locationUri = new URI(location);
|
||||
this.bucketName = locationUri.getAuthority();
|
||||
|
||||
if (locationUri.getPath() != null && locationUri.getPath().length() > 1) {
|
||||
this.objectKey = locationUri.getPath().substring(1);
|
||||
}
|
||||
else {
|
||||
this.objectKey = null;
|
||||
}
|
||||
this.location = locationUri;
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException("Invalid location: " + location, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
try {
|
||||
return isBucket() ? getBucket() != null : getOSSObject() != null;
|
||||
}
|
||||
catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the oss: protocol will normally not have a URL stream handler registered,
|
||||
* this method will always throw a {@link java.net.MalformedURLException}.
|
||||
* @return The URL for the OSS resource, if a URL stream handler is registered for the
|
||||
* oss protocol.
|
||||
*/
|
||||
@Override
|
||||
public URL getURL() throws IOException {
|
||||
return this.location.toURL();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() throws IOException {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFile() throws IOException {
|
||||
throw new UnsupportedOperationException(
|
||||
getDescription() + " cannot be resolved to absolute file path");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long contentLength() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new FileNotFoundException("OSSObject not existed.");
|
||||
}
|
||||
return getOSSObject().getObjectMetadata().getContentLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long lastModified() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new FileNotFoundException("OSSObject not existed.");
|
||||
}
|
||||
return getOSSObject().getObjectMetadata().getLastModified().getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return new OssStorageResource(this.oss,
|
||||
this.location.resolve(relativePath).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return isBucket() ? this.bucketName : this.objectKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.location.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
assertExisted();
|
||||
if (isBucket()) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot open an input stream to a bucket: '" + this.location + "'");
|
||||
}
|
||||
else {
|
||||
return getOSSObject().getObjectContent();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Bucket} associated with the resource.
|
||||
* @return the bucket if it exists, or null otherwise
|
||||
*/
|
||||
public Bucket getBucket() {
|
||||
List<Bucket> buckets = this.oss.listBuckets();
|
||||
for (Bucket bucket : buckets) {
|
||||
if (bucket.getName().equals(this.bucketName)) {
|
||||
return bucket;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for the existence of the {@link Bucket} associated with the resource.
|
||||
* @return true if the bucket exists
|
||||
*/
|
||||
public boolean bucketExists() {
|
||||
return getBucket() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the underlying resource object in Aliyun Object Storage Service.
|
||||
* @return The resource object, will be null if it does not exist in Aliyun Object
|
||||
* Storage Service.
|
||||
* @throws OSSException it is thrown upon error when accessing OSS
|
||||
* @throws ClientException it is the one thrown by the client side when accessing OSS
|
||||
*/
|
||||
public OSSObject getOSSObject() {
|
||||
return this.oss.getObject(this.bucketName, this.objectKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this resource references a bucket and not a blob.
|
||||
* @return if the resource is bucket
|
||||
*/
|
||||
public boolean isBucket() {
|
||||
return this.objectKey == null;
|
||||
}
|
||||
|
||||
private void assertExisted() throws FileNotFoundException {
|
||||
if (!exists()) {
|
||||
throw new FileNotFoundException("Bucket or OSSObject not existed.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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,75 +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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
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 final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
context.register(OSSAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.cloud.alibaba.oss.accessKeyId=your-ak",
|
||||
"spring.cloud.alibaba.oss.secretAccessKey=your-sk",
|
||||
"spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com",
|
||||
"spring.cloud.alibaba.oss.configuration.userAgent=alibaba");
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOSSProperties() {
|
||||
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() {
|
||||
assertThat(context.getBeansOfType(OSS.class).size()).isEqualTo(1);
|
||||
assertThat(context.getBeanNamesForType(OSS.class)[0]).isEqualTo("ossClient");
|
||||
OSSClient ossClient = (OSSClient) context.getBean(OSS.class);
|
||||
assertThat(ossClient.getCredentialsProvider().getCredentials().getAccessKeyId())
|
||||
.isEqualTo("your-ak");
|
||||
assertThat(
|
||||
ossClient.getCredentialsProvider().getCredentials().getSecretAccessKey())
|
||||
.isEqualTo("your-sk");
|
||||
assertThat(ossClient.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba");
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
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 final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
context.register(MultiClientConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.cloud.alibaba.oss.accessKeyId=your-ak",
|
||||
"spring.cloud.alibaba.oss.secretAccessKey=your-sk",
|
||||
"spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com",
|
||||
"spring.cloud.alibaba.oss.configuration.userAgent=alibaba",
|
||||
"spring.cloud.alibaba.oss1.accessKeyId=your-ak1",
|
||||
"spring.cloud.alibaba.oss1.secretAccessKey=your-sk1",
|
||||
"spring.cloud.alibaba.oss1.endpoint=http://oss-cn-beijing.aliyuncs.com",
|
||||
"spring.cloud.alibaba.oss1.configuration.userAgent=alibaba1");
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOSSClient() {
|
||||
assertThat(context.getBeansOfType(OSS.class).size()).isEqualTo(2);
|
||||
OSSClient ossClient = (OSSClient) context.getBean("ossClient1", OSS.class);
|
||||
assertThat(ossClient.getCredentialsProvider().getCredentials().getAccessKeyId())
|
||||
.isEqualTo("your-ak");
|
||||
assertThat(
|
||||
ossClient.getCredentialsProvider().getCredentials().getSecretAccessKey())
|
||||
.isEqualTo("your-sk");
|
||||
assertThat(ossClient.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba");
|
||||
OSSClient ossClient1 = (OSSClient) context.getBean("ossClient2", OSS.class);
|
||||
assertThat(ossClient1.getCredentialsProvider().getCredentials().getAccessKeyId())
|
||||
.isEqualTo("your-ak1");
|
||||
assertThat(
|
||||
ossClient1.getCredentialsProvider().getCredentials().getSecretAccessKey())
|
||||
.isEqualTo("your-sk1");
|
||||
assertThat(ossClient1.getEndpoint().toString())
|
||||
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
|
||||
assertThat(ossClient1.getClientConfiguration().getUserAgent())
|
||||
.isEqualTo("alibaba1");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue