Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qiniu.process.huawei.PrivateUrl Maven / Gradle / Ivy
package com.qiniu.process.huawei;
import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.obs.services.model.TemporarySignatureRequest;
import com.qiniu.interfaces.ILineProcess;
import com.qiniu.process.Base;
import com.qiniu.util.CloudApiUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class PrivateUrl extends Base> {
private ObsConfiguration configuration;
private long expires;
private Map queries;
private TemporarySignatureRequest request;
private ObsClient obsClient;
private ILineProcess> nextProcessor;
public PrivateUrl(String accessKeyId, String accessKeySecret, String bucket, String endpoint, long expires,
Map queries) {
super("huaweiprivate", accessKeyId, accessKeySecret, bucket);
this.expires = expires;
this.queries = new HashMap<>();
request = new TemporarySignatureRequest();
request.setBucketName(bucket);
request.setObjectKey("");
request.setExpires(expires);
if (queries != null) {
for (Map.Entry entry : queries.entrySet())
queries.put(entry.getKey(), entry.getValue());
}
request.setQueryParams(this.queries);
configuration = new ObsConfiguration();
configuration.setEndPoint(endpoint);
obsClient = new ObsClient(accessId, secretKey, configuration);
CloudApiUtils.checkHuaWei(obsClient);
}
public PrivateUrl(String accessKeyId, String accessKeySecret, String bucket, String endpoint, long expires,
Map queries, String savePath, int saveIndex) throws IOException {
super("huaweiprivate", accessKeyId, accessKeySecret, bucket, savePath, saveIndex);
this.expires = expires;
this.queries = new HashMap<>();
request = new TemporarySignatureRequest();
request.setBucketName(bucket);
request.setObjectKey("");
request.setExpires(expires);
if (queries != null) {
for (Map.Entry entry : queries.entrySet())
queries.put(entry.getKey(), entry.getValue());
}
request.setQueryParams(this.queries);
configuration = new ObsConfiguration();
configuration.setEndPoint(endpoint);
obsClient = new ObsClient(accessId, secretKey, configuration);
CloudApiUtils.checkHuaWei(obsClient);
}
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.queries = new HashMap<>(queries);
ossPrivateUrl.request = new TemporarySignatureRequest();
ossPrivateUrl.request.setBucketName(bucket);
ossPrivateUrl.request.setObjectKey("");
ossPrivateUrl.request.setExpires(expires);
ossPrivateUrl.obsClient = new ObsClient(accessId, secretKey, configuration);
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.setObjectKey(key);
String url = obsClient.createTemporarySignature(request).getSignedUrl();
if (nextProcessor != null) {
line.put("url", url);
return nextProcessor.processLine(line);
}
return String.join("\t", key, url);
}
@Override
public void closeResource() {
super.closeResource();
configuration = null;
queries = null;
request = null;
if (obsClient != null) {
try {
obsClient.close();
} catch (IOException ignored) {
// 有关闭异常直接避开
}
obsClient = null;
}
if (nextProcessor != null) nextProcessor.closeResource();
nextProcessor = null;
}
}