完成服务器签名web直传并回调

This commit is contained in:
shuzheng 2017-05-15 18:27:15 +08:00
parent 515b4aee37
commit 49093ccee3
10 changed files with 198 additions and 26 deletions

View File

@ -1,6 +1,7 @@
package com.zheng.oss.common.constant;
import com.zheng.common.base.BaseConstants;
import com.zheng.common.util.PropertiesFileUtil;
/**
* oss系统常量类
@ -8,4 +9,16 @@ import com.zheng.common.base.BaseConstants;
*/
public class OssConstant extends BaseConstants {
// endpoint
public static final String ALIYUN_OSS_ENDPOINT = PropertiesFileUtil.getInstance("config").get("aliyun.oss.endpoint");
// bucketName
public static final String ALIYUN_OSS_BUCKET_NAME = PropertiesFileUtil.getInstance("config").get("aliyun.oss.bucketName");
// 文件大小
public static final int ALIYUN_OSS_MAX_SIZE = PropertiesFileUtil.getInstance("config").getInt("aliyun.oss.maxSize");
// 签名有效期(单位:分钟)
public static final int ALIYUN_OSS_EXPIRE = PropertiesFileUtil.getInstance("config").getInt("aliyun.oss.policy.expire");
}

View File

@ -6,9 +6,9 @@
<description>zheng-oss-sdk</description>
<context:property-placeholder location="classpath*:zheng-oss-client.properties"/>
<context:property-placeholder location="classpath*:config.properties"/>
<!-- 阿里云OSS客户端 -->
<!-- 阿里云OSS -->
<bean id="aliyunOssClient" class="com.aliyun.oss.OSSClient">
<constructor-arg value="${aliyun.oss.endpoint}"/>
<constructor-arg value="${aliyun.oss.accessKeyId}"/>

View File

@ -0,0 +1,64 @@
package com.zheng.oss.web.controller;
import com.alibaba.fastjson.JSONObject;
import com.zheng.oss.common.constant.OssResult;
import com.zheng.oss.common.constant.OssResultConstant;
import com.zheng.oss.web.service.AliyunOssService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* Created by ZhangShuzheng on 2017/5/15.
*/
@Controller
@RequestMapping("/aliyun/oss")
public class AliyunOssController {
private static Logger _log = LoggerFactory.getLogger(AliyunOssController.class);
@Autowired
private AliyunOssService aliyunOssService;
/**
* 签名生成
* @param callback 跨域请求
* @return
*/
@GetMapping("/policy")
@ResponseBody
//@CrossOrigin(origins = "*", methods = RequestMethod.GET) // 该注解不支持JDK1.7
public Object policy(@RequestParam(required = false) String callback) {
JSONObject result = aliyunOssService.policy();
if (StringUtils.isBlank(callback)) {
return result;
}
MappingJacksonValue jsonp = new MappingJacksonValue(result);
jsonp.setJsonpFunction(callback);
return jsonp;
}
/**
* 上传成功回调方法
* @param request
* @return
*/
@PostMapping("callback")
@ResponseBody
public Object callback(HttpServletRequest request) {
JSONObject data = new JSONObject();
data.put("filename", request.getParameter("filename"));
data.put("size", request.getParameter("size"));
data.put("mimeType", request.getParameter("mimeType"));
data.put("width", request.getParameter("width"));
data.put("height", request.getParameter("height"));
return new OssResult(OssResultConstant.SUCCESS, data);
}
}

View File

@ -2,13 +2,14 @@ package com.zheng.oss.web.controller;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;
import com.zheng.common.util.PropertiesFileUtil;
import com.zheng.oss.common.constant.OssConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@ -20,29 +21,28 @@ import java.io.*;
@RequestMapping("/demo")
public class DemoController {
private static Logger _log = LoggerFactory.getLogger(DemoController.class);
@Autowired
private OSSClient aliyunOssClient;
private String endPoint = PropertiesFileUtil.getInstance("zheng-oss-client").get("aliyun.oss.endpoint");
private String bucketName = PropertiesFileUtil.getInstance("zheng-oss-client").get("aliyun.oss.bucketName");
@GetMapping("/aliyun/upload1")
public String upload1() {
PutObjectResult putObjectResult = aliyunOssClient.putObject(bucketName, "text.txt", new ByteArrayInputStream("Hello OSS".getBytes()));
PutObjectResult putObjectResult = aliyunOssClient.putObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "text.txt", new ByteArrayInputStream("Hello OSS".getBytes()));
return "success";
}
@GetMapping("/aliyun/upload2")
public String upload2() throws FileNotFoundException {
File file = new File("C:\\Users\\shuzheng\\Documents\\zheng.png");
PutObjectResult putObjectResult = aliyunOssClient.putObject(bucketName, "file.png", file);
File file = new File("d:\\zheng.png");
PutObjectResult putObjectResult = aliyunOssClient.putObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "file.png", file);
return "success";
}
@GetMapping("/aliyun/download1")
public String download1() throws IOException {
StringBuffer result = new StringBuffer();
OSSObject ossObject = aliyunOssClient.getObject(bucketName, "text.txt");
OSSObject ossObject = aliyunOssClient.getObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "text.txt");
InputStream content = ossObject.getObjectContent();
if (content != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
@ -58,7 +58,7 @@ public class DemoController {
@GetMapping("/aliyun/download2")
public String download2() throws IOException {
return "http://" + bucketName + "." + endPoint + "/file.png";
return "http://" + OssConstant.ALIYUN_OSS_BUCKET_NAME + "." + OssConstant.ALIYUN_OSS_ENDPOINT + "/file.png";
}
}

View File

@ -0,0 +1,68 @@
package com.zheng.oss.web.service;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.zheng.oss.common.constant.OssConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by ZhangShuzheng on 2017/5/15.
*/
@Service
public class AliyunOssService {
private static Logger _log = LoggerFactory.getLogger(AliyunOssService.class);
@Autowired
private OSSClient aliyunOssClient;
/**
* 签名生成
* @return
*/
public JSONObject policy() {
JSONObject result = new JSONObject();
// 存储目录
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dir = sdf.format(new Date());
// 签名有效期
long expireEndTime = System.currentTimeMillis() + OssConstant.ALIYUN_OSS_EXPIRE * 1000;
Date expiration = new Date(expireEndTime);
// 文件大小
long maxSize = OssConstant.ALIYUN_OSS_MAX_SIZE * 1024 * 1024;
// 回调
JSONObject callback = new JSONObject();
callback.put("callbackUrl", "http://shuzheng.tunnel.qydev.com/aliyun/oss/callback");
callback.put("callbackBody", "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
callback.put("callbackBodyType", "application/x-www-form-urlencoded");
try {
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, maxSize);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = aliyunOssClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String policy = BinaryUtil.toBase64String(binaryData);
String signature = aliyunOssClient.calculatePostSignature(postPolicy);
String callbackData = BinaryUtil.toBase64String(callback.toString().getBytes("utf-8"));
// 返回结果
result.put("OSSAccessKeyId", aliyunOssClient.getCredentialsProvider().getCredentials().getAccessKeyId());
result.put("policy", policy);
result.put("signature", signature);
result.put("dir", dir);
result.put("callback", callbackData);
} catch (Exception e) {
_log.error("签名生成失败", e);
}
return result;
}
}

View File

@ -1 +1,10 @@
env=${profile.env}
env=${profile.env}
### aliyun oss ###
aliyun.oss.endpoint=${aliyun.oss.endpoint}
aliyun.oss.endpoint.internal=${aliyun.oss.endpoint.internal}
aliyun.oss.accessKeyId=${aliyun.oss.accessKeyId}
aliyun.oss.accessKeySecret=${aliyun.oss.accessKeySecret}
aliyun.oss.bucketName=${aliyun.oss.bucketName}
aliyun.oss.policy.expire=${aliyun.oss.policy.expire}
aliyun.oss.maxSize=${aliyun.oss.maxSize}

View File

@ -1,5 +1,10 @@
profile.env=dev
### aliyun ###
aliyun.oss.endpoint=http://shuzheng.oss-cn-shanghai.aliyuncs.com
aliyun.oss.endpoint.internal=http://shuzheng.oss-cn-shanghai-internal.aliyuncs.com
### aliyun oss ###
aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
aliyun.oss.accessKeyId=
aliyun.oss.accessKeySecret=
aliyun.oss.bucketName=shuzheng
aliyun.oss.policy.expire=30
aliyun.oss.maxSize=10

View File

@ -1 +1,10 @@
profile.env=prod
profile.env=prod
### aliyun oss ###
aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
aliyun.oss.accessKeyId=
aliyun.oss.accessKeySecret=
aliyun.oss.bucketName=shuzheng_prod
aliyun.oss.policy.expire=30
aliyun.oss.maxSize=10

View File

@ -1 +1,10 @@
profile.env=test
profile.env=test
### aliyun oss ###
aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
aliyun.oss.accessKeyId=
aliyun.oss.accessKeySecret=
aliyun.oss.bucketName=shuzheng_test
aliyun.oss.policy.expire=30
aliyun.oss.maxSize=10

View File

@ -1,5 +0,0 @@
### aliyun oss ###
aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
aliyun.oss.accessKeyId=LTAIf5dDIthJN3h0
aliyun.oss.accessKeySecret=2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d
aliyun.oss.bucketName=shuzheng