Search in sources :

Example 6 with StorageException

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

the class AbstractS3Client method getBucket.

@Override
public StorageBucketInfo getBucket() {
    AmazonS3 amazonS3 = getClient("", "getBucket");
    try {
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        List<Bucket> buckets = amazonS3.listBuckets(listBucketsRequest);
        if (CollectionUtils.isEmpty(buckets)) {
            return null;
        }
        return buckets.stream().filter(bucket -> config.getBucketName().equals(bucket.getName())).map(bucket -> new StorageBucketInfo(bucket.getName(), bucket.getOwner() == null ? null : bucket.getOwner().getId(), bucket.getCreationDate())).findFirst().orElseGet(() -> null);
    } catch (AmazonS3Exception e) {
        throw new StorageException(e.getErrorCode(), e.getMessage());
    } catch (Exception e) {
        throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
    } finally {
        amazonS3.shutdown();
    }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) StorageClientConfig(com.workoss.boot.storage.config.StorageClientConfig) URL(java.net.URL) Date(java.util.Date) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Cache(com.github.benmanes.caffeine.cache.Cache) IOUtils(com.amazonaws.util.IOUtils) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) ProgressListener(com.amazonaws.event.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpUtil(com.workoss.boot.storage.util.HttpUtil) Map(java.util.Map) AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSCredentials(com.amazonaws.auth.AWSCredentials) RemovalCause(com.github.benmanes.caffeine.cache.RemovalCause) ExceptionUtils(com.workoss.boot.util.exception.ExceptionUtils) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Logger(org.slf4j.Logger) StorageException(com.workoss.boot.storage.exception.StorageException) com.amazonaws.services.s3.model(com.amazonaws.services.s3.model) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) StringUtils(com.workoss.boot.util.StringUtils) CollectionUtils(com.workoss.boot.util.collection.CollectionUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StorageUtil(com.workoss.boot.storage.util.StorageUtil) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) com.workoss.boot.storage.model(com.workoss.boot.storage.model) InputStream(java.io.InputStream) AmazonS3(com.amazonaws.services.s3.AmazonS3) StorageException(com.workoss.boot.storage.exception.StorageException) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException)

Example 7 with StorageException

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

the class AbstractS3Client method listBuckets.

@Override
public List<StorageBucketInfo> listBuckets() {
    AmazonS3 amazonS3 = getClient("", "listBuckets");
    try {
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        List<Bucket> buckets = amazonS3.listBuckets();
        if (CollectionUtils.isEmpty(buckets)) {
            return null;
        }
        return buckets.stream().map(bucket -> new StorageBucketInfo(bucket.getName(), bucket.getOwner() == null ? null : bucket.getOwner().getId(), bucket.getCreationDate())).collect(Collectors.toList());
    } catch (AmazonS3Exception e) {
        throw new StorageException(e.getErrorCode(), e.getMessage());
    } catch (Exception e) {
        throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
    } finally {
        amazonS3.shutdown();
    }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) StorageClientConfig(com.workoss.boot.storage.config.StorageClientConfig) URL(java.net.URL) Date(java.util.Date) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Cache(com.github.benmanes.caffeine.cache.Cache) IOUtils(com.amazonaws.util.IOUtils) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) ProgressListener(com.amazonaws.event.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpUtil(com.workoss.boot.storage.util.HttpUtil) Map(java.util.Map) AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSCredentials(com.amazonaws.auth.AWSCredentials) RemovalCause(com.github.benmanes.caffeine.cache.RemovalCause) ExceptionUtils(com.workoss.boot.util.exception.ExceptionUtils) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Logger(org.slf4j.Logger) StorageException(com.workoss.boot.storage.exception.StorageException) com.amazonaws.services.s3.model(com.amazonaws.services.s3.model) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) StringUtils(com.workoss.boot.util.StringUtils) CollectionUtils(com.workoss.boot.util.collection.CollectionUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StorageUtil(com.workoss.boot.storage.util.StorageUtil) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) com.workoss.boot.storage.model(com.workoss.boot.storage.model) InputStream(java.io.InputStream) AmazonS3(com.amazonaws.services.s3.AmazonS3) StorageException(com.workoss.boot.storage.exception.StorageException) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException)

Example 8 with StorageException

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

the class AbstractS3Client method doesObjectExist.

@Override
public boolean doesObjectExist(String key) {
    key = formatKey(key, false);
    try (S3AsyncClient s3AsyncClient = getClient(key, "doesObjectExist")) {
        HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket(config.getBucketName()).key(key).build();
        HeadObjectResponse headObjectResponse = s3AsyncClient.headObject(headObjectRequest).get();
    } catch (S3Exception s3Exception) {
        throw throwS3Exception(s3Exception);
    } catch (Exception e) {
        throw new StorageException("0002", e);
    }
    return false;
}
Also used : StorageException(com.workoss.boot.storage.exception.StorageException) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 9 with StorageException

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

the class StorageUtil method requestSTSToken.

public static StorageStsToken requestSTSToken(StorageHttpFunction httpFunc, StorageClientConfig config, String key, String action) {
    String url = formatTokenUrl(config.getTokenUrl()) + "/security/ststoken";
    String paramJson = StorageUtil.buildStsTokenParam(config, key, action);
    return request(url, paramJson, httpFunc, jsonNode -> {
        JsonNode dataNode = jsonNode.get("data");
        StorageStsToken stsToken = JsonMapper.convertValue(dataNode, StorageStsToken.class);
        boolean check = StringUtils.isNotBlank(stsToken.getAccessKey()) && StringUtils.isNotBlank(stsToken.getSecretKey()) && StringUtils.isNotBlank(stsToken.getStsToken()) && stsToken.getExpiration() != null;
        if (!check) {
            throw new StorageException("00001", "返回结果不正常");
        }
        return stsToken;
    });
}
Also used : StorageStsToken(com.workoss.boot.storage.model.StorageStsToken) JsonNode(com.fasterxml.jackson.databind.JsonNode) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 10 with StorageException

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

the class StorageUtil method request.

public static <T> T request(String url, String body, StorageHttpFunction httpFunc, Function<JsonNode, T> resultFun) {
    Map<String, String> header = new HashMap<>();
    header.put("X-SDK-CLIENT", "storage");
    String resp = httpFunc.apply(url, body, header);
    if (StringUtils.isBlank(resp)) {
        throw new StorageException("00001", "请求:" + url + "失败");
    }
    JsonNode jsonNode = JsonMapper.parse(resp);
    if (!jsonNode.has("code") || !jsonNode.has("data")) {
        throw new StorageException(resp);
    }
    String code = jsonNode.get("code").asText();
    if (!"0".equalsIgnoreCase(code)) {
        throw new StorageException(code, jsonNode.has("message") ? jsonNode.get("message").asText() : null);
    }
    return resultFun.apply(jsonNode);
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) 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