use of com.aliyun.oss.model.PolicyConditions in project jeesuite-libs by vakinge.
the class AliyunossProvider method createUploadToken.
// https://help.aliyun.com/document_detail/31926.html
// https://help.aliyun.com/document_detail/31989.html?spm=a2c4g.11186623.6.907.tlMQcL
@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;
}
use of com.aliyun.oss.model.PolicyConditions in project aliyun-oss-java-sdk by aliyun.
the class PostPolicyTest method testGenPostPolicy.
@Test
public void testGenPostPolicy() {
final String bucketName = "gen-post-policy";
try {
Date expiration = DateUtil.parseIso8601Date("2015-03-19T03:44:06.476Z");
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem("bucket", bucketName);
// $ must be escaped with backslash.
policyConds.addConditionItem(MatchMode.Exact, PolicyConditions.COND_KEY, "user/eric/\\${filename}");
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user/eric");
policyConds.addConditionItem(MatchMode.StartWith, "x-oss-meta-tag", "dummy_etag");
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1024);
String actualPostPolicy = ossClient.generatePostPolicy(expiration, policyConds);
String expectedPostPolicy = String.format("{\"expiration\":\"2015-03-19T03:44:06.476Z\",\"conditions\":[{\"bucket\":\"%s\"}," + "[\"eq\",\"$key\",\"user/eric/\\${filename}\"],[\"starts-with\",\"$key\",\"user/eric\"],[\"starts-with\",\"$x-oss-meta-tag\"," + "\"dummy_etag\"],[\"content-length-range\",1,1024]]}", bucketName);
Assert.assertEquals(expectedPostPolicy, actualPostPolicy);
byte[] binaryData = actualPostPolicy.getBytes("utf-8");
String actualEncodedPolicy = BinaryUtil.toBase64String(binaryData);
String expectedEncodedPolicy = "eyJleHBpcmF0aW9uIjoiMjAxNS0wMy0xOVQwMzo0NDowNi40Nz" + "ZaIiwiY29uZGl0aW9ucyI6W3siYnVja2V0IjoiZ2VuLXBvc3QtcG9saWN5In0sWyJlcSIsIiRrZXkiLC" + "J1c2VyL2VyaWMvXCR7ZmlsZW5hbWV9Il0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyL2Vya" + "WMiXSxbInN0YXJ0cy13aXRoIiwiJHgtb3NzLW1ldGEtdGFnIiwiZHVtbXlfZXRhZyJdLFsiY29udG" + "VudC1sZW5ndGgtcmFuZ2UiLDEsMTAyNF1dfQ==";
Assert.assertEquals(expectedEncodedPolicy, actualEncodedPolicy);
String actualPostSignature = ossClient.calculatePostSignature(actualPostPolicy);
// It has something to do with the local time
Assert.assertTrue((actualPostSignature.equals("88kD3wGu1W5isVAdWSG765DRPKY=") || actualPostSignature.equals("KbUYorFeyyqxntffsNlrRcV50Ds=")));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.PolicyConditions in project aliyun-oss-java-sdk by aliyun.
the class PostPolicyTest method testGenPostPolicy.
@Ignore
public void testGenPostPolicy() {
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
try {
Date expiration = DateUtil.parseIso8601Date("2015-03-19T03:44:06.476Z");
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem("bucket", bucketName);
// $ must be escaped with backslash.
policyConds.addConditionItem(MatchMode.Exact, PolicyConditions.COND_KEY, "user/eric/\\${filename}");
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user/eric");
policyConds.addConditionItem(MatchMode.StartWith, "x-oss-meta-tag", "dummy_etag");
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1024);
String actualPostPolicy = client.generatePostPolicy(expiration, policyConds);
String expectedPostPolicy = "{\"expiration\":\"2015-03-19T03:44:06.476Z\",\"conditions\":[{\"bucket\":\"oss-test2\"},[\"eq\",\"$key\",\"user/eric/\\${filename}\"],[\"starts-with\",\"$key\",\"user/eric\"],[\"starts-with\",\"$x-oss-meta-tag\",\"dummy_etag\"],[\"content-length-range\",1,1024]]}";
Assert.assertEquals(expectedPostPolicy, actualPostPolicy);
byte[] binaryData = actualPostPolicy.getBytes("utf-8");
String actualEncodedPolicy = BinaryUtil.toBase64String(binaryData);
String expectedEncodedPolicy = "eyJleHBpcmF0aW9uIjoiMjAxNS0wMy0xOVQwMzo0NDowNi40NzZaIiwiY29uZGl0aW9ucyI6W3siYnVja2V0Ijoib3NzLXRlc3QyIn0sWyJlcSIsIiRrZXkiLCJ1c2VyL2VyaWMvXCR7ZmlsZW5hbWV9Il0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyL2VyaWMiXSxbInN0YXJ0cy13aXRoIiwiJHgtb3NzLW1ldGEtdGFnIiwiZHVtbXlfZXRhZyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDEsMTAyNF1dfQ==";
Assert.assertEquals(expectedEncodedPolicy, actualEncodedPolicy);
String actualPostSignature = client.calculatePostSignature(actualPostPolicy);
String expectedPostSignature = "+fOC13qQyIUqF+T/mSA/So2qEBw=";
Assert.assertEquals(expectedPostSignature, actualPostSignature);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations