Skip to content

Commit 8780815

Browse files
committed
add policy auto
1 parent d051cf9 commit 8780815

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

src/main/java/com/qiniu/storage/model/Policy.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.qiniu.storage.model;
2+
3+
/**
4+
* Created by bailong on 16/9/21.
5+
*/
6+
7+
// 这只是个例子,可以自己创建policy 类使用
8+
public final class UploadPolicy {
9+
10+
public final String scope;
11+
public final long deadline;
12+
public int insertOnly;
13+
14+
public String endUser;
15+
16+
public String returnUrl;
17+
public String returnBody;
18+
19+
public String callbackUrl;
20+
public String callbackHost;
21+
public String callbackBody;
22+
public String callbackBodyType;
23+
public int callbackFetchKey;
24+
25+
public String persistentOps;
26+
public String persistentNotifyUrl;
27+
public String persistentPipeline;
28+
public String saveKey;
29+
30+
public long fsizeMin;
31+
public long fsizeLimit;
32+
33+
public int detectMime;
34+
35+
public String mimeLimit;
36+
37+
public int deleteAfterDays;
38+
39+
public UploadPolicy(String bucket, String key, long expired) {
40+
this.scope = key == null ? bucket : bucket + "" + key;
41+
this.deadline = System.currentTimeMillis() / 1000 + expired;
42+
}
43+
}

src/main/java/com/qiniu/util/Auth.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ public String uploadTokenWithDeadline(String bucket, String key, long deadline,
263263
return signWithData(StringUtils.utf8Bytes(s));
264264
}
265265

266+
public String uploadTokenWithPolicy(Object obj) {
267+
String s = Json.encode(obj);
268+
return signWithData(StringUtils.utf8Bytes(s));
269+
}
270+
266271
public StringMap authorization(String url, byte[] body, String contentType) {
267272
String authorization = "QBox " + signRequest(url, body, contentType);
268273
return new StringMap().put("Authorization", authorization);

src/main/java/com/qiniu/util/Json.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.qiniu.util;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
47
import com.google.gson.reflect.TypeToken;
58

69
import java.lang.reflect.Type;
@@ -15,6 +18,10 @@ public static String encode(StringMap map) {
1518
return new Gson().toJson(map.map());
1619
}
1720

21+
public static String encode(Object obj) {
22+
return new GsonBuilder().serializeNulls().create().toJson(obj);
23+
}
24+
1825
public static <T> T decode(String json, Class<T> classOfT) {
1926
return new Gson().fromJson(json, classOfT);
2027
}

src/test/java/com/qiniu/util/AuthTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.qiniu.TestConfig;
44
import com.qiniu.http.Client;
5+
import com.qiniu.storage.model.UploadPolicy;
56
import org.junit.Test;
67

78
import static org.junit.Assert.*;
@@ -56,4 +57,23 @@ public void testUploadToken() {
5657
// CHECKSTYLE:ON
5758
assertEquals(exp, token);
5859
}
60+
61+
static class Policy{
62+
String scope;
63+
long deadline;
64+
String endUser;
65+
}
66+
67+
@Test
68+
public void testUploadToken2() {
69+
Policy p = new Policy();
70+
p.endUser = "y";
71+
p.scope = "1:2";
72+
p.deadline = 1234567890L + 3600;
73+
String token = TestConfig.dummyAuth.uploadTokenWithPolicy(p);
74+
// CHECKSTYLE:OFF
75+
String exp = "abcdefghklmnopq:zx3NdMGffQ0JhUlgGSU5oeTx9Nk=:eyJzY29wZSI6IjE6MiIsImRlYWRsaW5lIjoxMjM0NTcxNDkwLCJlbmRVc2VyIjoieSJ9";
76+
// CHECKSTYLE:ON
77+
assertEquals(exp, token);
78+
}
5979
}

0 commit comments

Comments
 (0)