Search in sources :

Example 6 with OnexException

use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.

the class AliyunOssService method upload.

@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
    String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
    if (StrUtil.isNotEmpty(prefix)) {
        if (StrUtil.isNotEmpty(prefixTotal)) {
            prefixTotal += "/" + prefix;
        } else {
            prefixTotal = prefix;
        }
    }
    String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
    OSS ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
    try {
        if (ossClient.doesObjectExist(config.getBucketName(), objectKey)) {
            // 文件已存在,则需要对文件重命名
            objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
        }
        ossClient.putObject(config.getBucketName(), objectKey, inputStream);
    } catch (OSSException | com.aliyun.oss.ClientException e) {
        throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
    } finally {
        ossClient.shutdown();
    }
    return config.getDomain() + objectKey;
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyuncs.exceptions.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 7 with OnexException

use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.

the class AliyunOssService method generatePresignedUrl.

@Override
public String generatePresignedUrl(String objectName, long expire) {
    OSS ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
    try {
        // 设置URL过期时间。
        Date expiration = DateUtil.offsetSecond(DateUtil.date(), (int) expire);
        // 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(config.getBucketName(), objectName, expiration);
        return url.toString();
    } catch (com.aliyun.oss.ClientException e) {
        throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
    } finally {
        ossClient.shutdown();
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Date(java.util.Date) URL(java.net.URL)

Example 8 with OnexException

use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.

the class HuaweiCloudOssService method upload.

@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
    String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
    if (StrUtil.isNotEmpty(prefix)) {
        if (StrUtil.isNotEmpty(prefixTotal)) {
            prefixTotal += "/" + prefix;
        } else {
            prefixTotal = prefix;
        }
    }
    String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
    ObsClient ossClient = null;
    try {
        ossClient = new ObsClient(config.getAccessKeyId(), config.getAccessKeySecret(), config.getEndPoint());
        if (ossClient.doesObjectExist(config.getBucketName(), objectKey)) {
            // 文件已存在,则需要对文件重命名
            objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
        }
        ossClient.putObject(config.getBucketName(), objectKey, inputStream);
    } catch (ObsException e) {
        throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
    } finally {
        // ObsClient在调用ObsClient.close方法关闭后不能再次使用
        if (ossClient != null) {
            try {
                ossClient.close();
            } catch (IOException e) {
                log.error("huaweicloud obs close error", e);
            }
        }
    }
    return config.getDomain() + objectKey;
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) ObsException(com.obs.services.exception.ObsException) ObsClient(com.obs.services.ObsClient)

Example 9 with OnexException

use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.

the class LogOperationAspect method saveLog.

/**
 * 保存日志
 */
private void saveLog(ProceedingJoinPoint joinPoint, String params, long time, Integer state, Exception e) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    LogBody logEntity = new LogBody();
    // 日志记录类型
    String storeType = "db";
    String logType = "operation";
    try {
        Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        LogOperation annotation = method.getAnnotation(LogOperation.class);
        if (annotation != null) {
            // 注解上的描述
            logEntity.setOperation(annotation.value());
            logType = annotation.type();
            storeType = annotation.storeType();
        }
    } catch (NoSuchMethodException ne) {
        ne.printStackTrace();
    }
    logEntity.setStoreType(storeType);
    logEntity.setState(state);
    logEntity.setRequestTime(time);
    logEntity.setType(logType);
    // 保存错误信息
    if (e != null) {
        logEntity.setContent(e instanceof OnexException ? e.toString() : ExceptionUtil.stacktraceToString(e));
    }
    // 请求参数
    Dict requestParams = Dict.create().set("params", params);
    HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
    if (null != request) {
        logEntity.setUri(request.getRequestURI());
        // 记录内容
        requestParams.set("ip", HttpContextUtils.getIpAddr(request));
        requestParams.set("ua", request.getHeader(HttpHeaders.USER_AGENT));
        requestParams.set("queryString", request.getQueryString());
        requestParams.set("url", request.getRequestURL());
        requestParams.set("method", request.getMethod());
        requestParams.set("contentType", request.getContentType());
    /*if (request instanceof OnexHttpServletRequestWrapper) {
                try {
                    requestParams.set("params", IoUtil.read(request.getInputStream()).toString());
                } catch (IOException ie) {
                    log.error("读取流失败", e);
                }
            }*/
    }
    logEntity.setRequestParams(requestParams);
    logService.saveLog(logEntity);
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) HttpServletRequest(javax.servlet.http.HttpServletRequest) LogOperation(com.nb6868.onex.common.annotation.LogOperation) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Dict(cn.hutool.core.lang.Dict) LogBody(com.nb6868.onex.common.log.LogBody) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod)

Example 10 with OnexException

use of com.nb6868.onex.common.exception.OnexException in project onex-boot by zhangchaoxu.

the class AliyunOssService method download.

@Override
public InputStream download(String objectKey) {
    OSS ossClient = null;
    OSSObject ossObject = null;
    try {
        ossClient = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
        ossObject = ossClient.getObject(config.getBucketName(), objectKey);
        return ossObject.getObjectContent();
    } catch (OSSException | com.aliyun.oss.ClientException e) {
        throw new OnexException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e);
    } finally {
        if (ossClient != null) {
            ossClient.shutdown();
        }
        if (ossObject != null) {
            try {
                ossObject.close();
            } catch (IOException e) {
                log.error("aliyun oss close error", e);
            }
        }
    }
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) OSSObject(com.aliyun.oss.model.OSSObject) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyuncs.exceptions.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Aggregations

OnexException (com.nb6868.onex.common.exception.OnexException)13 OSS (com.aliyun.oss.OSS)3 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)3 ClientException (com.aliyuncs.exceptions.ClientException)3 OSSException (com.aliyun.oss.OSSException)2 ObsClient (com.obs.services.ObsClient)2 ObsException (com.obs.services.exception.ObsException)2 IOException (java.io.IOException)2 Dict (cn.hutool.core.lang.Dict)1 Entity (cn.hutool.db.Entity)1 JWT (cn.hutool.jwt.JWT)1 OSSObject (com.aliyun.oss.model.OSSObject)1 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 AssumeRoleRequest (com.aliyuncs.auth.sts.AssumeRoleRequest)1 AssumeRoleResponse (com.aliyuncs.auth.sts.AssumeRoleResponse)1 IClientProfile (com.aliyuncs.profile.IClientProfile)1 ColumnEntity (com.nb6868.onex.coder.entity.ColumnEntity)1 TableEntity (com.nb6868.onex.coder.entity.TableEntity)1 LogOperation (com.nb6868.onex.common.annotation.LogOperation)1 AuthProps (com.nb6868.onex.common.auth.AuthProps)1