|
@@ -0,0 +1,123 @@
|
|
|
+package com.ym.mec.thirdpart.storage.vendors;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import org.apache.commons.io.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.aliyun.oss.ClientConfiguration;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
+import com.ym.mec.thirdpart.exception.ThirdpartException;
|
|
|
+import com.ym.mec.thirdpart.storage.StoragePlugin;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
|
|
|
+
|
|
|
+ @Value("${storage.oss.endpoint:oss-cn-beijing.aliyuncs.com}")
|
|
|
+ private String endpoint;
|
|
|
+
|
|
|
+ @Value("${storage.oss.accessKeyId:LTAIwZW9XqrfsZ4r}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${storage.oss.accessKeySecret:5uDsNZmHMxcnxav8w9byII4zcPpu5G}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${storage.oss.bucketName:yooma-test}")
|
|
|
+ private String bucketName;
|
|
|
+
|
|
|
+ private OSSClient ossClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+
|
|
|
+ ClientConfiguration conf = new ClientConfiguration();
|
|
|
+
|
|
|
+
|
|
|
+ conf.setMaxConnections(200);
|
|
|
+
|
|
|
+ conf.setSocketTimeout(10000);
|
|
|
+
|
|
|
+ conf.setConnectionTimeout(10000);
|
|
|
+
|
|
|
+ conf.setConnectionRequestTimeout(1000);
|
|
|
+
|
|
|
+ conf.setIdleConnectionTime(10000);
|
|
|
+
|
|
|
+ conf.setMaxErrorRetry(5);
|
|
|
+
|
|
|
+ conf.setSupportCname(true);
|
|
|
+
|
|
|
+ ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadFile(String folderName, File file) {
|
|
|
+ if (!file.exists()) {
|
|
|
+ throw new ThirdpartException("需要上传的文件[{}]不存在", file.getAbsolutePath());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (folderName.endsWith("/")) {
|
|
|
+ folderName = folderName.substring(0, folderName.length() - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ ossClient.putObject(bucketName, folderName + "/" + file.getName(), file);
|
|
|
+
|
|
|
+ return "https://" + bucketName + "." + endpoint + "/" + folderName + "/" + file.getName();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] getFile(String folderName, String fileName) throws IOException {
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, folderName + "/" + fileName);
|
|
|
+ try {
|
|
|
+ return IOUtils.toByteArray(ossObject.getObjectContent());
|
|
|
+ } finally {
|
|
|
+ if (ossObject != null) {
|
|
|
+ ossObject.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void destroy() throws Exception {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ AliyunOssStoragePlugin aliyunOssStorageService = new AliyunOssStoragePlugin();
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+}
|