Search in sources :

Example 1 with ServiceException

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();
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException) SdkClientException(com.amazonaws.SdkClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ServiceException

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);
    }
}
Also used : BosClientConfiguration(com.baidubce.services.bos.BosClientConfiguration) ServiceException(com.zhouzifei.tool.common.ServiceException) DefaultBceCredentials(com.baidubce.auth.DefaultBceCredentials) BosClient(com.baidubce.services.bos.BosClient)

Example 3 with ServiceException

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());
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) Date(java.util.Date)

Example 4 with ServiceException

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;
}
Also used : FastDfsFileProperties(com.zhouzifei.tool.config.FastDfsFileProperties) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException) FileProperties(com.zhouzifei.tool.config.FileProperties) FastDfsFileProperties(com.zhouzifei.tool.config.FastDfsFileProperties)

Example 5 with ServiceException

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("文件上传异常!");
    }
}
Also used : NameValuePair(com.zhouzifei.tool.common.fastdfs.common.NameValuePair) ServiceException(com.zhouzifei.tool.common.ServiceException) IOException(java.io.IOException)

Aggregations

ServiceException (com.zhouzifei.tool.common.ServiceException)28 IOException (java.io.IOException)12 Connection (com.zhouzifei.tool.common.fastdfs.pool.Connection)4 SdkClientException (com.amazonaws.SdkClientException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)1 DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)1 BosClient (com.baidubce.services.bos.BosClient)1 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)1 ObsClient (com.obs.services.ObsClient)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1