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();
}
}
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();
}
}
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;
}
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;
});
}
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);
}
Aggregations