Search in sources :

Example 21 with ServiceException

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();
    }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) ServiceException(com.zhouzifei.tool.common.ServiceException) SdkClientException(com.amazonaws.SdkClientException) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 22 with ServiceException

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;
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) FileCacheEngine(com.zhouzifei.cache.FileCacheEngine) CheckFileResult(com.zhouzifei.tool.dto.CheckFileResult)

Example 23 with ServiceException

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

Example 24 with 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;
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) ObsClient(com.obs.services.ObsClient)

Example 25 with ServiceException

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;
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) ServiceException(com.zhouzifei.tool.common.ServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) QcloudFileProperties(com.zhouzifei.tool.config.QcloudFileProperties) ClientConfig(com.qcloud.cos.ClientConfig)

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