Skip to content

Commit 3387178

Browse files
authored
Merge pull request #236 from tengqiniu/master
修改增加多区域上传/资源管理方式 Demo
2 parents f961b7a + cc5a154 commit 3387178

13 files changed

+452
-304
lines changed

examples/BatchDemo.java

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,51 @@
22
import com.qiniu.http.Response;
33
import com.qiniu.storage.BucketManager;
44
import com.qiniu.util.Auth;
5+
import com.qiniu.common.Zone;
6+
import com.qiniu.storage.Configuration;
57

68

79
public class BatchDemo {
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);
1310

14-
//实例化一个BucketManager对象
15-
BucketManager bucketManager = new BucketManager(auth);
16-
17-
//创建Batch类型的operations对象
18-
BucketManager.Batch operations = new BucketManager.Batch();
19-
20-
//第一组源空间名、原文件名,目的空间名、目的文件名
21-
String bucketFrom1 = "yourbucket";
22-
String keyFrom1 = "srckey1";
23-
String bucketTo1 = "yourbucket";
24-
String keyTo1 = "destkey1";
25-
26-
//第二组源空间名、原文件名,目的空间名、目的文件名
27-
String bucketFrom2 = "yourbucket";
28-
String keyFrom2 = "srckey2";
29-
String bucketTo2 = "yourbucket";
30-
String keyTo2 = "destkey2";
31-
32-
33-
try {
34-
//调用批量操作的batch方法
35-
Response res = bucketManager.batch(operations.move(bucketFrom1, keyFrom1, bucketTo1, keyTo1)
36-
.move(bucketFrom2, keyFrom2, bucketTo2, keyTo2));
37-
38-
System.out.println(res.toString());
39-
40-
} catch (QiniuException e) {
41-
//捕获异常信息
42-
Response r = e.response;
43-
System.out.println(r.toString());
44-
}
45-
}
11+
public static void main(String args[]){
12+
//设置需要操作的账号的AK和SK
13+
String ACCESS_KEY = "Access_Key";
14+
String SECRET_KEY = "Secret_Key";
15+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
16+
17+
Zone z = Zone.zone0();
18+
Configuration c = new Configuration(z);
19+
20+
//实例化一个BucketManager对象
21+
BucketManager bucketManager = new BucketManager(auth,c);
22+
23+
//创建Batch类型的operations对象
24+
BucketManager.Batch operations = new BucketManager.Batch();
25+
26+
//第一组源空间名、原文件名,目的空间名、目的文件名
27+
String bucketFrom1 = "yourbucket";
28+
String keyFrom1 = "srckey1";
29+
String bucketTo1 = "yourbucket";
30+
String keyTo1 = "destkey1";
31+
32+
//第二组源空间名、原文件名,目的空间名、目的文件名
33+
String bucketFrom2 = "yourbucket";
34+
String keyFrom2 = "srckey2";
35+
String bucketTo2 = "yourbucket";
36+
String keyTo2 = "destkey2";
37+
38+
39+
try {
40+
//调用批量操作的batch方法
41+
Response res = bucketManager.batch(operations.move(bucketFrom1, keyFrom1, bucketTo1, keyTo1)
42+
.move(bucketFrom2, keyFrom2, bucketTo2, keyTo2));
43+
44+
System.out.println(res.toString());
45+
46+
} catch (QiniuException e) {
47+
//捕获异常信息
48+
Response r = e.response;
49+
System.out.println(r.toString());
50+
}
51+
}
4652
}

examples/FetchDemo.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22
import com.qiniu.http.Response;
33
import com.qiniu.storage.BucketManager;
44
import com.qiniu.util.Auth;
5-
5+
import com.qiniu.common.Zone;
6+
import com.qiniu.storage.Configuration;
67

78
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);
139

14-
//实例化一个BucketManager对象
15-
BucketManager bucketManager = new BucketManager(auth);
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);
1615

17-
//文件保存的空间名和文件名
18-
String bucket = "yourbucket";
19-
String key = "yourkey";
16+
Zone z = Zone.zone0();
17+
Configuration c = new Configuration(z);
2018

21-
//要fetch的url
22-
String url = "url";
19+
//实例化一个BucketManager对象
20+
BucketManager bucketManager = new BucketManager(auth,c);
21+
22+
//文件保存的空间名和文件名
23+
String bucket = "yourbucket";
24+
String key = "yourkey";
25+
26+
//要fetch的url
27+
String url = "url";
28+
29+
try {
30+
//调用fetch方法抓取文件
31+
bucketManager.fetch(url, bucket,key);
32+
} catch (QiniuException e) {
33+
//捕获异常信息
34+
Response r = e.response;
35+
System.out.println(r.toString());
36+
}
37+
}
2338

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-
}
3339
}

examples/ListDemo.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,43 @@
44
import com.qiniu.storage.model.FileInfo;
55
import com.qiniu.storage.model.FileListing;
66
import com.qiniu.util.Auth;
7+
import com.qiniu.common.Zone;
8+
import com.qiniu.storage.Configuration;
79

810

911
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);
1512

16-
//实例化一个BucketManager对象
17-
BucketManager bucketManager = new BucketManager(auth);
18-
19-
//要列举文件的空间名
20-
String bucket = "yourbucket";
13+
public static void main(String args[]){
14+
//设置需要操作的账号的AK和SK
15+
String ACCESS_KEY = "Access_Key";
16+
String SECRET_KEY = "Secret_Key";
17+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
18+
19+
Zone z = Zone.zone0();
20+
Configuration c = new Configuration(z);
2121

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-
}
22+
//实例化一个BucketManager对象
23+
BucketManager bucketManager = new BucketManager(auth,c);
24+
25+
//要列举文件的空间名
26+
String bucket = "yourbucket";
27+
28+
try {
29+
//调用listFiles方法列举指定空间的指定文件
30+
//参数一:bucket 空间名
31+
//参数二:prefix 文件名前缀
32+
//参数三:marker 上一次获取文件列表时返回的 marker
33+
//参数四:limit 每次迭代的长度限制,最大1000,推荐值 100
34+
//参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
35+
FileListing fileListing = bucketManager.listFiles(bucket,null,null,10,null);
36+
FileInfo[] items = fileListing.items;
37+
for(FileInfo fileInfo:items){
38+
System.out.println(fileInfo.key);
39+
}
40+
} catch (QiniuException e) {
41+
//捕获异常信息
42+
Response r = e.response;
43+
System.out.println(r.toString());
44+
}
45+
}
4046
}

examples/copy.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@
22
import com.qiniu.http.Response;
33
import com.qiniu.storage.BucketManager;
44
import com.qiniu.util.Auth;
5+
import com.qiniu.common.Zone;
6+
import com.qiniu.storage.Configuration;
57

68
public class BucketManagerDemo {
79

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-
//实例化一个BucketManager对象
14-
BucketManager bucketManager = new BucketManager(auth);
15-
//要测试的空间和key,并且这个key在你空间中存在
16-
String bucket = "Bucket_Name";
17-
String key = "Bucket_key";
18-
//将文件从文件key 复制到文件key2。 可以在不同bucket复制
19-
String key2 = "yourjavakey";
20-
try {
21-
//调用copy方法移动文件
22-
bucketManager.copy(bucket, key, bucket, key2);
23-
} catch (QiniuException e) {
24-
//捕获异常信息
25-
Response r = e.response;
26-
System.out.println(r.toString());
27-
}
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+
Zone z = Zone.zone0();
17+
Configuration c = new Configuration(z);
18+
19+
//实例化一个BucketManager对象
20+
BucketManager bucketManager = new BucketManager(auth,c);
21+
//要测试的空间和key,并且这个key在你空间中存在
22+
String bucket = "Bucket_Name";
23+
String key = "Bucket_key";
24+
//将文件从文件key 复制到文件key2。 可以在不同bucket复制
25+
String key2 = "yourjavakey";
26+
try {
27+
//调用copy方法移动文件
28+
bucketManager.copy(bucket, key, bucket, key2);
29+
} catch (QiniuException e) {
30+
//捕获异常信息
31+
Response r = e.response;
32+
System.out.println(r.toString());
2833
}
2934
}

examples/delete.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22
import com.qiniu.http.Response;
33
import com.qiniu.storage.BucketManager;
44
import com.qiniu.util.Auth;
5+
import com.qiniu.common.Zone;
6+
import com.qiniu.storage.Configuration;
57

68
public class BucketManagerDemo {
79

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-
//实例化一个BucketManager对象
14-
BucketManager bucketManager = new BucketManager(auth);
15-
//要测试的空间和key,并且这个key在你空间中存在
16-
String bucket = "Bucket_Name";
17-
String key = "Bucket_key";
18-
try {
19-
//调用delete方法移动文件
20-
bucketManager.delete(bucket, key);
21-
} catch (QiniuException e) {
22-
//捕获异常信息
23-
Response r = e.response;
24-
System.out.println(r.toString());
25-
}
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+
Zone z = Zone.zone0();
17+
Configuration c = new Configuration(z);
18+
19+
//实例化一个BucketManager对象
20+
BucketManager bucketManager = new BucketManager(auth,c);
21+
//要测试的空间和key,并且这个key在你空间中存在
22+
String bucket = "Bucket_Name";
23+
String key = "Bucket_key";
24+
try {
25+
//调用delete方法移动文件
26+
bucketManager.delete(bucket, key);
27+
} catch (QiniuException e) {
28+
//捕获异常信息
29+
Response r = e.response;
30+
System.out.println(r.toString());
31+
2632
}
2733
}

0 commit comments

Comments
 (0)