Search in sources :

Example 1 with StorageDownloadException

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

the class AbstractS3Client method download.

@Override
public File download(String key, File destFile, Consumer<StorageProgressEvent> consumer) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "downloadObject");
    try {
        minioClient.downloadObject(DownloadObjectArgs.builder().bucket(config.getBucketName()).object(key).filename(destFile.getPath()).build());
        return destFile;
    } catch (ErrorResponseException e) {
        throw throwS3Exception(e);
    } catch (Exception e) {
        throw new StorageDownloadException("0002", ExceptionUtils.toShortString(e, 2));
    }
}
Also used : ErrorResponseException(io.minio.errors.ErrorResponseException) 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) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException)

Example 2 with StorageDownloadException

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

the class AbstractS3Client method download.

@Override
public byte[] download(String key, Consumer<StorageProgressEvent> consumer) {
    key = formatKey(key, false);
    try (S3AsyncClient s3AsyncClient = getClient(key, "downloadObject")) {
        GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(config.getBucketName()).key(key).build();
        ResponseBytes<GetObjectResponse> getObjectResponseResponseBytes = s3AsyncClient.getObject(getObjectRequest, AsyncResponseTransformer.toBytes()).get();
        return getObjectResponseResponseBytes.asByteArray();
    } catch (S3Exception s3Exception) {
        throw throwS3Exception(s3Exception);
    } catch (Exception e) {
        throw new StorageDownloadException("0002", e);
    }
}
Also used : PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient)

Example 3 with StorageDownloadException

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

the class AbstractS3Client method downloadStream.

@Override
public StorageFileInfo downloadStream(String key, Consumer<StorageProgressEvent> consumer) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "downloadObject");
    try {
        GetObjectResponse objectResponse = minioClient.getObject(GetObjectArgs.builder().bucket(config.getBucketName()).object(key).build());
        StorageFileInfo storageFileInfo = new StorageFileInfo().setBucketName(config.getBucketName()).setKey(key).setHost(formatHost()).setContent(objectResponse);
        Headers headers = objectResponse.headers();
        if (headers != null && headers.toMultimap() != null) {
            Map<String, Object> metadata = headers.toMultimap().entrySet().stream().map(stringListEntry -> {
                List<String> value = stringListEntry.getValue();
                if (value != null && value.size() >= 1) {
                    return Pair.of(stringListEntry.getKey(), value.get(0));
                }
                return Pair.of(stringListEntry.getKey(), null);
            }).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
            storageFileInfo.setMetaData(metadata);
        }
        return storageFileInfo;
    } catch (ErrorResponseException e) {
        throw throwS3Exception(e);
    } catch (Exception e) {
        throw new StorageDownloadException("0002", ExceptionUtils.toShortString(e, 2));
    }
}
Also used : StaticProvider(io.minio.credentials.StaticProvider) StorageClientConfig(com.workoss.boot.storage.config.StorageClientConfig) URL(java.net.URL) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Bucket(io.minio.messages.Bucket) Headers(okhttp3.Headers) StorageClientNotFoundException(com.workoss.boot.storage.exception.StorageClientNotFoundException) Provider(io.minio.credentials.Provider) MinioException(io.minio.errors.MinioException) Owner(io.minio.messages.Owner) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpUtil(com.workoss.boot.storage.util.HttpUtil) Map(java.util.Map) Item(io.minio.messages.Item) ErrorResponse(io.minio.messages.ErrorResponse) ExceptionUtils(com.workoss.boot.util.exception.ExceptionUtils) Logger(org.slf4j.Logger) StorageException(com.workoss.boot.storage.exception.StorageException) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException) StringUtils(com.workoss.boot.util.StringUtils) FileInputStream(java.io.FileInputStream) CollectionUtils(com.workoss.boot.util.collection.CollectionUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StorageUtil(com.workoss.boot.storage.util.StorageUtil) io.minio(io.minio) Pair(com.workoss.boot.util.collection.Pair) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DateUtils(com.workoss.boot.util.DateUtils) List(java.util.List) com.workoss.boot.storage.model(com.workoss.boot.storage.model) ErrorResponseException(io.minio.errors.ErrorResponseException) CacheBuilder(com.google.common.cache.CacheBuilder) StreamUtils(com.workoss.boot.util.StreamUtils) Cache(com.google.common.cache.Cache) InputStream(java.io.InputStream) Headers(okhttp3.Headers) List(java.util.List) ErrorResponseException(io.minio.errors.ErrorResponseException) 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) StorageDownloadException(com.workoss.boot.storage.exception.StorageDownloadException)

Aggregations

StorageDownloadException (com.workoss.boot.storage.exception.StorageDownloadException)3 StorageException (com.workoss.boot.storage.exception.StorageException)3 StorageClientNotFoundException (com.workoss.boot.storage.exception.StorageClientNotFoundException)2 ErrorResponseException (io.minio.errors.ErrorResponseException)2 MinioException (io.minio.errors.MinioException)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 StorageClientConfig (com.workoss.boot.storage.config.StorageClientConfig)1 com.workoss.boot.storage.model (com.workoss.boot.storage.model)1 HttpUtil (com.workoss.boot.storage.util.HttpUtil)1 StorageUtil (com.workoss.boot.storage.util.StorageUtil)1 DateUtils (com.workoss.boot.util.DateUtils)1 StreamUtils (com.workoss.boot.util.StreamUtils)1 StringUtils (com.workoss.boot.util.StringUtils)1 CollectionUtils (com.workoss.boot.util.collection.CollectionUtils)1 Pair (com.workoss.boot.util.collection.Pair)1 ExceptionUtils (com.workoss.boot.util.exception.ExceptionUtils)1 io.minio (io.minio)1 Provider (io.minio.credentials.Provider)1 StaticProvider (io.minio.credentials.StaticProvider)1