Skip to content

Commit 83b0a62

Browse files
committed
postObject policy supports In and Not-In.
1 parent 10727ab commit 83b0a62

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/main/java/com/aliyun/oss/model/MatchMode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@
2525
public enum MatchMode {
2626
Unknown, Exact, // Exact match
2727
StartWith, // Starts With
28-
Range // The range of file size
28+
Range, // The range of file size
29+
In,
30+
NotIn
2931
}

src/main/java/com/aliyun/oss/model/PolicyConditions.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.aliyun.oss.model;
2121

22+
import com.aliyun.oss.common.utils.StringUtils;
2223
import java.util.ArrayList;
2324
import java.util.HashMap;
2425
import java.util.List;
@@ -41,6 +42,7 @@ enum TupleType {
4142
private TupleType tupleType;
4243
private long minimum;
4344
private long maximum;
45+
private String[] contain;
4446

4547
public ConditionItem(String name, String value) {
4648
this.matchMode = MatchMode.Exact;
@@ -64,6 +66,13 @@ public ConditionItem(MatchMode matchMode, String name, String value) {
6466
this.tupleType = TupleType.Three;
6567
}
6668

69+
public ConditionItem(MatchMode matchMode, String name, String[] contain) {
70+
this.matchMode = matchMode;
71+
this.name = name;
72+
this.contain = contain;
73+
this.tupleType = TupleType.Three;
74+
}
75+
6776
public String jsonize() {
6877
String jsonizedCond = null;
6978
switch (tupleType) {
@@ -81,6 +90,12 @@ public String jsonize() {
8190
case Range:
8291
jsonizedCond = String.format("[\"content-length-range\",%d,%d],", minimum, maximum);
8392
break;
93+
case In:
94+
jsonizedCond = String.format("[\"in\",\"$%s\",[\"%s\"]],", name, StringUtils.join("\",\"",contain));
95+
break;
96+
case NotIn:
97+
jsonizedCond = String.format("[\"not-in\",\"$%s\",[\"%s\"]],", name, StringUtils.join("\",\"",contain));
98+
break;
8499
default:
85100
throw new IllegalArgumentException(String.format("Unsupported match mode %s", matchMode.toString()));
86101
}
@@ -162,6 +177,8 @@ public class PolicyConditions {
162177
List<MatchMode> ordinaryMatchModes = new ArrayList<MatchMode>();
163178
ordinaryMatchModes.add(MatchMode.Exact);
164179
ordinaryMatchModes.add(MatchMode.StartWith);
180+
ordinaryMatchModes.add(MatchMode.In);
181+
ordinaryMatchModes.add(MatchMode.NotIn);
165182
List<MatchMode> specialMatchModes = new ArrayList<MatchMode>();
166183
specialMatchModes.add(MatchMode.Range);
167184

@@ -224,6 +241,21 @@ public void addConditionItem(String name, long min, long max) {
224241
_conds.add(new ConditionItem(name, min, max));
225242
}
226243

244+
/**
245+
* Adds a condition item with specified {@link MatchMode} value.
246+
*
247+
* @param matchMode
248+
* Conditions match mode.
249+
* @param name
250+
* Condition name.
251+
* @param contain
252+
* Condition contain.
253+
*/
254+
public void addConditionItem(MatchMode matchMode, String name, String[] contain) {
255+
checkMatchModes(matchMode, name);
256+
_conds.add(new ConditionItem(matchMode, name.toLowerCase(), contain));
257+
}
258+
227259
private void checkMatchModes(MatchMode matchMode, String condName) {
228260
if (_supportedMatchRules.containsKey(condName)) {
229261
List<MatchMode> mms = _supportedMatchRules.get(condName);

src/test/java/com/aliyun/oss/integrationtests/PostPolicyTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,50 @@ public void testjsonizeError() {
107107
}
108108
}
109109

110+
111+
@Test
112+
public void testGenPostPolicyWithInAndNotIn() {
113+
final String bucketName = "gen-post-policy";
114+
OSSClient client = null;
115+
116+
try {
117+
client = new OSSClient(TestConfig.OSS_TEST_ENDPOINT, "AAAAAAAAAAAAAAAA", "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
118+
Date expiration = DateUtil.parseIso8601Date("2020-03-19T03:44:06.476Z");
119+
120+
PolicyConditions policyConds = new PolicyConditions();
121+
policyConds.addConditionItem("bucket", bucketName);
122+
policyConds.addConditionItem(MatchMode.Exact, PolicyConditions.COND_KEY, "user/eric/\\${filename}");
123+
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user/eric");
124+
policyConds.addConditionItem(MatchMode.StartWith, "x-oss-meta-tag", "dummy_etag");
125+
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1024);
126+
policyConds.addConditionItem(MatchMode.In, PolicyConditions.COND_CONTENT_TYPE, new String[]{"image/jpg","image/png"});
127+
policyConds.addConditionItem(MatchMode.NotIn, PolicyConditions.COND_CACHE_CONTROL, new String[]{"no-cache"});
128+
129+
String actualPostPolicy = client.generatePostPolicy(expiration, policyConds);
130+
String expectedPostPolicy = String.format("{\"expiration\":\"2020-03-19T03:44:06.476Z\",\"conditions\":[{\"bucket\":\"%s\"},"
131+
+ "[\"eq\",\"$key\",\"user/eric/\\${filename}\"],[\"starts-with\",\"$key\",\"user/eric\"],[\"starts-with\",\"$x-oss-meta-tag\","
132+
+ "\"dummy_etag\"],[\"content-length-range\",1,1024],[\"in\",\"$content-type\",[\"image/jpg\",\"image/png\"]],[\"not-in\",\"$cache-control\",[\"no-cache\"]]]}", bucketName);
133+
Assert.assertEquals(expectedPostPolicy, actualPostPolicy);
134+
135+
byte[] binaryData = actualPostPolicy.getBytes("utf-8");
136+
String actualEncodedPolicy = BinaryUtil.toBase64String(binaryData);
137+
String expectedEncodedPolicy = "eyJleHBpcmF0aW9uIjoiMjAyMC0wMy0xOVQwMzo0NDowNi40NzZaIiwiY29uZGl0aW9ucyI6W" +
138+
"3siYnVja2V0IjoiZ2VuLXBvc3QtcG9saWN5In0sWyJlcSIsIiRrZXkiLCJ1c2VyL2VyaWMvXCR7ZmlsZW5hbWV9Il0sWyJzd" +
139+
"GFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyL2VyaWMiXSxbInN0YXJ0cy13aXRoIiwiJHgtb3NzLW1ldGEtdGFnIiwiZHVtbXlfZ" +
140+
"XRhZyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDEsMTAyNF0sWyJpbiIsIiRjb250ZW50LXR5cGUiLFsiaW1hZ2UvanBnI" +
141+
"iwiaW1hZ2UvcG5nIl1dLFsibm90LWluIiwiJGNhY2hlLWNvbnRyb2wiLFsibm8tY2FjaGUiXV1dfQ==";
142+
Assert.assertEquals(expectedEncodedPolicy, actualEncodedPolicy);
143+
144+
String actualPostSignature = client.calculatePostSignature(actualPostPolicy);
145+
146+
Assert.assertTrue(actualPostSignature.equals("xyOp/TIhzYWH8e+kjJ4bkY60BH8="));
147+
} catch (Exception e) {
148+
Assert.fail(e.getMessage());
149+
} finally {
150+
if (client != null) {
151+
client.shutdown();
152+
}
153+
}
154+
}
155+
110156
}

0 commit comments

Comments
 (0)