Search in sources :

Example 36 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class MultipartUploadDemo method InitMultipartUploadDemo.

public static String InitMultipartUploadDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    // 设置存储类型, 默认是标准(Standard), 低频(Standard_IA), 归档(Archive)
    request.setStorageClass(StorageClass.Standard);
    try {
        InitiateMultipartUploadResult initResult = cosclient.initiateMultipartUpload(request);
        // 获取uploadid
        String uploadId = initResult.getUploadId();
        return uploadId;
    } catch (CosServiceException e) {
        throw e;
    } catch (CosClientException e) {
        throw e;
    } finally {
        cosclient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 37 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testRequestSpecifiedKeyInfoPutGetDel.

@Test
public void testRequestSpecifiedKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
    COSClient cosclient = new COSClient(new AnonymousCOSCredentials(), clientConfig);
    File localFile = buildTestFile(1024L);
    COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
    try {
        String key = "ut/request-specified-key";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        putObjectRequest.setCosCredentials(cosCredentials);
        cosclient.putObject(putObjectRequest);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        getObjectRequest.setCosCredentials(cosCredentials);
        cosclient.getObject(getObjectRequest);
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
        deleteObjectRequest.setCosCredentials(cosCredentials);
        cosclient.deleteObject(deleteObjectRequest);
    } finally {
        localFile.delete();
        cosclient.shutdown();
    }
}
Also used : DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 38 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopySmallFileFromSameRegion.

// transfer manager对相同园区使用put object copy
@Ignore
public void testTransferManagerCopySmallFileFromSameRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = region;
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = bucket;
    String srcKey = "ut_copy/len1G.txt";
    String destKey = "ut_copy_dest/len1G_2.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    CopyResult copyResult = copy.waitForCopyResult();
    assertNotNull(copyResult.getRequestId());
    assertNotNull(copyResult.getDateStr());
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) CopyResult(com.qcloud.cos.model.CopyResult) Ignore(org.junit.Ignore)

Example 39 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopyBigFileFromDiffRegion.

// transfer manager对不同园区5G以上文件进行分块拷贝
@Ignore
public void testTransferManagerCopyBigFileFromDiffRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = "ap-guangzhou";
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = "chengwus3gz-1251668577";
    String srcKey = "ut_copy/len10G_1.txt";
    String destKey = "ut_copy_dest/len10G_1.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    copy.waitForCompletion();
    clearObject(destKey);
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) Ignore(org.junit.Ignore)

Example 40 with COSCredentials

use of com.qcloud.cos.auth.COSCredentials 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)

Aggregations

COSCredentials (com.qcloud.cos.auth.COSCredentials)112 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)105 Region (com.qcloud.cos.region.Region)105 ClientConfig (com.qcloud.cos.ClientConfig)99 COSClient (com.qcloud.cos.COSClient)96 CosClientException (com.qcloud.cos.exception.CosClientException)42 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)11 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)10 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 Date (java.util.Date)7 LinkedList (java.util.LinkedList)7