Search in sources :

Example 11 with StorageException

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;
    });
}
Also used : StorageSignature(com.workoss.boot.storage.model.StorageSignature) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 12 with StorageException

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()));
    }
}
Also used : ICredential(com.huaweicloud.sdk.core.auth.ICredential) STSToken(com.workoss.boot.storage.model.STSToken) ServiceResponseException(com.huaweicloud.sdk.core.exception.ServiceResponseException) ArrayList(java.util.ArrayList) ICredential(com.huaweicloud.sdk.core.auth.ICredential) GlobalCredentials(com.huaweicloud.sdk.core.auth.GlobalCredentials) IamClient(com.huaweicloud.sdk.iam.v3.IamClient) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 13 with StorageException

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));
    }
}
Also used : IOException(java.io.IOException) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 14 with StorageException

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));
    }
}
Also used : SimpleHttpContentPublisher(software.amazon.awssdk.core.internal.http.async.SimpleHttpContentPublisher) AsyncExecuteRequest(software.amazon.awssdk.http.async.AsyncExecuteRequest) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleHttpContentPublisher(software.amazon.awssdk.core.internal.http.async.SimpleHttpContentPublisher) Publisher(org.reactivestreams.Publisher) SdkHttpContentPublisher(software.amazon.awssdk.http.async.SdkHttpContentPublisher) SdkAsyncHttpResponseHandler(software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map) StorageException(com.workoss.boot.storage.exception.StorageException)

Example 15 with StorageException

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));
    }
}
Also used : Owner(io.minio.messages.Owner) HashMap(java.util.HashMap) 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) Item(io.minio.messages.Item) ErrorResponseException(io.minio.errors.ErrorResponseException) 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