Search in sources :

Example 1 with StorageException

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

the class HttpUtil method doGet.

public static String doGet(@NonNull String url, @Nullable Map<String, String> param, @Nullable Map<String, String> headers) {
    SdkHttpFullResponse response = execute(url, SdkHttpMethod.GET, param, headers, null, DEFAULT_TIME_OUT);
    if (response == null) {
        throw new StorageException("request get has no response");
    }
    AbortableInputStream abortableInputStream = response.content().orElse(null);
    try {
        return abortableInputStream != null ? IoUtils.toUtf8String(abortableInputStream) : null;
    } catch (IOException e) {
        throw new StorageException(ExceptionUtils.toString(e));
    }
}
Also used : IOException(java.io.IOException) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 2 with StorageException

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

the class AbstractS3Client method getObject.

@Override
public StorageFileInfo getObject(String key) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "getObject");
    try {
        StatObjectResponse response = minioClient.statObject(StatObjectArgs.builder().bucket(config.getBucketName()).object(key).build());
        StorageFileInfo storageFileInfo = new StorageFileInfo();
        storageFileInfo.setBucketName(response.bucket());
        storageFileInfo.setKey(key);
        storageFileInfo.setHost(formatHost());
        storageFileInfo.setSize(response.size());
        storageFileInfo.setLastModified(DateUtils.getMillis(response.lastModified().toLocalDateTime()));
        Map<String, Object> userMeta = new HashMap<>();
        userMeta.putAll(response.userMetadata());
        storageFileInfo.setMetaData(userMeta);
        storageFileInfo.setETag(response.etag());
        return storageFileInfo;
    } catch (ErrorResponseException e) {
        throw throwS3Exception(e);
    } catch (Exception e) {
        throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
    }
}
Also used : HashMap(java.util.HashMap) ErrorResponseException(io.minio.errors.ErrorResponseException) StorageException(com.workoss.boot.storage.exception.StorageException) 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 3 with StorageException

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

the class AbstractS3Client method putObjectCommon.

StorageFileInfo putObjectCommon(String key, Object in, String contentType, Map<String, String> userMetaData, Consumer<StorageProgressEvent> consumer) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "putObject");
    InputStream inputStream = null;
    try {
        PutObjectArgs.Builder builder = PutObjectArgs.builder().bucket(config.getBucketName()).object(key);
        if (in instanceof File) {
            inputStream = new FileInputStream((File) in);
        } else if (in instanceof InputStream) {
            // 根据文件名称 放入objectMetaData
            inputStream = (InputStream) in;
        } else if (in instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) in);
        }
        builder.stream(inputStream, inputStream.available(), -1);
        if (StringUtils.isBlank(contentType)) {
            contentType = StorageUtil.getMimeType(key);
        }
        if (StringUtils.isNotBlank(contentType)) {
            builder.contentType(contentType);
        }
        if (userMetaData == null) {
            userMetaData = new HashMap<>(1);
        }
        userMetaData.put("upclient", "storage");
        builder.userMetadata(userMetaData);
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(builder.build());
        StorageFileInfo fileInfo = new StorageFileInfo();
        fileInfo.setBucketName(config.getBucketName());
        fileInfo.setKey(objectWriteResponse.object());
        fileInfo.setETag(objectWriteResponse.etag());
        fileInfo.setHost(formatHost());
        return fileInfo;
    } catch (ErrorResponseException e) {
        throw throwS3Exception(e);
    } catch (Exception e) {
        throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
    } finally {
        StreamUtils.close(inputStream);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) ErrorResponseException(io.minio.errors.ErrorResponseException) File(java.io.File) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 4 with StorageException

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

the class AbstractS3Client method copyObject.

@Override
public StorageFileInfo copyObject(String sourceKeyWithoutBasePath, String destinationKeyWithoutBasePath, Map<String, String> userMetaData) {
    MinioClient minioClient = getClient(sourceKeyWithoutBasePath, "copyObject");
    try {
        if (userMetaData == null) {
            userMetaData = new HashMap<>(1);
        }
        userMetaData.put("upclient", "storage");
        CopyObjectArgs copyObjectArgs = CopyObjectArgs.builder().bucket(config.getBucketName()).object(destinationKeyWithoutBasePath).userMetadata(userMetaData).source(CopySource.builder().bucket(config.getBucketName()).object(sourceKeyWithoutBasePath).build()).build();
        ObjectWriteResponse response = minioClient.copyObject(copyObjectArgs);
        StorageFileInfo fileInfo = new StorageFileInfo();
        fileInfo.setBucketName(config.getBucketName());
        fileInfo.setKey(response.object());
        fileInfo.setETag(response.etag());
        fileInfo.setHost(formatHost());
        return fileInfo;
    } 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) 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 5 with StorageException

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

the class AbstractS3Client method deleteObject.

@Override
public void deleteObject(String key) {
    key = formatKey(key, false);
    MinioClient minioClient = getClient(key, "deleteObject");
    try {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(config.getBucketName()).object(key).build());
    } 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) 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)

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