use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.
the class StorageUtil method requestSign.
public static StorageSignature requestSign(StorageHttpFunction httpFunc, StorageClientConfig config, String key, String mimeType, String successActionStatus) {
String url = formatTokenUrl(config.getTokenUrl()) + "/security/stssign";
String paramJson = StorageUtil.buildSignatureParam(config, key, mimeType, successActionStatus);
return request(url, paramJson, httpFunc, jsonNode -> {
StorageSignature storageSignature = JsonMapper.convertValue(jsonNode.get("data"), StorageSignature.class);
boolean check = StringUtils.isNotBlank(storageSignature.getAccessKey()) && StringUtils.isNotBlank(storageSignature.getSignature()) && StringUtils.isNotBlank(storageSignature.getPolicy());
if (!check) {
throw new StorageException("00001", "返回结果不正常");
}
// config 设置了domain 覆盖
if (StringUtils.isNotBlank(config.getDomain())) {
storageSignature.setHost(config.getDomain());
}
return storageSignature;
});
}
use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.
the class OBSTokenHandler method generateStsToken.
@Override
public Mono<STSToken> generateStsToken(final Context<String, String> context, String bucketName, final String key, final String action) {
String policy = renderSecurityTokenPolicy(context, bucketName, key, action);
ICredential auth = new GlobalCredentials().withAk(context.get("access_key")).withSk(context.get("secret_key"));
IamClient client = IamClient.newBuilder().withCredential(auth).withRegion(IamRegion.valueOf(context.get("region"))).build();
CreateTemporaryAccessKeyByAgencyRequest request = new CreateTemporaryAccessKeyByAgencyRequest();
CreateTemporaryAccessKeyByAgencyRequestBody body = new CreateTemporaryAccessKeyByAgencyRequestBody();
ServicePolicy policyIdentity = JsonMapper.parseObject(policy, ServicePolicy.class);
AssumeroleSessionuser sessionUserAssumeRole = new AssumeroleSessionuser();
sessionUserAssumeRole.withName(context.get("session_name", "popeye"));
IdentityAssumerole assumeRoleIdentity = new IdentityAssumerole();
assumeRoleIdentity.withAgencyName(context.get("agency_name")).withDomainName(context.get("domain_name")).withDurationSeconds(Integer.parseInt(context.get("token_duration_seconds", "1200"))).withSessionUser(sessionUserAssumeRole);
List<AgencyAuthIdentity.MethodsEnum> listIdentityMethods = new ArrayList<>();
listIdentityMethods.add(AgencyAuthIdentity.MethodsEnum.fromValue("assume_role"));
AgencyAuthIdentity identityAuth = new AgencyAuthIdentity();
identityAuth.withMethods(listIdentityMethods).withAssumeRole(assumeRoleIdentity).withPolicy(policyIdentity);
AgencyAuth authbody = new AgencyAuth();
authbody.withIdentity(identityAuth);
body.withAuth(authbody);
request.withBody(body);
try {
CreateTemporaryAccessKeyByAgencyResponse response = client.createTemporaryAccessKeyByAgency(request);
Credential credential = response.getCredential();
STSToken stsToken = new STSToken();
stsToken.setStsToken(credential.getSecuritytoken());
stsToken.setAccessKey(credential.getAccess());
stsToken.setSecretKey(credential.getSecret());
stsToken.setExpiration(DateUtils.parse(credential.getExpiresAt(), "yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'").plusHours(8));
String endpoint = getDomain(context, bucketName);
stsToken.setEndpoint(endpoint);
return Mono.just(stsToken);
} catch (ServiceResponseException e) {
return Mono.error(new StorageException("10001", e.toString()));
}
}
use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.
the class HttpUtil method doPostJson.
public static String doPostJson(@NonNull String url, @Nullable String jsonParam, @Nullable Map<String, String> headers) {
if (headers == null) {
headers = new HashMap<>(8);
}
headers.put(Header.CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
SdkHttpFullResponse response = execute(url, SdkHttpMethod.POST, null, headers, jsonParam, DEFAULT_TIME_OUT);
if (response == null) {
throw new StorageException("request post 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));
}
}
use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.
the class HttpUtil method execute.
static SdkHttpFullResponse execute(String url, SdkHttpMethod method, Map<String, String> params, Map<String, String> headers, String body, Duration timeout) {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder().uri(URI.create(url)).method(method);
if (CollectionUtils.isNotEmpty(params)) {
for (Map.Entry<String, String> stringStringEntry : params.entrySet()) {
requestBuilder.putRawQueryParameter(stringStringEntry.getKey(), stringStringEntry.getValue());
}
}
if (CollectionUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) {
requestBuilder.putHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
}
}
SdkHttpContentPublisher contentPublisher = null;
CompletableFuture<ByteArrayOutputStream> streamFuture = new CompletableFuture<>();
SdkHttpFullResponse.Builder responseBuilder = SdkHttpFullResponse.builder();
SdkAsyncHttpResponseHandler sdkAsyncHttpResponseHandler = new SdkAsyncHttpResponseHandler() {
@Override
public void onHeaders(SdkHttpResponse response) {
responseBuilder.headers(response.headers()).statusCode(response.statusCode()).statusText(response.statusText().orElse(null));
}
@Override
public void onStream(Publisher<ByteBuffer> stream) {
stream.subscribe(new BytesSubscriber(streamFuture));
}
@Override
public void onError(Throwable error) {
streamFuture.completeExceptionally(error);
}
};
SdkHttpFullRequest request = requestBuilder.build();
if (body != null) {
contentPublisher = new SdkHttpContentPublisherAdapter(AsyncRequestBody.fromString(body, StandardCharsets.UTF_8));
} else {
contentPublisher = new SimpleHttpContentPublisher(request);
}
request = getRequestWithContentLength(request, contentPublisher);
AsyncExecuteRequest executeRequest = AsyncExecuteRequest.builder().request(request).requestContentPublisher(contentPublisher).responseHandler(sdkAsyncHttpResponseHandler).build();
try {
CompletableFuture<Void> execute = getHttpClient(timeout).execute(executeRequest);
ByteArrayOutputStream byteArrayOutputStream = streamFuture.get(1, TimeUnit.MINUTES);
return responseBuilder.content(AbortableInputStream.create(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))).build();
} catch (InterruptedException e) {
throw new StorageException("500", ExceptionUtils.toShortString(e, 2));
} catch (ExecutionException e) {
throw new StorageException("500", ExceptionUtils.toShortString(e, 2));
} catch (TimeoutException e) {
throw new StorageException("500", ExceptionUtils.toShortString(e, 2));
}
}
use of com.workoss.boot.storage.exception.StorageException in project boot by workoss.
the class AbstractS3Client method listObjects.
@Override
public StorageFileInfoListing listObjects(String key, String delimiter, String nextToken, Integer maxKey) {
key = formatKey(key, true);
MinioClient minioClient = getClient(key, "listObjects");
boolean isFile = StorageUtil.SLASH.equals(delimiter);
try {
ListObjectsArgs.Builder builder = ListObjectsArgs.builder().bucket(config.getBucketName()).prefix(key).includeUserMetadata(true);
if (maxKey != null) {
builder.maxKeys(maxKey);
}
builder.delimiter(delimiter);
if (StringUtils.isNotBlank(nextToken)) {
// 报错
// builder.continuationToken(nextToken);
}
Iterable<Result<Item>> listObjects = minioClient.listObjects(builder.build());
StorageFileInfoListing listing = new StorageFileInfoListing();
String host = null;
for (Result<Item> listObject : listObjects) {
Item item = listObject.get();
StorageFileInfo fileInfo = new StorageFileInfo();
fileInfo.setBucketName(config.getBucketName());
fileInfo.setKey(item.objectName());
if (host == null) {
host = formatHost();
}
fileInfo.setHost(host);
if (!item.isDir()) {
Owner owner = item.owner();
if (owner != null && owner.id() != null) {
fileInfo.setOwner(owner.id());
}
fileInfo.setSize(item.size());
if (item.lastModified() != null) {
fileInfo.setLastModified(DateUtils.getMillis(item.lastModified().toLocalDateTime()));
}
Map<String, String> userMeta = item.userMetadata();
if (CollectionUtils.isNotEmpty(userMeta)) {
Map<String, Object> userMetaMap = new HashMap<>();
userMetaMap.putAll(userMeta);
fileInfo.setMetaData(userMetaMap);
}
}
if (isFile == item.isDir()) {
continue;
}
listing.addFileInfo(fileInfo);
}
return listing;
} catch (ErrorResponseException e) {
throw throwS3Exception(e);
} catch (Exception e) {
throw new StorageException("0002", ExceptionUtils.toShortString(e, 2));
}
}
Aggregations