七牛云遍历文件列表和删除文件

七牛云遍历文件列表和删除文件

Scroll Down

七牛云遍历文件列表

Configuration cfg = new Configuration(Zone.zone0());
String bucket = "空间名称";
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
BucketManager bucketManager = new BucketManager(auth, cfg);
//文件名前缀
String prefix = "";
//每次迭代的长度限制,最大1000,推荐值 1000
int limit = 1000;
//指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
String delimiter = "";
//列举空间文件列表
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
while (fileListIterator.hasNext()) {
    //处理获取的file list结果
    FileInfo[] items = fileListIterator.next();
    for (FileInfo item : items) {
        System.out.println(item.key);
        System.out.println(item.hash);
        System.out.println(item.fsize);
        System.out.println(item.mimeType);
        System.out.println(item.putTime);
        System.out.println(item.endUser);
    }
}

删除文件

public static int deleteFileFromQiniu(String fileName){
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    String key = fileName;
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
        Response delete = bucketManager.delete(空间名称, key)
        delete.close()
        return delete.statusCode;
    } catch (QiniuException ex) {
        //如果遇到异常,说明删除失败
        ex.printStackTrace();
        System.err.println(ex.code());
        System.err.println(ex.response.toString());
    }
    return -1;
}