package com.obsupload.configur;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: zhengwj
* @Description:
* @Date: 2020/4/20 12:47
* @Version: 1.0
*/
public class OBSHandler {
private String accessKeyId;// 华为云的 Access Key Id
private String accessKeySecret;// 华为云的 Access Key Secret
private String endpoint; // 华为云连接的地址节点
private String obsBucketName; // 创建的桶的名称
private String url; // 访问OBS文件的url
private static ObsClient obsClient; // 进行操作的华为云的客户端组件
/**
* 创建华为云OBS的本地控制器
* @param accessKeyId
* @param accessKeySecret
* @param endpoint
*/
public OBSHandler(String accessKeyId, String accessKeySecret, String endpoint) {
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.endpoint = endpoint;
}
public OBSHandler(String accessKeyId, String accessKeySecret, String endpoint, String obsBucketName) {
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.endpoint = endpoint;
this.obsBucketName = obsBucketName;
}
/**
* 设置OBS访问的CDN路径
* @param url
*/
public void setUrlForCDN(String url) {
this.url = url;
}
/**
* 设置OBS操作的同桶名称
* @param obsBucketName
*/
public void setObsBucketName(String obsBucketName) {
this.obsBucketName = obsBucketName;
}
/**
* 获取华为云提供的操作客户端实体类
* @return
*/
public ObsClient getObsClient() {
if(obsClient == null) {
obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint);
}
return obsClient;
}
/**
* 下载ObsObject
* @param bucketName 操作的桶的名称 例:"wangmarket1232311"
* @param filePath 需要下载的文件路径。 例:"site/a.txt"
* @return 下载文件的字节数组
* @throws IOException
*/
public byte[] getFileByteArray(String bucketName,