use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class ListObjectsDemo method listObjectsVersions.
public static void listObjectsVersions() {
// 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-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
ListVersionsRequest listVersionsRequest = new ListVersionsRequest();
listVersionsRequest.setBucketName(bucketName);
listVersionsRequest.setPrefix("");
VersionListing versionListing = null;
do {
try {
versionListing = cosclient.listVersions(listVersionsRequest);
} catch (CosServiceException e) {
e.printStackTrace();
return;
} catch (CosClientException e) {
e.printStackTrace();
return;
}
List<COSVersionSummary> cosVersionSummaries = versionListing.getVersionSummaries();
for (COSVersionSummary cosVersionSummary : cosVersionSummaries) {
System.out.println(cosVersionSummary.getKey() + ":" + cosVersionSummary.getVersionId());
}
String keyMarker = versionListing.getNextKeyMarker();
String versionIdMarker = versionListing.getNextVersionIdMarker();
listVersionsRequest.setKeyMarker(keyMarker);
listVersionsRequest.setVersionIdMarker(versionIdMarker);
} while (versionListing.isTruncated());
cosclient.shutdown();
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class ListObjectsDemo method listAllObjects.
// 如果要获取超过maxkey数量的object或者获取所有的object, 则需要循环调用listobject, 用上一次返回的next marker作为下一次调用的marker,
// 直到返回的truncated为false
public static void listAllObjects() {
// 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-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
// 设置bucket名称
listObjectsRequest.setBucketName(bucketName);
// prefix表示列出的object的key以prefix开始
listObjectsRequest.setPrefix("");
// deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object
listObjectsRequest.setDelimiter("");
// 设置最大遍历出多少个对象, 一次listobject最大支持1000
listObjectsRequest.setMaxKeys(1000);
ObjectListing objectListing = null;
do {
try {
objectListing = cosclient.listObjects(listObjectsRequest);
} catch (CosServiceException e) {
e.printStackTrace();
return;
} catch (CosClientException e) {
e.printStackTrace();
return;
}
// common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
List<String> commonPrefixs = objectListing.getCommonPrefixes();
// object summary表示所有列出的object列表
List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
// 文件的路径key
String key = cosObjectSummary.getKey();
// 文件的etag
String etag = cosObjectSummary.getETag();
// 文件的长度
long fileSize = cosObjectSummary.getSize();
// 文件的存储类型
String storageClasses = cosObjectSummary.getStorageClass();
}
String nextMarker = objectListing.getNextMarker();
listObjectsRequest.setMarker(nextMarker);
} while (objectListing.isTruncated());
cosclient.shutdown();
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class PicOperationDemo method PicPersistentProcessing.
public static void PicPersistentProcessing() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
// 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);
// 4. bucket名需包含appid
String bucketName = "examplebucket-1250000000";
String key = "example.jpg";
File localFile = new File("/data/example.jpg");
// 5.对图像进行持久化处理
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setHeader("Pic-Operations", "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"example.png\",\"rule\":\"imageView2/format/png\"}]}");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setMetadata(objectMetadata);
try {
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// putobjectResult会返回文件的etag
String etag = putObjectResult.getETag();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.
the class HttpCredentialsFetcher method fetch.
@Override
public COSCredentials fetch() throws CosClientException {
if (null == this.cosCredentialsEndpointProvider) {
String errorMsg = "The cos credentials endpoint provider is not specified.";
LOG.error(errorMsg);
throw new CosClientException(errorMsg);
}
try {
String credentialsResponse = InstanceCredentialsUtils.getInstance().readResource(this.cosCredentialsEndpointProvider.getCredentialsEndpoint(), this.cosCredentialsEndpointProvider.getRetryPolicy(), this.cosCredentialsEndpointProvider.getHeaders());
return parse(credentialsResponse);
} catch (URISyntaxException e) {
throw new CosClientException("The cos credentials uri is invalid.", e);
} catch (IOException e) {
String exceptionMessage = String.format("The COSCredentialsFetcher [%s] fetch an exception.", this.getClass().getName());
throw new CosClientException(exceptionMessage, e);
}
}
use of com.qcloud.cos.exception.CosClientException 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();
}
}
Aggregations