java版七牛云批量删除空间文件

GoogleVip8 7月前 ⋅ 444 阅读

pom.xml导入七牛云jar

<!--MT: 七牛云必用-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.3.1</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.qiniu</groupId>
    <artifactId>happy-dns-java</artifactId>
    <version>0.1.4</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>com.qiniu</groupId>
    <artifactId>qiniu-java-sdk</artifactId>
    <version>7.2.6</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>
<!--end-->

七牛云官方介绍

地址:如何批量删除空间里的文件

java代码

package com.java.http;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import com.qiniu.util.Auth;

public class Test {

    public static void main(String[] args) throws QiniuException {
        QNForm qnForm = new QNForm("http://rs.qbox.me");
        getQNList(qnForm);

    }

    private static void getQNList(QNForm form) throws QiniuException {
        FileListing fileListing = form.bucketManager.listFiles(form.getBucket(), form.getPrefix(), form.getMarker(), form.getLimit(), form.getDelimiter());
        System.out.println("当前位置"+fileListing.marker);
        FileInfo[] items = fileListing.items;
        if (items.length == 0){
            System.out.println("已经删除完");
            return;
        }
        for (int i = 0; i < items.length; i++) {
            System.out.print(".");
            form.bucketManager.delete(form.getBucket(),items[i].key);
        }
        form.marker = fileListing.marker;
        getQNList(form);
    }

    static class QNForm {
        String bucket = "xxx";
        String marker = "";
        int limit = 1000;
        String prefix = "";
        String delimiter = "";

        String authorization;
        String url;
        BucketManager bucketManager;

        private final static String accessKey = "L0bQ2aL5xxxxxxxxxxxxxxxxxxxxfPMQhsA0IiBy";
        private final static String secretKey = "iPXXBf_WxxxxxxxxxxxxxxxxxxxxkpWAa71qx-94";
//    private final static String bucket = "gcyl";￿￿

        public QNForm(String url) {
            this.url = url;
            Auth auth = Auth.create(accessKey, secretKey);
            authorization = (String) auth.authorization(url).get("Authorization");
            Configuration config = new Configuration(Zone.autoZone());
            bucketManager = new BucketManager(auth, config);
        }

        public String getAuthorization() {
            return authorization;
        }

        public void setAuthorization(String authorization) {
            this.authorization = authorization;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getBucket() {
            return bucket;
        }

        public void setBucket(String bucket) {
            this.bucket = bucket;
        }

        public String getMarker() {
            return marker;
        }

        public void setMarker(String marker) {
            this.marker = marker;
        }public int getLimit() {
            return limit;
        }

        public void setLimit(int limit) {
            this.limit = limit;
        }

        public String getPrefix() {
            return prefix;
        }

        public void setPrefix(String prefix) {
            this.prefix = prefix;
        }

        public String getDelimiter() {
            return delimiter;
        }

        public void setDelimiter(String delimiter) {
            this.delimiter = delimiter;
        }

        @Override
        public String toString() {
            return "bucket=" + bucket +
                    "&marker=" + marker +
                    "&limit=" + limit +
                    "&prefix=" + prefix +
                    "&delimiter=" + delimiter;
        }
    }
}

代码比较粗糙,自行优化,我只用一次,就不调整了


全部评论: 0

    我有话说: