zoukankan      html  css  js  c++  java
  • 七牛云私有空间指定目录大量文件压缩示例

    最近使用到七牛云私有空间大量文件压缩功能,简单做下总结,大概步骤如下:
    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 = "
    ";
     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             //压缩完成后,七牛回调URL
    101             //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?id=" + 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 }

    作者:行舟逐梦

    出处:http://www.cnblogs.com/liujianwang

    感谢您的认真阅读。不足之处,欢迎指正。

  • 相关阅读:
    HDU5586 最大连续和 xingxing在努力
    HDU5587 递推式+二分 xingxing在努力
    HDU2639 第k小01背包 xingxing在努力
    Java面向对象之继承
    Java面向对象之封装
    Java面向对象之封装
    Java流程控制语句
    Java流程控制语句
    Java运算符
    Java运算符
  • 原文地址:https://www.cnblogs.com/liujianwang/p/7479391.html
Copyright © 2011-2022 走看看