七牛云私有空间指定目录大量文件压缩示例

来源:互联网 发布:签署淘宝图片空间协议 编辑:程序博客网 时间:2024/04/24 11:48

最近使用到七牛云私有空间大量文件压缩功能,简单做下总结,大概步骤如下:
1.查询指定目录下所有文件。
2.循环进行链接加密,修改为公网可以访问资源。
3.按照七牛索引文件要求格式,拼接索引文件内容。
4.上传索引文件。
5.调用压缩接口。

七牛多文件压缩官方文档:
https://developer.qiniu.com/dora/manual/1667/mkzip
持久化数据处理官方文档:
https://developer.qiniu.com/dora/manual/1291/persistent-data-processing-pfop

  1 import com.qiniu.common.QiniuException;  2 import com.qiniu.common.Zone;  3 import com.qiniu.http.Response;  4 import com.qiniu.processing.OperationManager;  5 import com.qiniu.storage.BucketManager;  6 import com.qiniu.storage.Configuration;  7 import com.qiniu.storage.UploadManager;  8 import com.qiniu.storage.model.FileInfo;  9 import com.qiniu.storage.model.FileListing; 10 import com.qiniu.util.Auth; 11 import com.qiniu.util.StringMap; 12 import com.qiniu.util.UrlSafeBase64; 13 import org.apache.commons.lang3.StringUtils; 14  15 /** 16  * ClassName: QiNiuZipUtil  17  * @Description: 七牛云私有空间指定目录大量文件压缩示例 18  * @author ljwang 19  * @date 2017年9月5日 20  */ 21 public class QiNiuZipUtil { 22  23     public static final String ACCESS_KEY = "*******"; 24     public static final String SECRET_KEY = "*******"; 25     public static final String BUCKET = "*******"; 26     public static final String DOMAIN = "*******"; 27      28     /** 29      * 七牛回调URL 30      */ 31     public static final String NOTIFY_URL = "*******"; 32     /** 33      * 七牛间隔符 34      */ 35     public static final String QN_SEPARATOR = "/"; 36     /** 37      * txt换行符 38      */ 39     public static final String QN_NEWLINE = "\n"; 40     /** 41      * 索引文件名称 42      */ 43     public static final String TXT_NAME = "index.txt";     44      45  46     /** 47      * @Description: 大量文件压缩 48      * @author ljwang 49      * @date 2017年9月5日 50      */ 51     public static void mkzip(String prefix) { 52          53         //密钥配置 54         Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); 55          56         //自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。 57         Zone z = Zone.autoZone(); 58         Configuration c = new Configuration(z); 59  60         //实例化一个BucketManager对象 61         BucketManager bucketManager = new BucketManager(auth, c); 62  63         //创建上传对象 64         UploadManager uploadManager = new UploadManager(c); 65  66         try { 67             //调用listFiles方法列举指定空间的指定文件 68             //参数一:bucket    空间名 69             //参数二:prefix    文件名前缀 70             //参数三:marker    上一次获取文件列表时返回的 marker 71             //参数四:limit     每次迭代的长度限制,最大1000,推荐值 100 72             //参数五:delimiter 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串 73             FileListing fileListing = bucketManager.listFiles(BUCKET, prefix, null, 100, null); 74             FileInfo[] items = fileListing.items; 75              76             //压缩索引文件内容 77             String content = ""; 78             for(FileInfo fileInfo : items){ 79                 //拼接原始链接 80                 String url = "http://" + DOMAIN + QN_SEPARATOR + fileInfo.key; 81                 //链接加密并进行Base64编码,别名去除前缀目录。 82                 String safeUrl = "/url/" + UrlSafeBase64.encodeToString(auth.privateDownloadUrl(url)) + "/alias/" + UrlSafeBase64.encodeToString(fileInfo.key.substring(prefix.length())); 83                 content += ((StringUtils.isBlank(content) ? "" : QN_NEWLINE) + safeUrl); 84             } 85 //            System.out.println(content); 86              87             //索引文件路径 88             String txtKey = prefix + TXT_NAME; 89             //生成索引文件token(覆盖上传) 90             String uptoken = auth.uploadToken(BUCKET, txtKey, 3600, new StringMap().put("insertOnly", 0)); 91             //上传索引文件 92             Response res = uploadManager.put(content.getBytes(), txtKey, uptoken); 93             94             //默认utf-8,但是中文显示乱码,修改为gbk 95             String fops = "mkzip/4/encoding/" + UrlSafeBase64.encodeToString("gbk") + "|saveas/" + UrlSafeBase64.encodeToString(BUCKET + ":"  + prefix + "压缩文件名.zip"); 96  97             OperationManager operater = new OperationManager(auth, c); 98              99             StringMap params = new StringMap();100             //压缩完成后,七牛回调URL101             //params.put("notifyURL", NOTIFY_URL);102              103             String id = operater.pfop(BUCKET, txtKey, fops, params);104             String purl = "http://api.qiniu.com/status/get/prefop?color: #000000"> id;105             System.out.println(purl);106         } catch (QiniuException e) {107             Response res = e.response;108             System.out.println(res);109             try {110                 System.out.println(res.bodyString());111             } catch (QiniuException e1) {112                 e1.printStackTrace();113             }114         }115     }116     117     public static void main(String[] args) {118         String prefix = "public/download/";119         mkzip(prefix);120     }121 }

 

阅读全文
0 0
原创粉丝点击