Skip to content

Commit 2857c4a

Browse files
committed
Add transform and fopTimeout Put Policy
1 parent b320af3 commit 2857c4a

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/main/java/com/qiniu/api/rs/PutPolicy.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ public class PutPolicy {
4747

4848
public long deadline;
4949

50+
/**
51+
*
52+
* 对文件先进行一次变换操作(比如将音频统一转为某种码率的mp3)再进行存储。
53+
* transform的值就是一个fop指令,比如 "avthumb/mp3"。其含义是对上传的文件
54+
* 执行这个 fop 指令,然后把结果保存到七牛。最后保存的是经过处理过的文件,
55+
* 而不是用户上传的原始文件。
56+
**/
57+
public String transform;
58+
59+
/**
60+
*
61+
* 单位: 秒
62+
* 文件变换操作执行的超时时间(单位:秒),上传和转码操作是同步进行的,
63+
* 先上传后转码,这个时间只是转码所需时间,不包括上传文件所需时间。
64+
* 这个值太小可能会导致误判(最终存储成功了但客户端得到超时的错误),
65+
* 但太大可能会导致服务端将其判断为低优先级任务。建议取一个相对准确的
66+
* 时间估计值*N(N不要超过5)。
67+
*
68+
**/
69+
public long fopTimeout;
70+
5071
public PutPolicy(String scope) {
5172
this.scope = scope;
5273
}
@@ -91,6 +112,12 @@ public String marshal() throws JSONException {
91112
if (this.persistentOps != null && this.persistentOps.length() > 0) {
92113
stringer.key("persistentOps").value(this.persistentOps);
93114
}
115+
if (this.transform != null && this.transform.length() > 0) {
116+
stringer.key("transform").value(this.transform);
117+
}
118+
if (this.fopTimeout > 0) {
119+
stringer.key("fopTimeout").value(this.fopTimeout);
120+
}
94121
stringer.key("deadline").value(this.deadline);
95122
stringer.endObject();
96123

src/test/java/com/qiniu/testing/IOTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public void testPut() throws Exception {
6464
}
6565
}
6666

67+
/**
68+
* 这个案例测试上传后转码并将转码结果保存为目标文件。例中使用了 PutPolicy 中的
69+
* transform 和 fopTimeout 两个参数,分别对应要进行的转码操作和转码超时时间,
70+
* 测试的时候请将例中的 localFile 改成合法的文件路径。
71+
**/
72+
public void testPutTransform() throws Exception {
73+
PutPolicy putPolicy = new PutPolicy(bucketName);
74+
putPolicy.transform = "avthumb/mp3";
75+
putPolicy.fopTimeout = 60;
76+
77+
String uptoken = putPolicy.token(mac);
78+
String localFile = "/Users/ikbear/7niu/music.mp4";
79+
String key = "music-ikbear.mp3";
80+
81+
PutExtra extra = new PutExtra();
82+
83+
PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
84+
assertTrue(ret.ok());
85+
}
86+
6787
@Override
6888
public void tearDown() {
6989
// delete the metadata from rs
@@ -89,4 +109,4 @@ public void tearDown() {
89109
assertTrue(!sr.ok());
90110
}
91111
}
92-
}
112+
}

0 commit comments

Comments
 (0)