Search in sources :

Example 16 with StorageException

use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.

the class AbstractS3Client method getClient.

protected MinioClient getClient(String key, String action) {
    if (minioClient != null) {
        return minioClient;
    }
    if (!useStsToken()) {
        throw new StorageClientNotFoundException("00002", "Storage action:" + action + " key:" + key + " initClient error");
    }
    long now = System.currentTimeMillis();
    String cacheKey = StorageUtil.generateCacheKey(action, key);
    StorageStsToken storageStsToken = STS_TOKEN_CACHE.getIfPresent(cacheKey);
    boolean cacheusable = storageStsToken != null && storageStsToken.getExpiration().getTime() > now;
    if (!cacheusable) {
        storageStsToken = getStsToken(config, key, action);
        Date expiration = storageStsToken.getExpiration();
        if (expiration != null && expiration.getTime() - now > 3 * 60 * 1000) {
            STS_TOKEN_CACHE.put(cacheKey, storageStsToken);
        }
    }
    if (storageStsToken == null) {
        throw new StorageException("00002", "securityToken获取失败");
    }
    config.setEndpoint(storageStsToken.getEndpoint().replaceAll(config.getBucketName() + StorageUtil.DOT, ""));
    return this.createClient(config, storageStsToken);
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException) Date(java.util.Date) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException)

Example 17 with StorageException

use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.

the class AbstractS3Client method generatePresignedUrl.

@Override
public URL generatePresignedUrl(String key, Date expiration) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "generatePresignedUrl");
    int expire = (int) (expiration.getTime() - System.currentTimeMillis());
    try {
        String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(config.getBucketName()).expiry(expire, TimeUnit.MILLISECONDS).object(key).build());
        return new URL(url);
    } catch (ErrorResponseException e) {
        throw throwS3Exception(e);
    } catch (Exception e) {
        throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
    }
}
Also used : ErrorResponseException(io.minio.errors.ErrorResponseException) StorageException(com.workoss.boot.storage.exception.StorageException) URL(java.net.URL) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) MinioException(io.minio.errors.MinioException) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) ErrorResponseException(io.minio.errors.ErrorResponseException)

Example 18 with StorageException

use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.

the class SecurityServiceImpl method generateUploadStsSign.

@Override
public Mono<UploadSign> generateUploadStsSign(BaseStorageModel storage, String key, String mimeType, String successActionStatus) {
    return getConfig(storage).flatMap(context -> {
        if (context.isEmpty()) {
            return Mono.error(new StorageException("00001"));
        }
        String finalMimeType = StringUtils.isBlank(mimeType) ? MediaTypeFactory.getMediaType(key) : mimeType;
        // 判断host是否为空
        return tokenHandlerFactory.getHandler(storage.getStorageType()).generateUploadStsSign(context, storage.getBucketName(), key, finalMimeType, successActionStatus).flatMap(uploadSign -> {
            uploadSign.setStorageType(storage.getStorageType());
            uploadSign.setTenentId(storage.getTenentId());
            uploadSign.setBucketName(storage.getBucketName());
            uploadSign.setMimeType(finalMimeType);
            uploadSign.setSuccessActionStatus(StringUtils.isBlank(successActionStatus) ? null : successActionStatus);
            return Mono.just(uploadSign);
        });
    });
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException)

Example 19 with StorageException

use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.

the class SecurityServiceImpl method generateUploadSign.

@Override
public Mono<UploadSign> generateUploadSign(BaseStorageModel storage, String key, String mimeType, String successActionStatus) {
    return getConfig(storage).flatMap(context -> {
        if (context.isEmpty()) {
            return Mono.error(new StorageException("00001"));
        }
        String finalMimeType = StringUtils.isBlank(mimeType) ? MediaTypeFactory.getMediaType(key) : mimeType;
        // 判断host是否为空
        return tokenHandlerFactory.getHandler(storage.getStorageType()).generateUploadSign(context, storage.getBucketName(), key, finalMimeType, successActionStatus).flatMap(uploadSign -> {
            uploadSign.setStorageType(storage.getStorageType());
            uploadSign.setTenentId(storage.getTenentId());
            uploadSign.setBucketName(storage.getBucketName());
            uploadSign.setMimeType(finalMimeType);
            uploadSign.setSuccessActionStatus(successActionStatus);
            return Mono.just(uploadSign);
        });
    });
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException)

Example 20 with StorageException

use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.

the class SecurityServiceImpl method generateStsToken.

@Override
public Mono<STSToken> generateStsToken(BaseStorageModel storage, String key, String action) {
    // 先查询到账号信息 然后调用下层
    return getConfig(storage).flatMap(context -> {
        if (context.isEmpty()) {
            return Mono.error(new StorageException("00001"));
        }
        return tokenHandlerFactory.getHandler(storage.getStorageType()).generateStsToken(context, storage.getBucketName(), key, action).flatMap(stsToken -> {
            stsToken.setStorageType(storage.getStorageType());
            stsToken.setTenentId(storage.getTenentId());
            stsToken.setBucketName(storage.getBucketName());
            return Mono.just(stsToken);
        });
    });
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException)

Aggregations

StorageException (com.workoss.boot.storage.exception.StorageException)22 StorageDownloadException (com.workoss.boot.storage.exception.StorageDownloadException)10 StorageClientNotFoundException (com.workoss.boot.storage.exception.StorageClientNotFoundException)9 ErrorResponseException (io.minio.errors.ErrorResponseException)6 MinioException (io.minio.errors.MinioException)6 HashMap (java.util.HashMap)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Date (java.util.Date)3 Map (java.util.Map)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)2 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)2 ProgressListener (com.amazonaws.event.ProgressListener)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)2