Skip to content

Add transform foptimeout policy #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/com/qiniu/api/rs/PutPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ public class PutPolicy {

public long deadline;

/**
*
* 对文件先进行一次变换操作(比如将音频统一转为某种码率的mp3)再进行存储。
* transform的值就是一个fop指令,比如 "avthumb/mp3"。其含义是对上传的文件
* 执行这个 fop 指令,然后把结果保存到七牛。最后保存的是经过处理过的文件,
* 而不是用户上传的原始文件。
*
**/
public String transform;

/**
*
* 单位: 秒
* 文件变换操作执行的超时时间(单位:秒),上传和转码操作是同步进行的,
* 先上传后转码,这个时间只是转码所需时间,不包括上传文件所需时间。
* 这个值太小可能会导致误判(最终存储成功了但客户端得到超时的错误),
* 但太大可能会导致服务端将其判断为低优先级任务。建议取一个相对准确的
* 时间估计值*N(N不要超过5)。
*
**/
public long fopTimeout;

public PutPolicy(String scope) {
this.scope = scope;
}
Expand Down Expand Up @@ -91,6 +113,12 @@ public String marshal() throws JSONException {
if (this.persistentOps != null && this.persistentOps.length() > 0) {
stringer.key("persistentOps").value(this.persistentOps);
}
if (this.transform != null && this.transform.length() > 0) {
stringer.key("transform").value(this.transform);
}
if (this.fopTimeout > 0) {
stringer.key("fopTimeout").value(this.fopTimeout);
}
stringer.key("deadline").value(this.deadline);
stringer.endObject();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/qiniu/testing/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ public void tearDown() {
assertTrue(!sr.ok());
}
}
}
}
83 changes: 83 additions & 0 deletions src/test/java/com/qiniu/testing/TransformTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.qiniu.testing;

import java.io.*;

import junit.framework.TestCase;

import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.rs.Entry;
import com.qiniu.api.rs.PutPolicy;
import com.qiniu.api.rs.RSClient;

public class TransformTest extends TestCase {

// because all the testcase concurrently executes
// so the key should be different.
public final String key = "IOTest-key";

public final String expectedHash = "FivxSqsM1SyWCnYeIGPUqZM5LL4b";

public String bucketName;

public Mac mac;
@Override
public void setUp() {
Config.ACCESS_KEY = System.getenv("QINIU_ACCESS_KEY");
Config.SECRET_KEY = System.getenv("QINIU_SECRET_KEY");
Config.RS_HOST = System.getenv("QINIU_RS_HOST");
bucketName = System.getenv("QINIU_TEST_BUCKET");

assertNotNull(Config.ACCESS_KEY);
assertNotNull(Config.SECRET_KEY);
assertNotNull(Config.RS_HOST);
assertNotNull(bucketName);
mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
}

public void testPutTransform() throws Exception {
PutPolicy putPolicy = new PutPolicy(bucketName);
putPolicy.transform = "imageView/2/w/100/h/100";
putPolicy.fopTimeout = 10;

String uptoken = putPolicy.token(mac);
String dir = System.getProperty("user.dir");
String localFile = dir + "/testdata/" + "logo.png";

PutExtra extra = new PutExtra();

PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
assertTrue(ret.ok());
}

@Override
public void tearDown() {
// delete the metadata from rs
// confirms it exists.
{
RSClient rs = new RSClient(mac);
Entry sr = rs.stat(bucketName, key);
System.out.println(sr.getHash());
assertTrue(sr.ok());
assertTrue(expectedHash.equals(sr.getHash()));
}

// deletes it from rs
{
RSClient rs = new RSClient(mac);
CallRet cr = rs.delete(bucketName, key);
assertTrue(cr.ok());
}

// confirms that it's deleted
{
RSClient rs = new RSClient(mac);
Entry sr = rs.stat(bucketName, key);
assertTrue(!sr.ok());
}
}
}