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

com.qiniu.datasource.TenCosContainer Maven / Gradle / Ivy

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

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.COSObjectSummary;
import com.qiniu.common.SuitsException;
import com.qiniu.convert.Converter;
import com.qiniu.convert.JsonObjectPair;
import com.qiniu.convert.StringBuilderPair;
import com.qiniu.convert.StringMapPair;
import com.qiniu.interfaces.ILister;
import com.qiniu.interfaces.IStringFormat;
import com.qiniu.interfaces.ITypeConvert;
import com.qiniu.persistence.FileSaveMapper;
import com.qiniu.interfaces.IResultOutput;
import com.qiniu.util.CloudApiUtils;
import com.qiniu.util.ConvertingUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.*;

public class TenCosContainer extends CloudStorageContainer> {

//    private String secretId;
//    private String secretKey;
    private COSCredentials credentials;
    private ClientConfig clientConfig;

    public TenCosContainer(String secretId, String secretKey, ClientConfig clientConfig, String bucket,
                           Map> prefixesMap, List antiPrefixes, boolean prefixLeft,
                           boolean prefixRight, Map indexMap, List fields, int unitLen,
                           int threads) throws IOException {
        super(bucket, prefixesMap, antiPrefixes, prefixLeft, prefixRight, indexMap, fields, unitLen, threads);
//        this.secretId = secretId;
//        this.secretKey = secretKey;
        this.credentials = new BasicCOSCredentials(secretId, secretKey);
        this.clientConfig = clientConfig;
        TenLister tenLister = new TenLister(new COSClient(credentials, clientConfig),
                bucket, null, null, null, 1);
        tenLister.close();
        tenLister = null;
        COSObjectSummary test = new COSObjectSummary();
        test.setKey("test");
        ConvertingUtils.toPair(test, indexMap, new StringMapPair());
    }

    @Override
    public String getSourceName() {
        return "tencent";
    }

    @Override
    protected ITypeConvert> getNewConverter() {
        return new Converter>() {
            @Override
            public Map convertToV(COSObjectSummary line) throws IOException {
                return ConvertingUtils.toPair(line, indexMap, new StringMapPair());
            }
        };
    }

    @Override
    protected ITypeConvert getNewStringConverter() {
        IStringFormat stringFormatter;
        if ("json".equals(saveFormat)) {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new JsonObjectPair()).toString();
        } else {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new StringBuilderPair(saveSeparator));
        }
        return new Converter() {
            @Override
            public String convertToV(COSObjectSummary line) throws IOException {
                return stringFormatter.toFormatString(line);
            }
        };
    }

    @Override
    protected IResultOutput getNewResultSaver(String order) throws IOException {
        return order != null ? new FileSaveMapper(savePath, getSourceName(), order) : new FileSaveMapper(savePath);
    }

    @Override
    protected ILister getLister(String prefix, String marker, String start, String end) throws SuitsException {
        if (marker == null || "".equals(marker)) marker = CloudApiUtils.getTenCosMarker(start);
        return new TenLister(new COSClient(credentials, clientConfig), bucket, prefix, marker, end, unitLen);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy