use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class AwsS3ApiClient method removeFile.
/**
* 删除文件
*
* @param fileName OSS中保存的文件名
*/
@Override
public boolean removeFile(String fileName) {
this.check();
if (StringUtils.isEmpty(fileName)) {
throw new ServiceException("[" + this.storageType + "]删除文件失败:文件key为空");
}
try {
boolean exists = this.amazonS3.doesBucketExist(bucketName);
if (!exists) {
throw new ServiceException("[阿里云OSS] 文件删除失败!Bucket不存在:" + bucketName);
}
if (!this.amazonS3.doesObjectExist(bucketName, fileName)) {
throw new ServiceException("[阿里云OSS] 文件删除失败!文件不存在:" + bucketName + "/" + fileName);
}
this.amazonS3.deleteObject(bucketName, fileName);
return true;
} catch (Exception e) {
throw new ServiceException(e.getMessage());
} finally {
this.amazonS3.shutdown();
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class BaiduBosApiClient method check.
@Override
protected void check() {
if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(bucketName)) {
throw new ServiceException("[" + this.storageType + "]尚未配置,文件上传功能暂时不可用!");
}
BosClientConfiguration config = new BosClientConfiguration();
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
config.setEndpoint(endPoint);
config.setProtocol(Protocol.HTTPS);
this.bos = new BosClient(config);
boolean bucketExist = bos.doesBucketExist(bucketName);
if (!bucketExist) {
bos.createBucket(bucketName);
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class BaseApiClient method uploadFile.
@Override
public VirtualFile uploadFile(InputStream is, String fileName) {
Date startTime = new Date();
this.newFileName = fileName;
this.checkName();
try (InputStream uploadIs = StreamUtil.clone(is);
InputStream fileHashIs = StreamUtil.clone(is)) {
final String filePath = this.uploadInputStream(uploadIs, newFileName);
return VirtualFile.builder().originalFileName(this.newFileName).suffix(this.suffix).uploadStartTime(startTime).uploadEndTime(new Date()).filePath(filePath).fileHash(DigestUtils.md5DigestAsHex(fileHashIs)).fullFilePath(this.newFileUrl + filePath).build();
} catch (IOException ex) {
throw new ServiceException("[" + this.storageType + "]文件上传失败:" + ex.getMessage());
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class FastDfsOssApiClient method init.
@Override
public FastDfsOssApiClient init(FileProperties fileProperties) {
final FastDfsFileProperties fastDfsFileProperties = fileProperties.getFast();
this.serverUrl = fastDfsFileProperties.getServerUrl();
this.domainUrl = fastDfsFileProperties.getUrl();
checkDomainUrl(domainUrl);
Properties props = new Properties();
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, serverUrl);
try {
ClientGlobal.initByProperties(props);
} catch (IOException e) {
throw new ServiceException("[" + this.storageType + "]尚未配置阿里云FastDfs,文件上传功能暂时不可用!");
}
return this;
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class FastDfsOssApiClient method uploadInputStream.
@Override
public String uploadInputStream(InputStream is, String fileName) {
try {
// tracker 客户端
TrackerClient trackerClient = new TrackerClient();
// 获取trackerServer
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 创建StorageClient 对象
StorageClient storageClient = new StorageClient(trackerServer);
// 文件元数据信息组
NameValuePair[] nameValuePairs = { new NameValuePair("author", "huhy") };
byte[] bytes = IOUtils.toByteArray(is);
final String suffix = FileUtil.getSuffix(fileName);
String[] txts = storageClient.upload_file(bytes, suffix, nameValuePairs);
return String.join("/", txts);
} catch (IOException var6) {
log.info("上传失败,失败原因{}", var6.getMessage());
throw new ServiceException("文件上传异常!");
}
}
Aggregations