use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class AwsS3ApiClient method check.
@Override
protected void check() {
if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(bucketName)) {
throw new ServiceException("[" + this.storageType + "]尚未配置阿里云,文件上传功能暂时不可用!");
}
AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
if (StringUtils.isNotBlank(endpoint)) {
builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region));
} else if (StringUtils.isNotBlank(region)) {
builder.withRegion(region);
}
this.amazonS3 = builder.build();
try {
final boolean bucketExist = amazonS3.doesBucketExistV2(bucketName);
if (!bucketExist) {
amazonS3.createBucket(bucketName);
}
} catch (SdkClientException e) {
e.printStackTrace();
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class FastDfsOssApiClient method checkFile.
@Override
public CheckFileResult checkFile(MetaDataRequest metaDataRequest, HttpServletRequest request) {
// storageClient.deleteFile(UpLoadConstant.DEFAULT_GROUP, "M00/00/D1/eSqQlFsM_RWASgIyAAQLLONv59s385.jpg");
String userName = (String) request.getSession().getAttribute("name");
FileCacheEngine fileCacheEngine = new FileCacheEngine();
if (StringUtils.isEmpty(userName)) {
request.getSession().setAttribute("name", "yxqy");
}
String fileMd5 = metaDataRequest.getFileMd5();
if (StringUtils.isEmpty(fileMd5)) {
throw new ServiceException("fileMd5不能为空");
}
CheckFileResult checkFileResult = new CheckFileResult();
// 模拟从mysql中查询文件表的md5,这里从redis里查询
// 查询锁占用
String lockName = UpLoadConstant.currLocks + fileMd5;
Integer i = fileCacheEngine.get(fileMd5, lockName, Integer.class);
if (null == i) {
i = 0;
fileCacheEngine.add(fileMd5, lockName, "0");
}
int lock = i + 1;
fileCacheEngine.add(fileMd5, lockName, lock);
String lockOwner = UpLoadConstant.lockOwner + fileMd5;
String chunkCurrkey = UpLoadConstant.chunkCurr + fileMd5;
if (lock > 1) {
checkFileResult.setLock(1);
// 检查是否为锁的拥有者,如果是放行
String oWner = fileCacheEngine.get(fileMd5, lockOwner, String.class);
if (StringUtils.isEmpty(oWner)) {
throw new ServiceException("无法获取文件锁拥有者");
} else {
if (oWner.equals(request.getSession().getAttribute("name"))) {
String chunkCurr = (String) fileCacheEngine.get(fileMd5, chunkCurrkey);
if (StringUtils.isEmpty(chunkCurr)) {
throw new ServiceException("无法获取当前文件chunkCurr");
}
checkFileResult.setChunkCurr(Integer.parseInt(chunkCurr));
return checkFileResult;
} else {
throw new ServiceException("当前文件已有人在上传,您暂无法上传该文件");
}
}
} else {
// 初始化锁.分块
fileCacheEngine.add(fileMd5, lockOwner, request.getSession().getAttribute("name"));
fileCacheEngine.add(fileMd5, chunkCurrkey, "0");
checkFileResult.setChunkCurr(0);
return checkFileResult;
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class FastDfsOssApiClient method downloadFileStream.
@Override
public InputStream downloadFileStream(String fileName) {
try {
// tracker 客户端
TrackerClient trackerClient = new TrackerClient();
// 获取trackerServer
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 创建StorageClient 对象
StorageClient storageClient = new StorageClient(trackerServer);
final String group = getGroup(fileName);
final String filePath = getFilePath(fileName);
FileInfo fileInfo = storageClient.query_file_info(group, filePath);
if (null == fileInfo) {
throw new ServiceException("文件不存在");
}
byte[] bytes = storageClient.download_file(group, filePath);
return new ByteArrayInputStream(bytes);
} catch (IOException var6) {
log.info("上传失败,失败原因{}", var6.getMessage());
throw new ServiceException("文件上传异常!");
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class HuaweiCloudOssApiClient method init.
@Override
public HuaweiCloudOssApiClient init(FileProperties fileProperties) {
String huaweiAccessKey = fileProperties.getHuawei().getAccessKey();
String huaweiSecretKey = fileProperties.getOss().getSecretKey();
String huaweiEndpoint = fileProperties.getHuawei().getEndpoint();
String huaweiUrl = fileProperties.getHuawei().getUrl();
String huaweiBucketName = fileProperties.getOss().getBucketName();
if (StringUtils.isNullOrEmpty(huaweiAccessKey) || StringUtils.isNullOrEmpty(huaweiSecretKey) || StringUtils.isNullOrEmpty(huaweiEndpoint)) {
throw new ServiceException("[" + this.storageType + "]尚未配置华为云,文件上传功能暂时不可用!");
}
// 创建ObsClient实例
obsClient = new ObsClient(huaweiAccessKey, huaweiSecretKey, huaweiEndpoint);
this.bucket = huaweiBucketName;
checkDomainUrl(huaweiUrl);
return this;
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class QCloudOssApiClient method init.
@Override
public QCloudOssApiClient init(FileProperties fileProperties) {
final QcloudFileProperties qcloudFileProperties = fileProperties.getTengxun();
String accessKey = qcloudFileProperties.getAccessKey();
String secretKey = qcloudFileProperties.getSecretKey();
String endpoint = qcloudFileProperties.getEndpoint();
String url = qcloudFileProperties.getUrl();
this.bucketName = qcloudFileProperties.getBucketName();
checkDomainUrl(url);
if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(bucketName)) {
throw new ServiceException("[" + this.storageType + "]尚未配置腾讯云,文件上传功能暂时不可用!");
}
COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
Region region = new Region(endpoint);
ClientConfig clientConfig = new ClientConfig(region);
cosClient = new COSClient(cred, clientConfig);
return this;
}
Aggregations