Search in sources :

Example 1 with BucketConfig

use of com.jeesuite.cos.BucketConfig in project jeesuite-libs by vakinge.

the class AbstractProvider method getBucketUrlPrefix.

/**
 * @param bucketName
 * @return
 */
protected String getBucketUrlPrefix(String bucketName) {
    BucketConfig config = currentBucketConfig(bucketName);
    if (StringUtils.isNotBlank(config.getUrlPrefix())) {
        return config.getUrlPrefix();
    }
    synchronized (bucketMappings) {
        if (StringUtils.isNotBlank(config.getUrlPrefix())) {
            return config.getUrlPrefix();
        }
        String urlPrefix = buildBucketUrlPrefix(bucketName);
        config.setUrlPrefix(urlPrefix);
        return urlPrefix;
    }
}
Also used : BucketConfig(com.jeesuite.cos.BucketConfig)

Example 2 with BucketConfig

use of com.jeesuite.cos.BucketConfig in project jeesuite-libs by vakinge.

the class MinioProvider method getBucketConfig.

@Override
public BucketConfig getBucketConfig(String bucketName) {
    if (!existsBucket(bucketName)) {
        return null;
    }
    boolean isPrivate = false;
    try {
        String bucketPolicy = minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
        isPrivate = StringUtils.isEmpty(bucketPolicy) ? true : false;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    BucketConfig config = new BucketConfig(bucketName, isPrivate, null);
    return config;
}
Also used : BucketConfig(com.jeesuite.cos.BucketConfig) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 3 with BucketConfig

use of com.jeesuite.cos.BucketConfig in project jeesuite-libs by vakinge.

the class QcloudProvider method getBucketConfig.

@Override
public BucketConfig getBucketConfig(String bucketName) {
    bucketName = buildBucketName(bucketName);
    if (!cosclient.doesBucketExist(bucketName))
        return null;
    CannedAccessControlList acl = cosclient.getBucketAcl(bucketName).getCannedAccessControl();
    return new BucketConfig(bucketName, acl == CannedAccessControlList.Private, null);
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) BucketConfig(com.jeesuite.cos.BucketConfig)

Example 4 with BucketConfig

use of com.jeesuite.cos.BucketConfig in project jeesuite-libs by vakinge.

the class AbstractProvider method getDownloadUrl.

@Override
public String getDownloadUrl(String bucketName, String fileKey, int expireInSeconds) {
    BucketConfig config = currentBucketConfig(bucketName);
    String url;
    if (config.isAuth()) {
        fileKey = resolveFileKey(bucketName, fileKey);
        String presignedUrl = generatePresignedUrl(bucketName, fileKey, expireInSeconds);
        String urlprefix = getBucketUrlPrefix(bucketName);
        url = presignedUrl.replaceFirst(URL_PREFIX_PATTERN, urlprefix);
    } else {
        url = getFullPath(bucketName, fileKey);
    }
    return url;
}
Also used : BucketConfig(com.jeesuite.cos.BucketConfig)

Example 5 with BucketConfig

use of com.jeesuite.cos.BucketConfig in project jeesuite-libs by vakinge.

the class AliyunProvider method createUploadToken.

@Override
public Map<String, Object> createUploadToken(UploadTokenParam param) {
    BucketConfig config = currentBucketConfig(param.getBucketName());
    Map<String, Object> result = new HashMap<>();
    PolicyConditions policyConds = new PolicyConditions();
    if (param.getFsizeMin() != null && param.getFsizeMax() != null) {
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, param.getFsizeMin(), param.getFsizeMax());
    } else {
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
    }
    if (param.getUploadDir() != null) {
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, param.getUploadDir());
    }
    if (StringUtils.isBlank(param.getCallbackHost())) {
        param.setCallbackHost(StringUtils.remove(config.getUrlPrefix(), "/").split(":")[1]);
    }
    if (StringUtils.isBlank(param.getCallbackBody())) {
        param.setCallbackBody(DEFAULT_CALLBACK_BODY);
    }
    Date expire = DateUtils.addSeconds(new Date(), (int) param.getExpires());
    String policy = ossClient.generatePostPolicy(expire, policyConds);
    String policyBase64 = null;
    String callbackBase64 = null;
    try {
        policyBase64 = BinaryUtil.toBase64String(policy.getBytes(StandardCharsets.UTF_8.name()));
        String callbackJson = param.getCallbackRuleAsJson();
        if (callbackJson != null) {
            callbackBase64 = BinaryUtil.toBase64String(callbackJson.getBytes(StandardCharsets.UTF_8.name()));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    String signature = ossClient.calculatePostSignature(policy);
    result.put("OSSAccessKeyId", conf.getAccessKey());
    result.put("policy", policyBase64);
    result.put("signature", signature);
    result.put("host", config.getUrlPrefix());
    result.put("dir", param.getUploadDir());
    result.put("expire", String.valueOf(expire.getTime()));
    if (callbackBase64 != null) {
        result.put("callback", callbackBase64);
    }
    return result;
}
Also used : PolicyConditions(com.aliyun.oss.model.PolicyConditions) HashMap(java.util.HashMap) CUploadObject(com.jeesuite.cos.CUploadObject) BucketConfig(com.jeesuite.cos.BucketConfig) Date(java.util.Date) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Aggregations

BucketConfig (com.jeesuite.cos.BucketConfig)5 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)2 OSSException (com.aliyun.oss.OSSException)1 PolicyConditions (com.aliyun.oss.model.PolicyConditions)1 CUploadObject (com.jeesuite.cos.CUploadObject)1 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)1 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1