All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qiniu.process.qos.ChangeLifecycle Maven / Gradle / Ivy

There is a newer version: 8.4.8
Show newest version
package com.qiniu.process.qos;

import com.qiniu.process.Base;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.BucketManager.*;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import com.qiniu.util.HttpRespUtils;
import com.qiniu.util.CloudApiUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChangeLifecycle extends Base> {

    private int days;
    private BatchOperations batchOperations;
    private List> lines;
    private Configuration configuration;
    private BucketManager bucketManager;

    public ChangeLifecycle(String accessKey, String secretKey, Configuration configuration, String bucket, int days)
            throws IOException{
        super("lifecycle", accessKey, secretKey, bucket);
        this.days = days;
        this.configuration = configuration;
        this.bucketManager = new BucketManager(Auth.create(accessKey, secretKey), configuration.clone());
        CloudApiUtils.checkQiniu(bucketManager, bucket);
    }

    public ChangeLifecycle(String accessKey, String secretKey, Configuration configuration, String bucket, int days,
                           String savePath, int saveIndex) throws IOException {
        super("lifecycle", accessKey, secretKey, bucket, savePath, saveIndex);
        this.days = days;
        this.batchSize = 1000;
        this.batchOperations = new BatchOperations();
        this.lines = new ArrayList<>();
        this.configuration = configuration;
        this.bucketManager = new BucketManager(Auth.create(accessKey, secretKey), configuration.clone());
        CloudApiUtils.checkQiniu(bucketManager, bucket);
    }

    public ChangeLifecycle(String accessKey, String secretKey, Configuration configuration, String bucket, int days,
                           String savePath) throws IOException {
        this(accessKey, secretKey, configuration, bucket, days, savePath, 0);
    }

    public ChangeLifecycle clone() throws CloneNotSupportedException {
        ChangeLifecycle changeLifecycle = (ChangeLifecycle)super.clone();
        changeLifecycle.bucketManager = new BucketManager(Auth.create(accessId, secretKey), configuration.clone());
        changeLifecycle.batchOperations = new BatchOperations();
        changeLifecycle.lines = new ArrayList<>();
        return changeLifecycle;
    }

    @Override
    protected String resultInfo(Map line) {
        return line.get("key");
    }

    @Override
    protected List> putBatchOperations(List> processList) throws IOException {
        batchOperations.clearOps();
        lines.clear();
        String key;
        for (Map map : processList) {
            key = map.get("key");
            if (key != null) {
                lines.add(map);
                batchOperations.addDeleteAfterDaysOps(bucket, days, key);
            } else {
                fileSaveMapper.writeError("key is not exists or empty in " + map, false);
            }
        }
        return lines;
    }

    @Override
    protected String batchResult(List> lineList) throws IOException {
        return HttpRespUtils.getResult(bucketManager.batch(batchOperations));
    }

    @Override
    protected String singleResult(Map line) throws IOException {
        String key = line.get("key");
        if (key == null) throw new IOException("key is not exists or empty in " + line);
        return String.join("\t", key, String.valueOf(days),
                HttpRespUtils.getResult(bucketManager.deleteAfterDays(bucket, key, days)));
    }

    @Override
    public void closeResource() {
        super.closeResource();
        batchOperations = null;
        lines = null;
        configuration = null;
        bucketManager = null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy