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

com.qiniu.process.aliyun.PrivateUrl Maven / Gradle / Ivy

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

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.DefaultCredentials;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.qiniu.interfaces.ILineProcess;
import com.qiniu.process.Base;
import com.qiniu.util.CloudApiUtils;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Map;

public class PrivateUrl extends Base> {

    private String endpoint;
    private Date expiration;
    private Map queries;
    private GeneratePresignedUrlRequest request;
    private Credentials credentials;
    private OSSClient ossClient;
    private ILineProcess> nextProcessor;

    public PrivateUrl(String accessKeyId, String accessKeySecret, String bucket, String endpoint, long expires,
                      Map queries) {
        super("aliprivate", accessKeyId, accessKeySecret, bucket);
        this.endpoint = endpoint;
        expiration = new Date(System.currentTimeMillis() + expires);
        this.queries = queries;
        request = new GeneratePresignedUrlRequest(bucket, "");
        request.setExpiration(expiration);
        if (queries != null) {
            for (Map.Entry entry : queries.entrySet())
                request.addQueryParameter(entry.getKey(), entry.getValue());
        }
        credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(credentials), null);
        CloudApiUtils.checkAliyun(ossClient);
    }

    public PrivateUrl(String accessKeyId, String accessKeySecret, String bucket, String endpoint, long expires,
                      Map queries, String savePath, int saveIndex) throws IOException {
        super("aliprivate", accessKeyId, accessKeySecret, bucket, savePath, saveIndex);
        this.endpoint = endpoint;
        expiration = new Date(System.currentTimeMillis() + expires);
        this.queries = queries;
        request = new GeneratePresignedUrlRequest(bucket, "");
        request.setExpiration(expiration);
        if (queries != null) {
            for (Map.Entry entry : queries.entrySet())
                request.addQueryParameter(entry.getKey(), entry.getValue());
        }
        credentials = new DefaultCredentials(accessKeyId, accessKeySecret);
        ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(credentials), null);
        CloudApiUtils.checkAliyun(ossClient);
    }

    public PrivateUrl(String accessKeyId, String accessKeySecret, String bucket, String endpoint, long expires,
                      Map queries, String savePath) throws IOException {
        this(accessKeyId, accessKeySecret, bucket, endpoint, expires, queries, savePath, 0);
    }

    @Override
    public void setNextProcessor(ILineProcess> nextProcessor) {
        this.nextProcessor = nextProcessor;
        if (nextProcessor != null) processName = String.join("_with_", nextProcessor.getProcessName(), processName);
    }

    @Override
    public PrivateUrl clone() throws CloneNotSupportedException {
        PrivateUrl ossPrivateUrl = (PrivateUrl)super.clone();
        ossPrivateUrl.request = new GeneratePresignedUrlRequest(bucket, "");
        ossPrivateUrl.request.setExpiration(expiration);
        if (queries != null) {
            for (Map.Entry entry : queries.entrySet())
                ossPrivateUrl.request.addQueryParameter(entry.getKey(), entry.getValue());
        }
        ossPrivateUrl.ossClient = new OSSClient(endpoint, new DefaultCredentialProvider(credentials), null);
        if (nextProcessor != null) ossPrivateUrl.nextProcessor = nextProcessor.clone();
        return ossPrivateUrl;
    }

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

    @Override
    public String singleResult(Map line) throws Exception {
        String key = line.get("key");
        if (key == null) throw new IOException("no key in " + line);
        request.setKey(key);
        URL url = ossClient.generatePresignedUrl(request);
        if (nextProcessor != null) {
            line.put("url", url.toString());
            return nextProcessor.processLine(line);
        }
        return String.join("\t", key, url.toString());
    }

    @Override
    public void closeResource() {
        super.closeResource();
        endpoint = null;
        expiration = null;
        queries = null;
        request = null;
        credentials = null;
        if (ossClient != null) {
            ossClient.shutdown();
            ossClient = null;
        }
        if (nextProcessor != null) nextProcessor.closeResource();
        nextProcessor = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy