Skip to content

Commit 366a4a2

Browse files
authored
Merge pull request #205 from tengqiniu/master
add some demos
2 parents b86b36a + 7938501 commit 366a4a2

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

examples/BatchDemo.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.http.Response;
3+
import com.qiniu.storage.BucketManager;
4+
import com.qiniu.storage.model.FileInfo;
5+
import com.qiniu.storage.model.FileListing;
6+
import com.qiniu.util.Auth;
7+
8+
9+
public class BatchDemo {
10+
public static void main(String args[]){
11+
//设置需要操作的账号的AK和SK
12+
String ACCESS_KEY = "Access_Key";
13+
String SECRET_KEY = "Secret_Key";
14+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
15+
16+
//实例化一个BucketManager对象
17+
BucketManager bucketManager = new BucketManager(auth);
18+
19+
//创建Batch类型的operations对象
20+
BucketManager.Batch operations = new BucketManager.Batch();
21+
22+
//第一组源空间名、原文件名,目的空间名、目的文件名
23+
String bucketFrom1 = "yourbucket";
24+
String keyFrom1 = "srckey1";
25+
String bucketTo1 = "yourbucket";
26+
String keyTo1 = "destkey1";
27+
28+
//第二组源空间名、原文件名,目的空间名、目的文件名
29+
String bucketFrom2 = "yourbucket";
30+
String keyFrom2 = "srckey2";
31+
String bucketTo2 = "yourbucket";
32+
String keyTo2 = "destkey2";
33+
34+
35+
try {
36+
//调用批量操作的batch方法
37+
Response res = bucketManager.batch(operations.move(bucketFrom1, keyFrom1, bucketTo1, keyTo1)
38+
.move(bucketFrom2, keyFrom2, bucketTo2, keyTo2));
39+
40+
System.out.println(res.toString());
41+
42+
} catch (QiniuException e) {
43+
//捕获异常信息
44+
Response r = e.response;
45+
System.out.println(r.toString());
46+
}
47+
}
48+
}

examples/FetchDemo.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.http.Response;
3+
import com.qiniu.storage.BucketManager;
4+
import com.qiniu.util.Auth;
5+
6+
7+
public class FetchDemo {
8+
public static void main(String args[]){
9+
//设置需要操作的账号的AK和SK
10+
String ACCESS_KEY = "Access_Key";
11+
String SECRET_KEY = "Secret_Key";
12+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
13+
14+
//实例化一个BucketManager对象
15+
BucketManager bucketManager = new BucketManager(auth);
16+
17+
//文件保存的空间名和文件名
18+
String bucket = "yourbucket";
19+
String key = "yourkey";
20+
21+
//要fetch的url
22+
String url = "url";
23+
24+
try {
25+
//调用fetch方法抓取文件
26+
bucketManager.fetch(url, bucket,key);
27+
} catch (QiniuException e) {
28+
//捕获异常信息
29+
Response r = e.response;
30+
System.out.println(r.toString());
31+
}
32+
}
33+
}

examples/ListDemo.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.http.Response;
3+
import com.qiniu.storage.BucketManager;
4+
import com.qiniu.storage.model.FileInfo;
5+
import com.qiniu.storage.model.FileListing;
6+
import com.qiniu.util.Auth;
7+
8+
9+
public class ListDemo {
10+
public static void main(String args[]){
11+
//设置需要操作的账号的AK和SK
12+
String ACCESS_KEY = "Access_Key";
13+
String SECRET_KEY = "Secret_Key";
14+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
15+
16+
//实例化一个BucketManager对象
17+
BucketManager bucketManager = new BucketManager(auth);
18+
19+
//要列举文件的空间名
20+
String bucket = "yourbucket";
21+
22+
try {
23+
//调用listFiles方法列举指定空间的指定文件
24+
//参数一:bucket 空间名
25+
//参数二:prefix 文件名前缀
26+
//参数三:marker 上一次获取文件列表时返回的 marker
27+
//参数四:limit 每次迭代的长度限制,最大1000,推荐值 100
28+
//参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
29+
FileListing fileListing = bucketManager.listFiles(bucket,null,null,10,null);
30+
FileInfo[] items = fileListing.items;
31+
for(FileInfo fileInfo:items){
32+
System.out.println(fileInfo.key);
33+
}
34+
} catch (QiniuException e) {
35+
//捕获异常信息
36+
Response r = e.response;
37+
System.out.println(r.toString());
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)