yonge 4 年之前
父節點
當前提交
3b57d7dcbe

+ 5 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/UploadFileService.java

@@ -14,7 +14,8 @@ import org.springframework.stereotype.Service;
 
 import com.ym.mec.common.entity.UploadReturnBean;
 import com.ym.mec.common.exception.BizException;
-import com.ym.mec.thirdparty.storage.StoragePlugin;
+import com.ym.mec.thirdparty.storage.StoragePluginContext;
+import com.ym.mec.thirdparty.storage.provider.AliyunOssStoragePlugin;
 import com.ym.mec.util.upload.UploadUtil;
 
 /** 
@@ -24,7 +25,7 @@ import com.ym.mec.util.upload.UploadUtil;
 public class UploadFileService {
 
 	@Autowired
-	private StoragePlugin storagePlugin;
+	private StoragePluginContext storagePluginContext;
 
 	/** 最大上传大小,单位kb */
 	@Value("${common.upload.maxSize:153600}")
@@ -66,7 +67,8 @@ public class UploadFileService {
 			return uploadReturn;
 		}
 
-		String url = storagePlugin.uploadFile(staticFloder + folder, file);
+		//String url = storagePlugin.uploadFile(staticFloder + folder, file);
+		String url = storagePluginContext.uploadFile(AliyunOssStoragePlugin.PLUGIN_NAME,staticFloder + folder, file);
 
 		FileUtils.deleteQuietly(file);
 

+ 6 - 0
mec-thirdparty/pom.xml

@@ -104,6 +104,12 @@
 			<artifactId>feign-form-spring</artifactId>
 			<version>2.0.5</version>
 		</dependency>
+        
+		<dependency>
+			<groupId>com.ksyun</groupId>
+			<artifactId>ks3-kss-java-sdk</artifactId>
+			<version>0.8.14</version>
+		</dependency>
 	</dependencies>
 
 </project>

+ 2 - 0
mec-thirdparty/src/main/java/com/ym/mec/thirdparty/storage/StoragePlugin.java

@@ -4,6 +4,8 @@ import java.io.File;
 import java.io.IOException;
 
 public interface StoragePlugin {
+	
+	String getName();
 
 	/**
 	 * 上传文件

+ 16 - 75
mec-thirdparty/src/main/java/com/ym/mec/thirdparty/storage/StoragePluginContext.java

@@ -1,97 +1,38 @@
 package com.ym.mec.thirdparty.storage;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
 import org.springframework.stereotype.Component;
 
 import com.ym.mec.thirdparty.exception.ThirdpartyException;
-import com.ym.mec.thirdparty.storage.provider.AliyunOssStoragePlugin;
 
 @Component
-public class StoragePluginContext implements ApplicationContextAware, InitializingBean {
+public class StoragePluginContext {
 
-	@Value("${thirdparty.storagePluginName:Aliyun}")
-	private String storagePluginName;
-
-	private ApplicationContext applicationContext;
-
-	private final Map<String, String> mapper = new HashMap<String, String>() {
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = -9071481806931421021L;
-
-		{
-			put(AliyunOssStoragePlugin.getName(), StringUtils.uncapitalize(AliyunOssStoragePlugin.class.getSimpleName()));
-		}
-	};
-
-	private StoragePlugin storagePlugin;
-
-	@Override
-	public void afterPropertiesSet() throws Exception {
-		if (StringUtils.isBlank(storagePluginName)) {
-			throw new ThirdpartyException("存储插件变量thirdparty.storagePlugName不能为空");
-		}
-
-		storagePlugin = getStoragePlugin(storagePluginName);
-
-		if (storagePlugin == null) {
-			throw new ThirdpartyException("存储插件{}不存在", storagePluginName);
+	private static final Map<String, StoragePlugin> mapper = new HashMap<String, StoragePlugin>();
+	
+	public static void addStoragePlugin(StoragePlugin storagePlugin) {
+		if (mapper.containsKey(storagePlugin.getName())) {
+			throw new ThirdpartyException("存储插件:{}已存在", storagePlugin.getName());
 		}
+		mapper.put(storagePlugin.getName(), storagePlugin);
 	}
-
-	@Override
-	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-		this.applicationContext = applicationContext;
-	}
-
-	/**
-	 * 上传文件
-	 * @param vendors 供应商
-	 * @param folderName 文件夹
-	 * @param file 需要上传的文件
-	 * @return 返回文件路径
-	 */
-	public String uploadFile(String folderName, File file) {
-
-		return storagePlugin.uploadFile(folderName, file);
-	}
-
-	/**
-	 * 下载文件
-	 * @param vendors 供应商
-	 * @param folderName 文件夹
-	 * @param fileName 文件名称
-	 * @return 返回文件内容
-	 * @throws IOException
-	 */
-	public byte[] getFile(String folderName, String fileName) throws IOException {
-
-		return storagePlugin.getFile(folderName, fileName);
+	
+	public String uploadFile(String storagePluginName, String folderName, File file){
+		StoragePlugin StoragePlugin = getStoragePlugin(storagePluginName);
+		return StoragePlugin.uploadFile(folderName, file);
 	}
 
-	public void setStoragePluginName(String storagePluginName) {
-		this.storagePluginName = storagePluginName;
-	}
+	private StoragePlugin getStoragePlugin(String storagePluginName) {
+		StoragePlugin storagePlugin = mapper.get(storagePluginName);
 
-	private StoragePlugin getStoragePlugin(String vendors) {
-		String beanId = mapper.get(vendors);
-
-		if (StringUtils.isBlank(beanId)) {
-			throw new ThirdpartyException("存储提供方不存在");
+		if (storagePlugin == null) {
+			throw new ThirdpartyException("存储插件:{}不存在", storagePluginName);
 		}
 
-		return applicationContext.getBean(beanId, StoragePlugin.class);
+		return storagePlugin;
 	}
 
 }

+ 8 - 3
mec-thirdparty/src/main/java/com/ym/mec/thirdparty/storage/provider/AliyunOssStoragePlugin.java

@@ -3,7 +3,7 @@ package com.ym.mec.thirdparty.storage.provider;
 import java.io.File;
 import java.io.IOException;
 
-import org.apache.commons.io.IOUtils;
+import org.apache.poi.util.IOUtils;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Value;
@@ -14,10 +14,13 @@ import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.OSSObject;
 import com.ym.mec.thirdparty.exception.ThirdpartyException;
 import com.ym.mec.thirdparty.storage.StoragePlugin;
+import com.ym.mec.thirdparty.storage.StoragePluginContext;
 
 @Component
 public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
 
+	public final static String PLUGIN_NAME = "Aliyun";
+
 	@Value("${storage.oss.endpoint:oss-cn-beijing.aliyuncs.com}")
 	private String endpoint;
 
@@ -32,8 +35,8 @@ public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean,
 
 	private OSSClient ossClient;
 
-	public static String getName() {
-		return "Aliyun";
+	public String getName() {
+		return PLUGIN_NAME;
 	}
 
 	@Override
@@ -57,6 +60,8 @@ public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean,
 		conf.setSupportCname(true);
 
 		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
+		
+		StoragePluginContext.addStoragePlugin(this);
 	}
 
 	@Override

+ 145 - 0
mec-thirdparty/src/main/java/com/ym/mec/thirdparty/storage/provider/KS3StoragePlugin.java

@@ -0,0 +1,145 @@
+package com.ym.mec.thirdparty.storage.provider;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.poi.util.IOUtils;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import com.ksyun.ks3.dto.CannedAccessControlList;
+import com.ksyun.ks3.dto.GetObjectResult;
+import com.ksyun.ks3.dto.Ks3Object;
+import com.ksyun.ks3.http.HttpClientConfig;
+import com.ksyun.ks3.service.Ks3;
+import com.ksyun.ks3.service.Ks3Client;
+import com.ksyun.ks3.service.Ks3ClientConfig;
+import com.ksyun.ks3.service.Ks3ClientConfig.PROTOCOL;
+import com.ksyun.ks3.service.request.GetObjectRequest;
+import com.ksyun.ks3.service.request.PutObjectRequest;
+import com.ym.mec.thirdparty.exception.ThirdpartyException;
+import com.ym.mec.thirdparty.storage.StoragePlugin;
+import com.ym.mec.thirdparty.storage.StoragePluginContext;
+
+@Component
+public class KS3StoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
+
+	public final static String PLUGIN_NAME = "Ksyun";
+
+	@Value("${storage.oss.endpoint:ks3-cn-beijing.ksyun.com}")
+	private String endpoint;
+
+	@Value("${storage.oss.accessKeyId:AKLTtTeIbadpRG-pil4S0Q4m-Q}")
+	private String accessKeyId;
+
+	@Value("${storage.oss.accessKeySecret:OB1HmNOfDNW95wHoxMkP6IPFZXormk2ngA800TkvKAw7ozhiJGRqrMnnV8ZrAU3WRQ==}")
+	private String accessKeySecret;
+
+	@Value("${storage.oss.bucketName:daya}")
+	private String bucketName;
+
+	private Ks3 client;
+
+	public String getName() {
+		return PLUGIN_NAME;
+	}
+
+	@Override
+	public void afterPropertiesSet() throws Exception {
+		Ks3ClientConfig config = new Ks3ClientConfig();
+		config.setEndpoint(endpoint);// 如果使用自定义域名,设置endpoint为自定义域名,同时设置domainMode为true
+		/**
+		 * true:表示以自定义域名访问 
+		 * false:表示以KS3的外网域名或内网域名访问
+		 * 默认为false
+		 */
+		config.setDomainMode(false);
+		config.setProtocol(PROTOCOL.http);
+
+		/**
+		 * true表示以   endpoint/{bucket}/{key}的方式访问
+		 * false表示以  {bucket}.endpoint/{key}的方式访问
+		 * 如果domainMode设置为true,pathStyleAccess可忽略设置
+		 */
+		config.setPathStyleAccess(false);
+		HttpClientConfig hconfig = new HttpClientConfig();
+		// 在HttpClientConfig中可以设置httpclient的相关属性,比如代理,超时,重试等。
+		config.setHttpClientConfig(hconfig);
+		client = new Ks3Client(accessKeyId, accessKeySecret, config);
+
+		StoragePluginContext.addStoragePlugin(this);
+	}
+
+	@Override
+	public String uploadFile(String folderName, File file) {
+		if (!file.exists()) {
+			throw new ThirdpartyException("需要上传的文件[{}]不存在", file.getAbsolutePath());
+		}
+
+		if (folderName.endsWith("/")) {
+			folderName = folderName.substring(0, folderName.length() - 1);
+		}
+
+		PutObjectRequest request = new PutObjectRequest(bucketName, folderName + "/" + file.getName(), file);
+
+		// 上传一个公开文件
+		request.setCannedAcl(CannedAccessControlList.PublicRead);
+
+		client.putObject(request);
+
+		return "https://" + bucketName + "." + endpoint + "/" + folderName + "/" + file.getName();
+	}
+
+	@Override
+	public byte[] getFile(String folderName, String fileName) throws IOException {
+		GetObjectRequest request = new GetObjectRequest(bucketName, folderName + "/" + fileName);
+		GetObjectResult result = client.getObject(request);
+
+		Ks3Object object = result.getObject();
+		try {
+			return IOUtils.toByteArray(object.getObjectContent());
+		} finally {
+			if (object != null) {
+				object.close();
+			}
+		}
+	}
+
+	@Override
+	public void destroy() throws Exception {
+	}
+
+	public void setEndpoint(String endpoint) {
+		this.endpoint = endpoint;
+	}
+
+	public void setAccessKeyId(String accessKeyId) {
+		this.accessKeyId = accessKeyId;
+	}
+
+	public void setAccessKeySecret(String accessKeySecret) {
+		this.accessKeySecret = accessKeySecret;
+	}
+
+	public void setBucketName(String bucketName) {
+		this.bucketName = bucketName;
+	}
+
+	public static void main(String[] args) throws Exception {
+		KS3StoragePlugin aliyunOssStorageService = new KS3StoragePlugin();
+		aliyunOssStorageService.setAccessKeyId("LTAIwZW9XqrfsZ4r");
+		aliyunOssStorageService.setAccessKeySecret("5uDsNZmHMxcnxav8w9byII4zcPpu5G");
+		aliyunOssStorageService.setBucketName("yooma-test");
+		aliyunOssStorageService.setEndpoint("oss-cn-beijing.aliyuncs.com");
+		aliyunOssStorageService.afterPropertiesSet();
+
+		File file = new File("e:/var/2.jpg");
+		System.out.println(aliyunOssStorageService.uploadFile("aaa", file));
+
+		System.err.println("***********" + aliyunOssStorageService.getFile("aaa", file.getName()).length);
+
+		aliyunOssStorageService.destroy();
+	}
+}