七牛空间数据转移 lowb 方法

2018年11月9日 17:14:49  逗比  [email protected]
public static void main(String[] args) {  
 //老数据   一些基本数据 项目里都会配置的
   String ACCESS_KEY = "";
   String SECRET_KEY = "";
   String oldbucket = "";
   String url = "";
   Auth oldauth = Auth.create(ACCESS_KEY, SECRET_KEY);
   Zone z = Zone.zone0();
   Configuration c = new Configuration(z);
   BucketManager bucketManager = new BucketManager(oldauth, c);
   //所有的文件集合
   List infos = new ArrayList<>();
   //失败文件集合
   List list = new ArrayList<>();
   //要列举文件的空间名
   try {
      String marker = null;
      Integer count = 0;
      while (true){
         FileListing domainLists = bucketManager.listFiles(oldbucket,null,marker,1000,null);
         if(EmptyUtils.isEmpty(domainLists.marker)){
            count += domainLists.items.length;
            infos.addAll(Arrays.asList(domainLists.items));
            break;
         }
         marker = domainLists.marker;
         count += domainLists.items.length;
         infos.addAll(Arrays.asList(domainLists.items));
      }
      System.out.println("共有:"+count+"个文件");
   } catch (QiniuException e) {
         e.printStackTrace();
   }
   //新空间 
   String ak = "";
   String sk = "";
   String bucket = "";
   Auth auth = Auth.create(ak, sk);
   Configuration cfg = new Configuration(Zone.zone0());
   UploadManager uploadManager = new UploadManager(cfg);
   for (FileInfo info : infos) {
      if (EmptyUtils.isNotEmpty(info.key)) {
         try {
            //  downLoadFromUrl 下载文件的方法  网上很多
            File file = CommonUtil.downLoadFromUrl(url + info.key, info.key, "d:/");
            FileInputStream fileInputStream = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                  ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
            Response res = uploadManager.put(multipartFile.getBytes(),info.key, getUpTokenImg(auth,bucket));
            MyRet ret = res.jsonToObject(MyRet.class);
            if(EmptyUtils.isEmpty(ret)){
               list.add(info);
            }else {
               file.delete();
            }
         } catch (Exception e) {
            list.add(info);
            System.out.println("错误"+e);
         }
      }
   }
   System.out.println(list.size()+"个文件失败!");
}

 

 

七牛自己的转移太牛逼看不懂  还是用简单的循环 舒服   

通俗易懂  

 

不过要是文件太大就很慢=。=  没办法

你可能感兴趣的:(java)