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

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

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

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
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 ChangeStatus extends Base> {

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

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

    public ChangeStatus(String accessKey, String secretKey, Configuration configuration, String bucket, int status,
                        String savePath, int saveIndex) throws IOException {
        super("status", accessKey, secretKey, bucket, savePath, saveIndex);
        this.status = status;
        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 ChangeStatus(String accessKey, String secretKey, Configuration configuration, String bucket, int status,
                        String savePath) throws IOException {
        this(accessKey, secretKey, configuration, bucket, status, savePath, 0);
    }

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

    @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.addChangeStatusOps(bucket, status, 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);
        Response response = bucketManager.changeStatus(bucket, key, status);
        if (response.statusCode != 200) throw new QiniuException(response);
        response.close();
        return String.join("\t", key, "200");
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy