Skip to content

Commit f5bb7e3

Browse files
committed
Merge pull request #82 from qiniu/develop
add warning of utf8 keys
2 parents 69434d7 + 6b43af2 commit f5bb7e3

File tree

11 files changed

+144
-49
lines changed

11 files changed

+144
-49
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
## CHANGE LOG
22

3+
### v6.0.5
4+
5+
2013-10-08 issue [#82](https://github.com/qiniu/java-sdk/pull/82)
6+
7+
- 增加私有资源fop的接口,包括exif,imageInfo,ImageView
8+
39
### v6.0.4
410

511
2013-09-02 issue [#78](https://github.com/qiniu/java-sdk/pull/78)
612

7-
hot fix,增加EndUser字段至PutPolicy的JSON字符串中
13+
- 添加ListPrefix
14+
- hot fix,增加EndUser字段至PutPolicy的JSON字符串中
815

916
### v6.0.3
1017

1118
2013-08-5 issue [#76](https://github.com/qiniu/java-sdk/pull/76)
1219

13-
Bug fix,编码强制UTF-8修复
20+
- Bug fix,编码强制UTF-8修复
1421

1522
### v6.0.1
1623

1724
2013-08-5 issue [#74](https://github.com/qiniu/java-sdk/pull/74)
1825

19-
Bug fix,增加PutPolicy类的 callbackBody字段到PutPolicy的Json格式中
26+
- Bug fix,增加PutPolicy类的 callbackBody字段到PutPolicy的Json格式中
2027

2128

2229
### v6.0.0

docs/README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ public class UploadFile {
197197
String uptoken = putPolicy.token(mac);
198198
PutExtra extra = new PutExtra();
199199
String key = "<key>";
200-
File file = new File("<your local file path>");
201-
PutRet ret = IoApi.put(uptoken, key, file, extra);
200+
String localFile = "<local file path>";
201+
PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
202202
}
203203
}
204204
@@ -217,7 +217,7 @@ public class UploadFile {
217217
[uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 实际上是用 AccessKey/SecretKey 进行数字签名的上传策略(`rs.PutPolicy`),它控制则整个上传流程的行为。让我们快速过一遍你都能够决策啥:
218218

219219
* `expires` 指定 [uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 有效期(默认1小时)。一个 [uptoken](http://docs.qiniu.com/api/put.html#uploadToken) 可以被用于多次上传(只要它还没有过期)。
220-
* `scope` 限定客户端的权限。如果 `scope` 是 bucket,则客户端只能新增文件到指定的 bucket,不能修改文件。如果 `scope` 为 bucket:key,则客户端可以修改指定的文件。
220+
* `scope` 限定客户端的权限。如果 `scope` 是 bucket,则客户端只能新增文件到指定的 bucket,不能修改文件。如果 `scope` 为 bucket:key,则客户端可以修改指定的文件。**注意: key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误**
221221
* `callbackUrl` 设定业务服务器的回调地址,这样业务服务器才能感知到上传行为的发生。可选。
222222
* `asyncOps` 可指定上传完成后,需要自动执行哪些数据处理。这是因为有些数据处理操作(比如音视频转码)比较慢,如果不进行预转可能第一次访问的时候效果不理想,预转可以很大程度改善这一点。
223223
* `returnBody` 可调整返回给客户端的数据包(默认情况下七牛返回文件内容的 `hash`,也就是下载该文件时的 `etag`)。这只在没有 `CallbackUrl` 时有效。
@@ -242,7 +242,9 @@ public class UploadFile {
242242

243243
[GET] http://<domain>/<key>
244244

245-
其中<domain>可以到[七牛云存储开发者自助网站](https://portal.qiniu.com)绑定, 域名可以使用自己一级域名的或者是由七牛提供的二级域名(`<bucket>.qiniudn.com`)。注意,尖括号不是必需,代表替换项。
245+
其中\<domain\>是bucket所对应的域名。七牛云存储为每一个bucket提供一个默认域名。默认域名可以到[七牛云存储开发者平台](https://portal.qiniu.com/)中,空间设置的域名设置一节查询。用户也可以将自有的域名绑定到bucket上,通过自有域名访问七牛云存储。
246+
247+
**注意: key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误**
246248

247249
<a name="private-download"></a>
248250

@@ -674,8 +676,24 @@ public class ListPrefix {
674676
Config.ACCESS_KEY = "<YOUR APP ACCESS_KEY>";
675677
Config.SECRET_KEY = "<YOUR APP SECRET_KEY>";
676678
Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
679+
677680
RSFClient client = new RSFClient(mac);
678-
client.listPrifix("<bucketName>", "<prefix>", "<marker>", 10);
681+
String marker = "";
682+
683+
List<ListItem> all = new ArrayList<ListItem>();
684+
ListPrefixRet ret = null;
685+
while (true) {
686+
ret = client.listPrifix(bucketName, "<prifix>", marker, 10);
687+
marker = ret.marker;
688+
all.addAll(ret.results);
689+
if (!ret.ok()) {
690+
// no more items or error occurs
691+
break;
692+
}
693+
}
694+
if (ret.exception.getClass() != RSFEofException.class) {
695+
// error handler
696+
}
679697
}
680698
}
681699
```

src/main/java/com/qiniu/api/fop/ImageExif.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import com.qiniu.api.net.CallRet;
44
import com.qiniu.api.net.Client;
5-
6-
5+
import com.qiniu.api.auth.AuthException;
6+
import com.qiniu.api.auth.digest.*;
7+
import com.qiniu.api.rs.*;
78
public class ImageExif {
89

910
/**
@@ -22,5 +23,11 @@ public static ExifRet call(String url) {
2223
return new ExifRet(ret);
2324
}
2425

25-
26+
public static ExifRet call(String url,Mac mac) throws AuthException {
27+
String pubUrl = makeRequest(url);
28+
GetPolicy policy =new GetPolicy();
29+
String priUrl = policy.makeRequest(pubUrl, mac);
30+
CallRet ret = new Client().call(priUrl);
31+
return new ExifRet(ret);
32+
}
2633
}

src/main/java/com/qiniu/api/fop/ImageInfo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.qiniu.api.fop;
22

3+
34
import com.qiniu.api.net.CallRet;
45
import com.qiniu.api.net.Client;
5-
6+
import com.qiniu.api.auth.AuthException;
7+
import com.qiniu.api.auth.digest.*;
8+
import com.qiniu.api.rs.*;
69
public class ImageInfo {
710

811
/**
@@ -20,4 +23,12 @@ public static ImageInfoRet call(String url) {
2023
CallRet ret = new Client().call(makeRequest(url));
2124
return new ImageInfoRet(ret);
2225
}
26+
27+
public static ImageInfoRet call(String url,Mac mac) throws AuthException {
28+
String pubUrl = makeRequest(url);
29+
GetPolicy policy =new GetPolicy();
30+
String priUrl = policy.makeRequest(pubUrl, mac);
31+
CallRet ret = new Client().call(priUrl);
32+
return new ImageInfoRet(ret);
33+
}
2334
}

src/main/java/com/qiniu/api/fop/ImageView.java

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

33
import com.qiniu.api.net.CallRet;
44
import com.qiniu.api.net.Client;
5+
import com.qiniu.api.rs.GetPolicy;
6+
import com.qiniu.api.auth.AuthException;
7+
import com.qiniu.api.auth.digest.*;
8+
import com.qiniu.api.rs.*;
59

610
public class ImageView {
711
/**
@@ -81,4 +85,12 @@ public CallRet call(String url) {
8185
CallRet ret = new Client().call(this.makeRequest(url));
8286
return ret;
8387
}
88+
89+
public CallRet call(String url,Mac mac) throws AuthException {
90+
String pubUrl = makeRequest(url);
91+
GetPolicy policy =new GetPolicy();
92+
String priUrl = policy.makeRequest(pubUrl, mac);
93+
CallRet ret = new Client().call(priUrl);
94+
return ret;
95+
}
8496
}

src/main/java/com/qiniu/api/rsf/ListItem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ private void unmarshal(JSONObject jsonObject) throws JSONException {
3939
this.endUser = jsonObject.getString("endUser");
4040
}
4141
}
42+
43+
@Override
44+
public String toString() {
45+
StringBuilder sbuf = new StringBuilder();
46+
sbuf.append("key:").append(this.key);
47+
sbuf.append(" hash:").append(this.hash);
48+
sbuf.append(" fsize:").append(this.fsize);
49+
sbuf.append(" putTime:").append(this.putTime);
50+
sbuf.append(" mimeType:").append(this.mimeType);
51+
sbuf.append(" endUser:").append(this.endUser);
52+
return sbuf.toString();
53+
}
4254
}

src/main/java/com/qiniu/api/rsf/RSFClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ListPrefixRet listPrifix(String bucketName, String prefix, String marker,
1919
if (marker != null && marker.length() != 0) {
2020
params.append("&marker=").append(marker);
2121
}
22-
if (prefix != null && marker.length() != 0) {
22+
if (prefix != null && prefix.length() != 0) {
2323
params.append("&prefix=").append(prefix);
2424
}
2525
if (limit > 0) {
@@ -28,7 +28,11 @@ public ListPrefixRet listPrifix(String bucketName, String prefix, String marker,
2828

2929
String url = Config.RSF_HOST + "/list?" + params.toString();
3030
CallRet ret = conn.call(url);
31-
return new ListPrefixRet(ret);
31+
ListPrefixRet listRet = new ListPrefixRet(ret);
32+
if (listRet.marker == null || "".equals(listRet.marker)) {
33+
listRet.exception = new RSFEofException("EOF");
34+
}
35+
return listRet;
3236
}
3337

3438
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.qiniu.api.rsf;
2+
3+
public class RSFEofException extends Exception {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
protected RSFEofException() {
8+
super();
9+
}
10+
11+
public RSFEofException(String detailMessage) {
12+
super(detailMessage);
13+
}
14+
15+
public RSFEofException(String detailMessage, Throwable throwable) {
16+
super(detailMessage, throwable);
17+
}
18+
19+
public RSFEofException(Throwable throwable) {
20+
super(throwable);
21+
}
22+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static Test suite() {
2121
suite.addTestSuite(BatchCopyTest.class);
2222
suite.addTestSuite(BatchMoveTest.class);
2323

24+
suite.addTestSuite(RSFTest.class);
2425
return suite;
2526
}
2627

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void testImageExif() throws Exception {
8282
}
8383

8484
public void testImageView() throws Exception {
85-
String url = domain + "/" + key;
85+
String url = "http://qiniuphotos.qiniudn.com/gogopher.jpg";
8686
{
8787
ImageView iv = new ImageView();
8888
iv.mode = 1;
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.qiniu.testing;
22

33
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import junit.framework.TestCase;
68

@@ -9,18 +11,17 @@
911
import com.qiniu.api.io.IoApi;
1012
import com.qiniu.api.io.PutExtra;
1113
import com.qiniu.api.io.PutRet;
12-
import com.qiniu.api.net.CallRet;
13-
import com.qiniu.api.rs.Entry;
1414
import com.qiniu.api.rs.PutPolicy;
15-
import com.qiniu.api.rs.RSClient;
15+
import com.qiniu.api.rsf.ListItem;
1616
import com.qiniu.api.rsf.ListPrefixRet;
1717
import com.qiniu.api.rsf.RSFClient;
18+
import com.qiniu.api.rsf.RSFEofException;
1819

1920
public class RSFTest extends TestCase {
2021

2122
// because all the testcase concurrently executes
2223
// so the key should be different.
23-
public final String key = "RSFTest-key";
24+
public final String key = "_javasdk_RSFTest-key";
2425

2526
public final String expectedHash = "FmDZwqadA4-ib_15hYfQpb7UXUYR";
2627

@@ -50,45 +51,45 @@ public void testRSF() throws Exception {
5051

5152
PutExtra extra = new PutExtra();
5253

53-
PutRet ret = IoApi
54-
.putFile(uptoken, key, localFile, extra);
55-
56-
assertTrue(ret.ok());
57-
assertTrue(expectedHash.equals(ret.getHash()));
58-
54+
// upload 3 files
55+
for (int i = 0; i < 3; i++) {
56+
PutRet ret = IoApi.putFile(uptoken, key + "_" + i,localFile, extra);
57+
assertTrue(ret.ok());
58+
assertTrue(expectedHash.equals(ret.getHash()));
59+
}
60+
5961
}
6062
// we don't checkout the result of how may items are in the buckets.
6163
// not very convient, it's better, although.
6264
{
6365
RSFClient client = new RSFClient(mac);
64-
ListPrefixRet ret = client.listPrifix(bucketName, "", "", 100);
65-
assertTrue(ret.ok());
66+
String marker = "";
67+
68+
List<ListItem> all = new ArrayList<ListItem>();
69+
ListPrefixRet ret = null;
70+
while (true) {
71+
ret = client.listPrifix(bucketName, "_javasdk", marker, 10);
72+
marker = ret.marker;
73+
all.addAll(ret.results);
74+
if (!ret.ok()) {
75+
// no more items or error occurs
76+
break;
77+
}
78+
}
79+
if (ret.exception.getClass() != RSFEofException.class) {
80+
// error handler
81+
System.out.println(ret.exception);
82+
}
83+
84+
// 防止该bucket有别人在用
85+
System.out.println(ret.exception);
86+
System.out.println(all.size());
87+
assertTrue(all.size() >= 3);
6688
}
6789
}
6890

6991
@Override
7092
public void tearDown() {
71-
// delete the metadata from rs
72-
// confirms it exists.
73-
{
74-
RSClient rs = new RSClient(mac);
75-
Entry sr = rs.stat(bucketName, key);
76-
assertTrue(sr.ok());
77-
assertTrue(expectedHash.equals(sr.getHash()));
78-
}
79-
80-
// deletes it from rs
81-
{
82-
RSClient rs = new RSClient(mac);
83-
CallRet cr = rs.delete(bucketName, key);
84-
assertTrue(cr.ok());
85-
}
86-
87-
// confirms that it's deleted
88-
{
89-
RSClient rs = new RSClient(mac);
90-
Entry sr = rs.stat(bucketName, key);
91-
assertTrue(!sr.ok());
92-
}
93+
// do nothing here.
9394
}
9495
}

0 commit comments

Comments
 (0)