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