From 79385014a4a68f59ca5b3d9afdcc6c8b4f6a0639 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 12 Jun 2016 21:52:23 +0800 Subject: [PATCH] add some demos --- examples/BatchDemo.java | 48 +++++++++++++++++++++++++++++++++++++++++ examples/FetchDemo.java | 33 ++++++++++++++++++++++++++++ examples/ListDemo.java | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 examples/BatchDemo.java create mode 100644 examples/FetchDemo.java create mode 100644 examples/ListDemo.java diff --git a/examples/BatchDemo.java b/examples/BatchDemo.java new file mode 100644 index 000000000..691c5e261 --- /dev/null +++ b/examples/BatchDemo.java @@ -0,0 +1,48 @@ +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.model.FileInfo; +import com.qiniu.storage.model.FileListing; +import com.qiniu.util.Auth; + + +public class BatchDemo { + public static void main(String args[]){ + //设置需要操作的账号的AK和SK + String ACCESS_KEY = "Access_Key"; + String SECRET_KEY = "Secret_Key"; + Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); + + //实例化一个BucketManager对象 + BucketManager bucketManager = new BucketManager(auth); + + //创建Batch类型的operations对象 + BucketManager.Batch operations = new BucketManager.Batch(); + + //第一组源空间名、原文件名,目的空间名、目的文件名 + String bucketFrom1 = "yourbucket"; + String keyFrom1 = "srckey1"; + String bucketTo1 = "yourbucket"; + String keyTo1 = "destkey1"; + + //第二组源空间名、原文件名,目的空间名、目的文件名 + String bucketFrom2 = "yourbucket"; + String keyFrom2 = "srckey2"; + String bucketTo2 = "yourbucket"; + String keyTo2 = "destkey2"; + + + try { + //调用批量操作的batch方法 + Response res = bucketManager.batch(operations.move(bucketFrom1, keyFrom1, bucketTo1, keyTo1) + .move(bucketFrom2, keyFrom2, bucketTo2, keyTo2)); + + System.out.println(res.toString()); + + } catch (QiniuException e) { + //捕获异常信息 + Response r = e.response; + System.out.println(r.toString()); + } + } +} diff --git a/examples/FetchDemo.java b/examples/FetchDemo.java new file mode 100644 index 000000000..2abf01d83 --- /dev/null +++ b/examples/FetchDemo.java @@ -0,0 +1,33 @@ +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import com.qiniu.storage.BucketManager; +import com.qiniu.util.Auth; + + +public class FetchDemo { + public static void main(String args[]){ + //设置需要操作的账号的AK和SK + String ACCESS_KEY = "Access_Key"; + String SECRET_KEY = "Secret_Key"; + Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); + + //实例化一个BucketManager对象 + BucketManager bucketManager = new BucketManager(auth); + + //文件保存的空间名和文件名 + String bucket = "yourbucket"; + String key = "yourkey"; + + //要fetch的url + String url = "url"; + + try { + //调用fetch方法抓取文件 + bucketManager.fetch(url, bucket,key); + } catch (QiniuException e) { + //捕获异常信息 + Response r = e.response; + System.out.println(r.toString()); + } + } +} diff --git a/examples/ListDemo.java b/examples/ListDemo.java new file mode 100644 index 000000000..fbcb29c2d --- /dev/null +++ b/examples/ListDemo.java @@ -0,0 +1,40 @@ +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.model.FileInfo; +import com.qiniu.storage.model.FileListing; +import com.qiniu.util.Auth; + + +public class ListDemo { + public static void main(String args[]){ + //设置需要操作的账号的AK和SK + String ACCESS_KEY = "Access_Key"; + String SECRET_KEY = "Secret_Key"; + Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); + + //实例化一个BucketManager对象 + BucketManager bucketManager = new BucketManager(auth); + + //要列举文件的空间名 + String bucket = "yourbucket"; + + try { + //调用listFiles方法列举指定空间的指定文件 + //参数一:bucket 空间名 + //参数二:prefix 文件名前缀 + //参数三:marker 上一次获取文件列表时返回的 marker + //参数四:limit 每次迭代的长度限制,最大1000,推荐值 100 + //参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串 + FileListing fileListing = bucketManager.listFiles(bucket,null,null,10,null); + FileInfo[] items = fileListing.items; + for(FileInfo fileInfo:items){ + System.out.println(fileInfo.key); + } + } catch (QiniuException e) { + //捕获异常信息 + Response r = e.response; + System.out.println(r.toString()); + } + } +}