From scratch, use Qiniu cloud to realize file cloud storage in java (3)

In the previous blog, the author briefly introduced the resource management of qiniu SDK and the generation of various upload credentials. In this chapter, the author will introduce the resource batch management function of qiniu SDK.

 

The link to the previous article: Starting from scratch, using Qiniu cloud to realize file cloud storage in java (2)

 

Into the title!

  1. Get file status in batches:
    /**
    	 * Get file status in batches
    	 * @param zone
    	 * @param auth
    	 * @param keyList file name list, the number of files in a single batch request cannot exceed 1000
    	 * @param bucket
    	 * @return
    	 */
    	public static BatchStatus[] getFilesStat(Zone zone,Auth auth,String[] keyList,String bucket) {
    		// Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    			BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations ();
    			batchOperations.addStatOps(bucket, keyList);
    			Response response = bucketManager.batch(batchOperations);
    			BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    			for (int i = 0; i < keyList.length; i++) {
    				BatchStatus status = batchStatusList[i];
    				String key = keyList[i];
    				System.out.print(key + "\t");
    				if (status.code == 200) {
    					// file exists
    					System.out.println(status.data.hash);
    					System.out.println(status.data.mimeType);
    					System.out.println(status.data.fsize);
    					System.out.println (status.data.putTime);
    				} else {
    					System.out.println(status.data.error);
    				}
    			}
    			return batchStatusList;
    		} catch (QiniuException ex) {
    			System.err.println(ex.response.toString());
    		}
    		return null;
    	}
     Description: Parameter keyList: Array of file names to get the status.
  2. Batch edit file types:
    /**
    	 * Modify file types in batches, the number of files in a single batch request cannot exceed 1000
    	 * @param zone
    	 * @param auth
    	 * @param keyMimeMap file name: destination type, for example keyMimeMap.put("qiniu.jpg", "image/jpg");
    	 * @param bucket
    	 * @return
    	 */
    	public static BatchStatus[] editFilesType(Zone zone,Auth auth,HashMap<String, String> keyMimeMap,String bucket) {
    		//Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    		    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations ();
    		    //add command
    		    for (Map.Entry<String, String> entry : keyMimeMap.entrySet()) {
    		        String key = entry.getKey();
    		        String newMimeType = entry.getValue();
    		        batchOperations.addChgmOp (bucket, key, newMimeType);
    		    }
    		    Response response = bucketManager.batch(batchOperations);
    		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    		    int index = 0;
    		    for (Map.Entry<String, String> entry : keyMimeMap.entrySet()) {
    		        String key = entry.getKey();
    		        System.out.print(key + "\t");
    		        BatchStatus status = batchStatusList[index];
    		        if (status.code == 200) {
    		            System.out.println("change mime success");
    		        } else {
    		            System.out.println(status.data.error);
    		        }
    		        index += 1;
    		    }
    		    return batchStatusList;
    		} catch (QiniuException ex) {
    		    System.err.println(ex.response.toString());
    		}
    		return null;
    	}
    
     Description: parameter keyMimeMap: map type parameter, key is the name of the file to be processed, and value is the file format to be modified.
  3. batch deletion:
    /**
    	 * Bulk delete files
    	 * @param zone
    	 * @param auth
    	 * @param keyList file name list, the number of files in a single batch request cannot exceed 1000
    	 * @param bucket
    	 * @return
    	 */
    	public static BatchStatus[] deleteFiles(Zone zone,Auth auth,String[] keyList,String bucket) {
    		//Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    		    //The number of files in a single batch request cannot exceed 1000
    		    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations ();
    		    batchOperations.addDeleteOp (bucket, keyList);
    		    Response response = bucketManager.batch(batchOperations);
    		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    		    for (int i = 0; i < keyList.length; i++) {
    		        BatchStatus status = batchStatusList[i];
    		        String key = keyList[i];
    		        System.out.print(key + "\t");
    		        if (status.code == 200) {
    		            System.out.println("delete success");
    		        } else {
    		            System.out.println(status.data.error);
    		        }
    		    }
    		    return batchStatusList;
    		} catch (QiniuException ex) {
    		    System.err.println(ex.response.toString());
    		    return null;
    		}
    	}
     Description: Parameter keyList: Array of file names to get the status.
  4. Bulk move:
    /**
    	 * Bulk move files, can be used for renaming
    	 * @param zone
    	 * @param auth
    	 * @param opts set of operations
    	 * @return
    	 */
    	public static BatchStatus[] moveOrRenameFiles(Zone zone,Auth auth,List<OptInfo> opts) {
    		//Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    		    //The number of files in a single batch request cannot exceed 1000
    		    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations ();
    		    for (OptInfo opt : opts) {
    		        batchOperations.addMoveOp(opt.getFromBucket(), opt.getFromKey(), opt.getToBucket(), opt.getToKey());
    		    }
    		    Response response = bucketManager.batch(batchOperations);
    		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    		    for (int i = 0; i < opts.size(); i++) {
    		        BatchStatus status = batchStatusList[i];
    		        String key = opts.get(i).getFromKey();
    		        System.out.print(key + "\t");
    		        if (status.code == 200) {
    		            System.out.println("move success");
    		        } else {
    		            System.out.println(status.data.error);
    		        }
    		    }
    		    return batchStatusList;
    		} catch (QiniuException ex) {
    		    System.err.println(ex.response.toString());
    		}
    		return null;
    	}
     Description: Parameter opts: encapsulates the source and destination file names and space names, the code is as follows:
    public class OptInfo {
    	private String fromBucket;
    	private String fromKey;
    	private String toBucket;
    	private String toKey;
    	
    	public OptInfo(String fromBucket, String fromKey, String toBucket, String toKey) {
    		super();
    		this.fromBucket = fromBucket;
    		this.fromKey = fromKey;
    		this.toBucket = toBucket;
    		this.toKey = toKey;
    	}
    	//
    	//Save space getters and setters are not sticky
    }
     
  5. Bulk copy:
    /**
    	 * Bulk copy
    	 * @param zone
    	 * @param auth
    	 * @param opts set of operations
    	 * @return
    	 */
    	public static BatchStatus[] copyFiles(Zone zone,Auth auth,List<OptInfo> opts) {
    		//Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    		    BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations ();
    		    for (OptInfo opt : opts) {
    		        batchOperations.addCopyOp(opt.getFromBucket(), opt.getFromKey(), opt.getToBucket(), opt.getToKey());
    		    }
    		    Response response = bucketManager.batch(batchOperations);
    		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    		    for (int i = 0; i < opts.size(); i++) {
    		        BatchStatus status = batchStatusList[i];
    		        String key = opts.get(i).getFromKey();
    		        System.out.print(key + "\t");
    		        if (status.code == 200) {
    		            System.out.println("copy success");
    		        } else {
    		            System.out.println(status.data.error);
    		        }
    		    }
    		    return batchStatusList;
    		} catch (QiniuException ex) {
    		    System.err.println(ex.response.toString());
    		}
    		return null;
    	}
     Description: The parameter opts is the same as above.
  6. Batch mix operation:
    	/**
    	 * Mixed batch operations, the number of files in a single batch request cannot exceed 1000
    	 * @param zone
    	 * @param auth
    	 * @param batchOperations batch instruction set
    	 * 		例batchOperations.addStatOps(bucket, "qiniu.png", "qiniu.jpg");
    	 *		  batchOperations.addCopyOp(bucket, "qiniu.png", bucket, "qiniu_copy1.png");
    	 *		  batchOperations.addMoveOp(bucket, "qiniu2.png", bucket, "qiniu3.png");
    	 *		  batchOperations.addDeleteOp(bucket, "qiniu4.png");
    	 * @return
    	 */
    	public static BatchStatus[] fixOpts(Zone zone,Auth auth,BucketManager.BatchOperations batchOperations) {
    		//Construct a configuration class with the specified Zone object
    		Configuration cfg = new Configuration(zone);
    		BucketManager bucketManager = new BucketManager(auth, cfg);
    		try {
    		    Response response = bucketManager.batch(batchOperations);
    		    BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
    		    for (BatchStatus status : batchStatusList) {
    		        if (status.code == 200) {
    		            System.out.println("operation success");
    		        } else {
    		            System.out.println(status.data.error);
    		        }
    		    }
    		    return batchStatusList;
    		} catch (QiniuException ex) {
    		    System.err.println(ex.response.toString());
    		}
    		return null;
    	}
     Description: Parameter batchOperations: batch instruction set, which can delete, move and other instructions, please refer to the code comments for specific usage. 

Let’s experiment with the above methods in a unified way:

	public static void main(String[] args) {
		Zone zone=Zone.zone0();
		Auth auth=CredentialsManager.getAuth();
		String bucket="your space name";
		
// //Test getFilesStat
//		getFilesStat(zone, auth, new String[]{"2.jpg","3.jpg","4.jpg","noFile.jpg"},bucket);
//		
// //Test editFilesType
//		HashMap<String,String> keyMimeMap =new HashMap<>();
//		keyMimeMap. put("2.jpg", "text/plain");
//		keyMimeMap. put("4.jpg", "text/plain");
//		editFilesType(zone, auth, keyMimeMap, bucket);
//		
// //Test deleteFiles
//		deleteFiles(zone, auth, new String[]{"5.jpg","3.jpg","noFile.jpg"}, bucket);
//		
// //Test moveOrRenameFiles
//		List<OptInfo> opts=new ArrayList<>();
//		opts.add(new OptInfo(bucket, "as.txt", "bucket2", "as.txt"));
//		opts.add(new OptInfo(bucket, "sa.txt", "bucket2", "as.txt"));
//		moveOrRenameFiles(zone, auth, opts);
//		
// //Test copyFiles
//		List<OptInfo> opts2=new ArrayList<>();
//		opts2.add(new OptInfo(bucket, "fff.txt", "bucket2", "fff.txt"));
//		opts2.add(new OptInfo(bucket, "ggg.txt", "bucket2", "ggg.txt"));
//		copyFiles(zone, auth, opts2);
//		
// //Test fixOpts
// BatchOperations batchOperations = new BatchOperations ();
//		batchOperations.addDeleteOp(bucket, new String[]{"15.jpg","13.jpg","noFile.jpg"});
//		batchOperations.addCopyOp(bucket, "mmm.txt", "bucket2", "mmm.txt");
// fixOpts (zone, auth, batchOperations);
}

 The related operations have been mentioned in the previous two blogs, so I won't say more here.

 

 

Well, that's all for the introduction to Qiniu cloud storage!

Qiniu also provides a lot of interesting APIs, such as audio and video transcoding, image identification, political figure identification, watermarking, image compression and many other functions. If you need or are interested, please check the official documentation : https ://developer.qiniu.com/dora

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359540&siteId=291194637
Recommended