Skip to content

Commit 817e11e

Browse files
committed
Merge pull request #201 from clouddxy/master
add some demos
2 parents b7bc4dd + 1b82467 commit 817e11e

File tree

11 files changed

+473
-0
lines changed

11 files changed

+473
-0
lines changed

examples/copy.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.util.Auth;
6+
7+
public class BucketManagerDemo {
8+
9+
public static void main(String args[]){
10+
//设置需要操作的账号的AK和SK
11+
String ACCESS_KEY = "Access_Key";
12+
String SECRET_KEY = "Secret_Key";
13+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
14+
//实例化一个BucketManager对象
15+
BucketManager bucketManager = new BucketManager(auth);
16+
//要测试的空间和key,并且这个key在你空间中存在
17+
String bucket = "Bucket_Name";
18+
String key = "Bucket_key";
19+
//将文件从文件key 复制到文件key2。 可以在不同bucket复制
20+
String key2 = "yourjavakey";
21+
try {
22+
//调用copy方法移动文件
23+
bucketManager.copy(bucket, key, bucket, key2);
24+
} catch (QiniuException e) {
25+
//捕获异常信息
26+
Response r = e.response;
27+
System.out.println(r.toString());
28+
}
29+
}
30+
}

examples/delete.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.util.Auth;
6+
7+
public class BucketManagerDemo {
8+
9+
public static void main(String args[]){
10+
//设置需要操作的账号的AK和SK
11+
String ACCESS_KEY = "Access_Key";
12+
String SECRET_KEY = "Secret_Key";
13+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
14+
//实例化一个BucketManager对象
15+
BucketManager bucketManager = new BucketManager(auth);
16+
//要测试的空间和key,并且这个key在你空间中存在
17+
String bucket = "Bucket_Name";
18+
String key = "Bucket_key";
19+
try {
20+
//调用delete方法移动文件
21+
bucketManager.delete(bucket, key);
22+
} catch (QiniuException e) {
23+
//捕获异常信息
24+
Response r = e.response;
25+
System.out.println(r.toString());
26+
}
27+
}
28+
}

examples/download.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import com.qiniu.util.Auth;
2+
public class DownloadDemo {
3+
//设置好账号的ACCESS_KEY和SECRET_KEY
4+
String ACCESS_KEY = "Access_Key";
5+
String SECRET_KEY = "Secret_Key";
6+
//密钥配置
7+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
8+
//构造私有空间的需要生成的下载的链接
9+
String URL = "http://bucketdomain/key";
10+
11+
public void download(){
12+
//调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
13+
String downloadRUL = auth.privateDownloadUrl(URL,3600);
14+
System.out.println(downloadRUL);
15+
}
16+
public static void main(String args[]){
17+
new DownloadDemo().download();
18+
}
19+
}

examples/fops.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.http.Response;
3+
import com.qiniu.processing.OperationManager;
4+
import com.qiniu.util.Auth;
5+
import com.qiniu.util.StringMap;
6+
import com.qiniu.util.UrlSafeBase64;
7+
8+
public class OperateDemo {
9+
10+
public static void main(String[] args) throws QiniuException {
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+
//新建一个OperationManager对象
16+
OperationManager operater = new OperationManager(auth);
17+
//设置要转码的空间和key,并且这个key在你空间中存在
18+
String bucket = "Bucket_Name";
19+
String key = "Bucket_key";
20+
//设置转码操作参数
21+
String fops = "avthumb/mp4/s/640x360/vb/1.25m";
22+
//设置转码的队列
23+
String pipeline = "yourpipelinename";
24+
//可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当前空间。
25+
String urlbase64 = UrlSafeBase64.encodeToString("目标Bucket_Name:自定义文件key");
26+
String pfops = fops + "|saveas/"+urlbase64;
27+
//设置pipeline参数
28+
StringMap params = new StringMap().putWhen("force", 1, true).putNotEmpty("pipeline", pipeline);
29+
try {
30+
String persistid = operater.pfop(bucket, key, pfops, params);
31+
//打印返回的persistid
32+
System.out.println(persistid);
33+
} catch (QiniuException e) {
34+
//捕获异常信息
35+
Response r = e.response;
36+
// 请求失败时简单状态信息
37+
System.out.println(r.toString());
38+
try {
39+
// 响应的文本信息
40+
System.out.println(r.bodyString());
41+
} catch (QiniuException e1) {
42+
//ignore
43+
}
44+
}
45+
}
46+
}

examples/move.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.util.Auth;
6+
7+
public class BucketManagerDemo {
8+
9+
public static void main(String args[]){
10+
//设置需要操作的账号的AK和SK
11+
String ACCESS_KEY = "Access_Key";
12+
String SECRET_KEY = "Secret_Key";
13+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
14+
//实例化一个BucketManager对象
15+
BucketManager bucketManager = new BucketManager(auth);
16+
//要测试的空间和key,并且这个key在你空间中存在
17+
String bucket = "Bucket_Name";
18+
String key = "Bucket_key";
19+
//将文件从文件key移动到文件key2, 可以在不同bucket移动,同空间移动相当于重命名
20+
String key2 = "yourjavakey";
21+
try {
22+
//调用move方法移动文件
23+
bucketManager.move(bucket, key, bucket, key2);
24+
} catch (QiniuException e) {
25+
//捕获异常信息
26+
Response r = e.response;
27+
System.out.println(r.toString());
28+
}
29+
}
30+
}

examples/stat.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.util.Auth;
6+
7+
public class BucketManagerDemo {
8+
9+
public static void main(String args[]){
10+
//设置需要操作的账号的AK和SK
11+
String ACCESS_KEY = "Access_Key";
12+
String SECRET_KEY = "Secret_Key";
13+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
14+
//实例化一个BucketManager对象
15+
BucketManager bucketManager = new BucketManager(auth);
16+
//要测试的空间和key,并且这个key在你空间中存在
17+
String bucket = "Bucket_Name";
18+
String key = "Bucket_key";
19+
try {
20+
//调用stat()方法获取文件的信息
21+
FileInfo info = bucketManager.stat(bucket, key);
22+
System.out.println(info.hash);
23+
System.out.println(info.key);
24+
} catch (QiniuException e) {
25+
//捕获异常信息
26+
Response r = e.response;
27+
System.out.println(r.toString());
28+
}
29+
}
30+
}

examples/upload.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import com.qiniu.util.Auth;
2+
import java.io.IOException;
3+
import com.qiniu.common.QiniuException;
4+
import com.qiniu.http.Response;
5+
import com.qiniu.storage.UploadManager;
6+
7+
public class UploadDemo {
8+
//设置好账号的ACCESS_KEY和SECRET_KEY
9+
String ACCESS_KEY = "Access_Key";
10+
String SECRET_KEY = "Secret_Key";
11+
//要上传的空间
12+
String bucketname = "Bucket_Name";
13+
//上传到七牛后保存的文件名
14+
String key = "my-java.png";
15+
//上传文件的路径
16+
String FilePath = "/.../...";
17+
18+
//密钥配置
19+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
20+
//创建上传对象
21+
UploadManager uploadManager = new UploadManager();
22+
23+
//简单上传,使用默认策略,只需要设置上传的空间名就可以了
24+
public String getUpToken(){
25+
return auth.uploadToken(bucketname);
26+
}
27+
28+
public void upload() throws IOException{
29+
try {
30+
//调用put方法上传
31+
Response res = uploadManager.put(FilePath, key, getUpToken());
32+
//打印返回的信息
33+
System.out.println(res.bodyString());
34+
} catch (QiniuException e) {
35+
Response r = e.response;
36+
// 请求失败时打印的异常的信息
37+
System.out.println(r.toString());
38+
try {
39+
//响应的文本信息
40+
System.out.println(r.bodyString());
41+
} catch (QiniuException e1) {
42+
//ignore
43+
}
44+
}
45+
}
46+
47+
public static void main(String args[]) throws IOException{
48+
new UploadDemo().upload();
49+
}
50+
51+
}

examples/upload_ recorder.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.IOException;
2+
3+
import com.qiniu.common.QiniuException;
4+
import com.qiniu.http.Response;
5+
import com.qiniu.storage.Recorder;
6+
import com.qiniu.storage.UploadManager;
7+
import com.qiniu.storage.persistent.FileRecorder;
8+
import com.qiniu.util.Auth;
9+
10+
public class UploadDemo {
11+
//设置好账号的ACCESS_KEY和SECRET_KEY
12+
String ACCESS_KEY = "Access_Key";
13+
String SECRET_KEY = "Secret_Key";
14+
//要上传的空间
15+
String bucketname = "Bucket_Name";
16+
//上传到七牛后保存的文件名
17+
String key = "my-java.png";
18+
//上传文件的路径
19+
String filePath = "/.../...";
20+
21+
//密钥配置
22+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
23+
//创建上传对象
24+
UploadManager uploadManager = new UploadManager();
25+
26+
// 覆盖上传
27+
public String getUpToken(){
28+
return auth.uploadToken(bucketname);
29+
}
30+
31+
public void upload() throws IOException{
32+
//设置断点记录文件保存在指定文件夹或的File对象
33+
String recordPath = "/.../...";
34+
//实例化recorder对象
35+
Recorder recorder = new FileRecorder(recordPath);
36+
//实例化上传对象,并且传入一个recorder对象
37+
UploadManager uploadManager = new UploadManager(recorder);
38+
39+
try {
40+
//调用put方法上传
41+
Response res = uploadManager.put("path/file", "key", getUpToken());
42+
//打印返回的信息
43+
System.out.println(res.bodyString());
44+
} catch (QiniuException e) {
45+
Response r = e.response;
46+
// 请求失败时打印的异常的信息
47+
System.out.println(r.toString());
48+
try {
49+
//响应的文本信息
50+
System.out.println(r.bodyString());
51+
} catch (QiniuException e1) {
52+
//ignore
53+
}
54+
}
55+
}
56+
57+
public static void main(String args[]) throws IOException{
58+
new UploadDemo().upload();
59+
}
60+
61+
}

examples/upload_callback.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import com.qiniu.util.Auth;
2+
import com.qiniu.util.StringMap;
3+
4+
import java.io.IOException;
5+
6+
import com.qiniu.common.QiniuException;
7+
import com.qiniu.http.Response;
8+
import com.qiniu.storage.UploadManager;
9+
10+
public class UploadDemo {
11+
//设置好账号的ACCESS_KEY和SECRET_KEY
12+
String ACCESS_KEY = "Access_Key";
13+
String SECRET_KEY = "Secret_Key";
14+
//要上传的空间
15+
String bucketname = "Bucket_Name";
16+
//上传到七牛后保存的文件名
17+
String key = "my-java.png";
18+
//上传文件的路径
19+
String FilePath = "/.../...";
20+
21+
//密钥配置
22+
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
23+
//创建上传对象
24+
UploadManager uploadManager = new UploadManager();
25+
26+
//设置callbackUrl以及callbackBody,七牛将文件名和文件大小回调给业务服务器
27+
public String getUpToken(){
28+
return auth.uploadToken(bucketname,null,3600,new StringMap()
29+
.put("callbackUrl","http://your.domain.com/callback")
30+
.put("callbackBody", "filename=$(fname)&filesize=$(fsize)"));
31+
}
32+
33+
public void upload() throws IOException{
34+
try {
35+
//调用put方法上传
36+
Response res = uploadManager.put(FilePath, null, getUpToken());
37+
//打印返回的信息
38+
System.out.println(res.bodyString());
39+
} catch (QiniuException e) {
40+
Response r = e.response;
41+
// 请求失败时打印的异常的信息
42+
System.out.println(r.toString());
43+
try {
44+
//响应的文本信息
45+
System.out.println(r.bodyString());
46+
} catch (QiniuException e1) {
47+
//ignore
48+
}
49+
}
50+
}
51+
52+
public static void main(String args[]) throws IOException{
53+
new UploadDemo().upload();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)