use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class COSClient method setBucketCrossOriginConfiguration.
@Override
public void setBucketCrossOriginConfiguration(SetBucketCrossOriginConfigurationRequest setBucketCrossOriginConfigurationRequest) throws CosClientException, CosServiceException {
rejectNull(setBucketCrossOriginConfigurationRequest, "The set bucket cross origin configuration request object must be specified.");
rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when setting bucket cross origin configuration");
String bucketName = setBucketCrossOriginConfigurationRequest.getBucketName();
BucketCrossOriginConfiguration bucketCrossOriginConfiguration = setBucketCrossOriginConfigurationRequest.getCrossOriginConfiguration();
rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket cross origin configuration.");
rejectNull(bucketCrossOriginConfiguration, "The cross origin configuration parameter must be specified when setting bucket cross origin configuration.");
CosHttpRequest<SetBucketCrossOriginConfigurationRequest> request = createRequest(bucketName, null, setBucketCrossOriginConfigurationRequest, HttpMethodName.PUT);
request.addParameter("cors", null);
byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketCrossOriginConfiguration);
request.addHeader("Content-Length", String.valueOf(content.length));
request.addHeader("Content-Type", "application/xml");
request.setContent(new ByteArrayInputStream(content));
try {
byte[] md5 = Md5Utils.computeMD5Hash(content);
String md5Base64 = BinaryUtils.toBase64(md5);
request.addHeader("Content-MD5", md5Base64);
} catch (Exception e) {
throw new CosClientException("Couldn't compute md5 sum", e);
}
invoke(request, voidCosResponseHandler);
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class COSClient method setBucketReplicationConfiguration.
@Override
public void setBucketReplicationConfiguration(SetBucketReplicationConfigurationRequest setBucketReplicationConfigurationRequest) throws CosClientException, CosServiceException {
rejectNull(setBucketReplicationConfigurationRequest, "The set bucket replication configuration request object must be specified.");
rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when setting bucket replication configuration");
final String bucketName = setBucketReplicationConfigurationRequest.getBucketName();
final BucketReplicationConfiguration bucketReplicationConfiguration = setBucketReplicationConfigurationRequest.getReplicationConfiguration();
rejectNull(bucketName, "The bucket name parameter must be specified when setting replication configuration.");
rejectNull(bucketReplicationConfiguration, "The replication configuration parameter must be specified when setting replication configuration.");
CosHttpRequest<SetBucketReplicationConfigurationRequest> request = createRequest(bucketName, null, setBucketReplicationConfigurationRequest, HttpMethodName.PUT);
request.addParameter("replication", null);
final byte[] bytes = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketReplicationConfiguration);
request.addHeader("Content-Length", String.valueOf(bytes.length));
request.addHeader("Content-Type", "application/xml");
request.setContent(new ByteArrayInputStream(bytes));
try {
request.addHeader("Content-MD5", BinaryUtils.toBase64(Md5Utils.computeMD5Hash(bytes)));
} catch (Exception e) {
throw new CosClientException("Not able to compute MD5 of the replication rule configuration. Exception Message : " + e.getMessage(), e);
}
invoke(request, voidCosResponseHandler);
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class COSClient method setBucketLifecycleConfiguration.
@Override
public void setBucketLifecycleConfiguration(SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest) throws CosClientException, CosServiceException {
rejectNull(setBucketLifecycleConfigurationRequest, "The set bucket lifecycle configuration request object must be specified.");
String bucketName = setBucketLifecycleConfigurationRequest.getBucketName();
BucketLifecycleConfiguration bucketLifecycleConfiguration = setBucketLifecycleConfigurationRequest.getLifecycleConfiguration();
rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket lifecycle configuration.");
rejectNull(bucketLifecycleConfiguration, "The lifecycle configuration parameter must be specified when setting bucket lifecycle configuration.");
rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when setting bucket lifecycle configuration");
CosHttpRequest<SetBucketLifecycleConfigurationRequest> request = createRequest(bucketName, null, setBucketLifecycleConfigurationRequest, HttpMethodName.PUT);
request.addParameter("lifecycle", null);
byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketLifecycleConfiguration);
request.addHeader("Content-Length", String.valueOf(content.length));
request.addHeader("Content-Type", "application/xml");
request.setContent(new ByteArrayInputStream(content));
try {
byte[] md5 = Md5Utils.computeMD5Hash(content);
String md5Base64 = BinaryUtils.toBase64(md5);
request.addHeader("Content-MD5", md5Base64);
} catch (Exception e) {
throw new CosClientException("Couldn't compute md5 sum", e);
}
invoke(request, voidCosResponseHandler);
}
use of com.qcloud.cos.exception.CosClientException 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());
}
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class COSClient method restoreObject.
@Override
public void restoreObject(RestoreObjectRequest restoreObjectRequest) throws CosClientException, CosServiceException {
rejectNull(restoreObjectRequest, "The RestoreObjectRequest parameter must be specified when restore a object.");
String bucketName = restoreObjectRequest.getBucketName();
String key = restoreObjectRequest.getKey();
String versionId = restoreObjectRequest.getVersionId();
int expirationIndays = restoreObjectRequest.getExpirationInDays();
rejectNull(bucketName, "The bucket name parameter must be specified when copying a cas object");
rejectNull(key, "The key parameter must be specified when copying a cas object");
if (expirationIndays == -1) {
throw new IllegalArgumentException("The expiration in days parameter must be specified when copying a cas object");
}
CosHttpRequest<RestoreObjectRequest> request = createRequest(bucketName, key, restoreObjectRequest, HttpMethodName.POST);
request.addParameter("restore", null);
addParameterIfNotNull(request, "versionId", versionId);
byte[] content = RequestXmlFactory.convertToXmlByteArray(restoreObjectRequest);
request.addHeader("Content-Length", String.valueOf(content.length));
request.addHeader("Content-Type", "application/xml");
request.setContent(new ByteArrayInputStream(content));
try {
byte[] md5 = Md5Utils.computeMD5Hash(content);
String md5Base64 = BinaryUtils.toBase64(md5);
request.addHeader("Content-MD5", md5Base64);
} catch (Exception e) {
throw new CosClientException("Couldn't compute md5 sum", e);
}
invoke(request, voidCosResponseHandler);
}
Aggregations