Search in sources :

Example 1 with HttpMethodName

use of com.qcloud.cos.http.HttpMethodName in project cos-java-sdk-v5 by tencentyun.

the class COSClient method generatePresignedUrl.

@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest req, Boolean signHost) throws CosClientException {
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when generating a pre-signed URL");
    rejectNull(req, "The request parameter must be specified when generating a pre-signed URL");
    req.rejectIllegalArguments();
    final String bucketName = req.getBucketName();
    final String key = req.getKey();
    if (req.getExpiration() == null) {
        req.setExpiration(new Date(System.currentTimeMillis() + this.clientConfig.getSignExpired() * 1000));
    }
    HttpMethodName httpMethod = req.getMethod();
    CosHttpRequest<GeneratePresignedUrlRequest> request = createRequest(bucketName, key, req, httpMethod);
    addParameterIfNotNull(request, "versionId", req.getVersionId());
    for (Entry<String, String> entry : req.getRequestParameters().entrySet()) {
        request.addParameter(entry.getKey(), entry.getValue());
    }
    addHeaderIfNotNull(request, Headers.CONTENT_TYPE, req.getContentType());
    addHeaderIfNotNull(request, Headers.CONTENT_MD5, req.getContentMd5());
    // Custom headers that open up the possibility of supporting unexpected
    // cases.
    Map<String, String> customHeaders = req.getCustomRequestHeaders();
    if (customHeaders != null) {
        for (Map.Entry<String, String> e : customHeaders.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
    }
    addResponseHeaderParameters(request, req.getResponseHeaders());
    COSSigner cosSigner = new COSSigner();
    COSCredentials cred = fetchCredential();
    String authStr = cosSigner.buildAuthorizationStr(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), cred, req.getExpiration(), signHost);
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(clientConfig.getHttpProtocol().toString()).append("://");
    strBuilder.append(clientConfig.getEndpointBuilder().buildGeneralApiEndpoint(formatBucket(bucketName, cred.getCOSAppId())));
    strBuilder.append(UrlEncoderUtils.encodeUrlPath(formatKey(key)));
    boolean hasAppendFirstParameter = false;
    if (authStr != null) {
        if (req.isSignPrefixMode()) {
            strBuilder.append("?sign=").append(UrlEncoderUtils.encode(authStr));
        } else {
            // urlencode auth string key & value
            String[] authParts = authStr.split("&");
            String[] encodeAuthParts = new String[authParts.length];
            for (int i = 0; i < authParts.length; i++) {
                String[] kv = authParts[i].split("=", 2);
                if (kv.length == 2) {
                    encodeAuthParts[i] = StringUtils.join("=", UrlEncoderUtils.encode(kv[0]), UrlEncoderUtils.encode(kv[1]));
                } else if (kv.length == 1) {
                    encodeAuthParts[i] = StringUtils.join("=", UrlEncoderUtils.encode(kv[0]));
                }
            }
            authStr = StringUtils.join("&", encodeAuthParts);
            strBuilder.append("?").append(authStr);
        }
        if (cred instanceof COSSessionCredentials) {
            strBuilder.append("&").append(Headers.SECURITY_TOKEN).append("=").append(UrlEncoderUtils.encode(((COSSessionCredentials) cred).getSessionToken()));
        }
        hasAppendFirstParameter = true;
    }
    for (Entry<String, String> entry : request.getParameters().entrySet()) {
        String paramKey = entry.getKey();
        String paramValue = entry.getValue();
        if (!hasAppendFirstParameter) {
            strBuilder.append("?");
            hasAppendFirstParameter = true;
        } else {
            strBuilder.append("&");
        }
        strBuilder.append(UrlEncoderUtils.encode(paramKey));
        if (paramValue != null) {
            strBuilder.append("=").append(UrlEncoderUtils.encode(paramValue));
        }
    }
    try {
        return new URL(strBuilder.toString());
    } catch (MalformedURLException e) {
        throw new CosClientException(e.toString());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) COSCredentials(com.qcloud.cos.auth.COSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) Date(java.util.Date) URL(java.net.URL) HttpMethodName(com.qcloud.cos.http.HttpMethodName) COSSessionCredentials(com.qcloud.cos.auth.COSSessionCredentials) COSSigner(com.qcloud.cos.auth.COSSigner) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with HttpMethodName

use of com.qcloud.cos.http.HttpMethodName in project yili-music by programmer-yili.

the class CosStorageServiceImpl method getFileUri.

@Override
public String getFileUri(String fileKey) {
    COSClient cosClient = createCOSClient();
    Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    Map<String, String> params = new HashMap<String, String>();
    Map<String, String> headers = new HashMap<String, String>();
    HttpMethodName method = HttpMethodName.GET;
    URL url = cosClient.generatePresignedUrl(bucket, fileKey, expirationDate, method, headers, params);
    cosClient.shutdown();
    return url.toString();
}
Also used : COSClient(com.qcloud.cos.COSClient) HttpMethodName(com.qcloud.cos.http.HttpMethodName) HashMap(java.util.HashMap) Date(java.util.Date) URL(java.net.URL)

Example 3 with HttpMethodName

use of com.qcloud.cos.http.HttpMethodName in project yili-music by programmer-yili.

the class CosStorageServiceImpl method getFileUri.

@Override
public String getFileUri(String fileKey) {
    COSClient cosClient = createCOSClient();
    Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    Map<String, String> params = new HashMap<String, String>();
    Map<String, String> headers = new HashMap<String, String>();
    HttpMethodName method = HttpMethodName.GET;
    URL url = cosClient.generatePresignedUrl(bucket, fileKey, expirationDate, method, headers, params);
    cosClient.shutdown();
    return url.toString();
}
Also used : COSClient(com.qcloud.cos.COSClient) HttpMethodName(com.qcloud.cos.http.HttpMethodName) HashMap(java.util.HashMap) Date(java.util.Date) URL(java.net.URL)

Aggregations

HttpMethodName (com.qcloud.cos.http.HttpMethodName)3 URL (java.net.URL)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 COSClient (com.qcloud.cos.COSClient)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 COSSessionCredentials (com.qcloud.cos.auth.COSSessionCredentials)1 COSSigner (com.qcloud.cos.auth.COSSigner)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 MalformedURLException (java.net.MalformedURLException)1 Map (java.util.Map)1