Просмотр исходного кода

[chg] OSSClientBuilder抽成单例

wcz 5 лет назад
Родитель
Сommit
63121a2b08

+ 4 - 42
lift-common/src/main/java/cn.com.ty.lift.common/aliservice/aliyunoss/AliyunOSS.java

@@ -36,13 +36,11 @@ public class AliyunOSS {
     private static class Builder{
         private static OSSClientBuilder ossClientBuilder = new OSSClientBuilder();
     }
-
     /**
      * 无参构造,初始化oss
      */
     private AliyunOSS() {
-        hint("Default");
-        init();
+        init("Default");
     }
     /**
      * 全参构造方法
@@ -57,18 +55,14 @@ public class AliyunOSS {
         this.accessKeyId = accessKeyId;
         this.accessKeySecret = accessKeySecret;
         this.bucketName = bucketName;
-        hint("Custom");
-        init();
-    }
-
-    private void hint(String type){
-        log.info("Hint: Using the {} Configuration To Create Aliyun OSS Client.",type);
+        init("Custom");
     }
 
     /**
      * 初始化,创建OSSClient实例
      */
-    private void init() {
+    private void init(String type) {
+        log.info("Hint: Using the {} Configuration To Create Aliyun OSS Client.",type);
         if (null == ossClient) {
             ossClient = Builder.ossClientBuilder.build(endpoint, accessKeyId, accessKeySecret);
         }
@@ -155,38 +149,6 @@ public class AliyunOSS {
             destroy();
         }
     }
-
-    /**
-     * 批量上传文件到oss,返回 key:objectName ,value:url
-     *
-     * @param bucketName
-     * @param ossEntities
-     * @return
-     */
-    public Map<String, String> putObjects(String bucketName, List<OssEntity> ossEntities) {
-        try {
-            if (IterUtil.isEmpty(ossEntities)) {
-                return null;
-            }
-            Map<String, String> urls = new HashMap<>();
-            ossEntities.forEach(entity -> {
-                // 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
-                String objectName = entity.getObjectName();
-                ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(entity.getContent()));
-                String url = getObjectUrl(bucketName, objectName);
-                log.info("objectName: {}, url: {}", objectName, url);
-                urls.put(objectName, url);
-            });
-            return urls;
-        } catch (Exception e) {
-            log.error("上传文件异常: {}", e);
-            return null;
-        } finally {
-            destroy();
-        }
-    }
-
-
     public String putObject(String objectName, byte[] content) {
         if (StrUtil.isEmpty(objectName)) {
             return null;

+ 0 - 23
lift-common/src/main/java/cn.com.ty.lift.common/aliservice/aliyunoss/OssEntity.java

@@ -1,23 +0,0 @@
-package cn.com.ty.lift.common.aliservice.aliyunoss;
-
-import lombok.Data;
-
-/**
- * <p>
- *     封装上传数据
- * </p>
- * @author wcz
- * @since  2020/1/14 17:17
- */
-@Data
-public class OssEntity {
-
-    /**
-     * 对象名
-     */
-    private String objectName;
-    /**
-     * 对象数据字节数组
-     */
-    private byte[] content;
-}