com.aliyun.oss.model.MatchMode Java Examples

The following examples show how to use com.aliyun.oss.model.MatchMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AliossTemplate.java    From blade-tool with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * TODO 上传大小限制、基础路径
 * <p>
 * 获取上传凭证,普通上传
 */
public String getUploadToken(String bucketName, long expireTime) {
	String baseDir = "upload";

	long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
	Date expiration = new Date(expireEndTime);

	PolicyConditions policyConds = new PolicyConditions();
	// 默认大小限制10M
	policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, ossProperties.getArgs().get("contentLengthRange", 10485760));
	policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, baseDir);

	String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
	byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
	String encodedPolicy = BinaryUtil.toBase64String(binaryData);
	String postSignature = ossClient.calculatePostSignature(postPolicy);

	Map<String, String> respMap = new LinkedHashMap<>(16);
	respMap.put("accessid", ossProperties.getAccessKey());
	respMap.put("policy", encodedPolicy);
	respMap.put("signature", postSignature);
	respMap.put("dir", baseDir);
	respMap.put("host", getOssHost(bucketName));
	respMap.put("expire", String.valueOf(expireEndTime / 1000));
	return JsonUtil.toJson(respMap);
}
 
Example #2
Source File: OSSUtil.java    From anyline with Apache License 2.0 5 votes vote down vote up
public String policy(String dir,long second){ 
	String result = null; 
	PolicyConditions policyConds = new PolicyConditions(); 
	policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000); 
       policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir); 
    long expireEndTime = System.currentTimeMillis() + second * 1000; 
    Date expiration = new Date(expireEndTime); 
    result = client.generatePostPolicy(expiration, policyConds); 
	return result; 
}
 
Example #3
Source File: AliyunossProvider.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> createUploadToken(UploadTokenParam param) {
	
	Map<String, Object> result = new HashMap<>();
	
	PolicyConditions policyConds = new PolicyConditions();
	if(param.getFsizeMin() != null && param.getFsizeMax() != null){			
		policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, param.getFsizeMin(), param.getFsizeMax());
	}else{
		policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
	}
	if(param.getUploadDir() != null){			
		policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, param.getUploadDir());
	}
       
	if(StringUtils.isBlank(param.getCallbackHost())){
		param.setCallbackHost(host);
	}
	
	if(StringUtils.isBlank(param.getCallbackBody())){
		param.setCallbackBody(DEFAULT_CALLBACK_BODY);
	}
	
	Date expire = DateUtils.addSeconds(new Date(), (int)param.getExpires());
	String policy = ossClient.generatePostPolicy(expire, policyConds);
       String policyBase64 = null;
       String callbackBase64 = null;
       try {
       	policyBase64 = BinaryUtil.toBase64String(policy.getBytes(StandardCharsets.UTF_8.name()));
       	String callbackJson = param.getCallbackRuleAsJson();
       	if(callbackJson != null){
       		callbackBase64 = BinaryUtil.toBase64String(callbackJson.getBytes(StandardCharsets.UTF_8.name()));
       	}
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	String signature = ossClient.calculatePostSignature(policy);
	
	result.put("OSSAccessKeyId", accessKeyId);
	result.put("policy", policyBase64);
	result.put("signature", signature);
	result.put("host", this.urlprefix);
	result.put("dir", param.getUploadDir());
	result.put("expire", String.valueOf(expire.getTime()));
	if(callbackBase64 != null){
		result.put("callback", callbackBase64);
	}
	return result;
}