Prechádzať zdrojové kódy

oss相关代码改写

macro 5 rokov pred
rodič
commit
2a520725de

+ 6 - 0
mall-admin/src/main/java/com/macro/mall/dto/OssCallbackParam.java

@@ -1,11 +1,17 @@
 package com.macro.mall.dto;
 
+import io.swagger.annotations.ApiModelProperty;
+
 /**
+ * oss上传成功后的回调参数
  * Created by macro on 2018/5/17.
  */
 public class OssCallbackParam {
+    @ApiModelProperty("请求的回调地址")
     private String callbackUrl;
+    @ApiModelProperty("回调是传入request中的参数")
     private String callbackBody;
+    @ApiModelProperty("回调时传入参数的格式,比如表单提交形式")
     private String callbackBodyType;
 
     public String getCallbackUrl() {

+ 8 - 0
mall-admin/src/main/java/com/macro/mall/dto/OssCallbackResult.java

@@ -1,13 +1,21 @@
 package com.macro.mall.dto;
 
+import io.swagger.annotations.ApiModelProperty;
+
 /**
+ * oss上传文件的回调结果
  * Created by macro on 2018/5/17.
  */
 public class OssCallbackResult {
+    @ApiModelProperty("文件名称")
     private String filename;
+    @ApiModelProperty("文件大小")
     private String size;
+    @ApiModelProperty("文件的mimeType")
     private String mimeType;
+    @ApiModelProperty("图片文件的宽")
     private String width;
+    @ApiModelProperty("图片文件的高")
     private String height;
 
     public String getFilename() {

+ 18 - 1
mall-admin/src/main/java/com/macro/mall/dto/OssPolicyResult.java

@@ -1,15 +1,24 @@
 package com.macro.mall.dto;
 
+import io.swagger.annotations.ApiModelProperty;
+
 /**
- * 获取OSS上传授权返回结果
+ * 获取OSS上传文件授权返回结果
  * Created by macro on 2018/5/17.
  */
 public class OssPolicyResult {
+    @ApiModelProperty("访问身份验证中用到用户标识")
     private String accessKeyId;
+    @ApiModelProperty("用户表单上传的策略,经过base64编码过的字符串")
     private String policy;
+    @ApiModelProperty("对policy签名后的字符串")
     private String signature;
+    @ApiModelProperty("上传文件夹路径前缀")
     private String dir;
+    @ApiModelProperty("oss对外服务的访问域名")
     private String host;
+    @ApiModelProperty("上传成功后的回调设置")
+    private String callback;
 
     public String getAccessKeyId() {
         return accessKeyId;
@@ -50,4 +59,12 @@ public class OssPolicyResult {
     public void setHost(String host) {
         this.host = host;
     }
+
+    public String getCallback() {
+        return callback;
+    }
+
+    public void setCallback(String callback) {
+        this.callback = callback;
+    }
 }

+ 6 - 0
mall-admin/src/main/java/com/macro/mall/service/OssService.java

@@ -10,6 +10,12 @@ import javax.servlet.http.HttpServletRequest;
  * Created by macro on 2018/5/17.
  */
 public interface OssService {
+    /**
+     * oss上传策略生成
+     */
     OssPolicyResult policy();
+    /**
+     * oss上传成功回调
+     */
     OssCallbackResult callback(HttpServletRequest request);
 }

+ 7 - 6
mall-admin/src/main/java/com/macro/mall/service/impl/OssServiceImpl.java

@@ -20,6 +20,7 @@ import java.text.SimpleDateFormat;
 import java.util.Date;
 
 /**
+ * oss上传管理Service实现类
  * Created by macro on 2018/5/17.
  */
 @Service
@@ -57,10 +58,10 @@ public class OssServiceImpl implements OssService {
 		// 文件大小
 		long maxSize = ALIYUN_OSS_MAX_SIZE * 1024 * 1024;
 		// 回调
-//		OssCallbackParam callback = new OssCallbackParam();
-//		callback.setCallbackUrl(ALIYUN_OSS_CALLBACK);
-//		callback.setCallbackBody("filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
-//		callback.setCallbackBodyType("application/x-www-form-urlencoded");
+		OssCallbackParam callback = new OssCallbackParam();
+		callback.setCallbackUrl(ALIYUN_OSS_CALLBACK);
+		callback.setCallbackBody("filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
+		callback.setCallbackBodyType("application/x-www-form-urlencoded");
 		// 提交节点
 		String action = "http://" + ALIYUN_OSS_BUCKET_NAME + "." + ALIYUN_OSS_ENDPOINT;
 		try {
@@ -71,13 +72,13 @@ public class OssServiceImpl implements OssService {
 			byte[] binaryData = postPolicy.getBytes("utf-8");
 			String policy = BinaryUtil.toBase64String(binaryData);
 			String signature = ossClient.calculatePostSignature(postPolicy);
-//			String callbackData = BinaryUtil.toBase64String(JSONUtil.parse(callback).toString().getBytes("utf-8"));
+			String callbackData = BinaryUtil.toBase64String(JSONUtil.parse(callback).toString().getBytes("utf-8"));
 			// 返回结果
 			result.setAccessKeyId(ossClient.getCredentialsProvider().getCredentials().getAccessKeyId());
 			result.setPolicy(policy);
 			result.setSignature(signature);
 			result.setDir(dir);
-//			result.setCallback(callbackData);
+			result.setCallback(callbackData);
 			result.setHost(action);
 		} catch (Exception e) {
 			LOGGER.error("签名生成失败", e);

+ 8 - 8
mall-admin/src/main/resources/application.yml

@@ -15,16 +15,16 @@ jwt:
 
 aliyun:
   oss:
-    endpoint: oss-cn-shenzhen.aliyuncs.com
-    accessKeyId: test
-    accessKeySecret: test
-    bucketName: macro-oss
+    endpoint: oss-cn-shenzhen.aliyuncs.com # oss对外服务的访问域名
+    accessKeyId: test # 访问身份验证中用到用户标识
+    accessKeySecret: test # 用户用于加密签名字符串和oss用来验证签名字符串的密钥
+    bucketName: macro-oss # oss的存储空间
     policy:
-      expire: 300
-    maxSize: 10
-    callback: http://localhost:8080/aliyun/oss/callback
+      expire: 300 # 签名有效期(S)
+    maxSize: 10 # 上传文件大小(M)
+    callback: http://39.98.190.128:8080/aliyun/oss/callback # 文件上传成功后的回调地址
     dir:
-      prefix: mall/images/
+      prefix: mall/images/ # 上传文件夹路径前缀
 
 logging:
   level: