use of com.qcloud.cos.COSClient in project xin-open-enterprise-platform by hui0xin.
the class CosFileUploadService method downFile.
/**
* 下载文件
*
* @param downFile 本地存放文件路径
* @param fileName cos上文件名称
*/
public Boolean downFile(File downFile, String fileName) {
Boolean result = false;
// 生成 cos 客户端。
COSClient cosClient = new COSClient(cred, clientConfig);
try {
// file中截取文件名称
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileName);
cosClient.getObject(getObjectRequest, downFile);
result = true;
} catch (Exception e) {
log.error("文件下载失败:{}", ExceptionUtils.getStackTrace(e));
} finally {
cosClient.shutdown();
}
return result;
}
use of com.qcloud.cos.COSClient in project xin-open-enterprise-platform by hui0xin.
the class CosFileUploadService method uploadFile.
/**
* 文件上传 将本地文件上传到 COS
*
* @param localFile 本地上传文件路径
* @param fileName 文件名称
*/
public Boolean uploadFile(File localFile, String fileName) {
Boolean result = false;
// 生成 cos 客户端。
COSClient cosClient = new COSClient(cred, clientConfig);
try {
cosClient.putObject(bucketName, fileName, localFile);
result = true;
} catch (Exception e) {
log.error("文件上传失败:{}", ExceptionUtils.getStackTrace(e));
} finally {
// 关闭客户端(关闭后台线程)
cosClient.shutdown();
}
return result;
}
use of com.qcloud.cos.COSClient in project xin-open-enterprise-platform by hui0xin.
the class CosFileUploadService method deleteFile.
/**
* 删除文件
*
* @param fileName cos上文件名称
*/
public Boolean deleteFile(String fileName) {
Boolean result = false;
// 生成 cos 客户端。
COSClient cosClient = new COSClient(cred, clientConfig);
try {
// file中截取文件名称
cosClient.deleteObject(bucketName, fileName);
result = true;
} catch (Exception e) {
log.error("文件下载失败:{}", ExceptionUtils.getStackTrace(e));
} finally {
cosClient.shutdown();
}
return result;
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class CAMRoleDemo method SimpleUploadFileFromLocal.
public static void SimpleUploadFileFromLocal() {
InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.CVM);
InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
COSClient cosClient = new COSClient(cosCredentialsProvider, clientConfig);
String bucketName = "3399demo-125xxxxxxxx";
String key = "test/demo.txt";
File localFile = new File("test");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
cosClient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class CopyFileDemo method copySmallFileDemo.
// copyObject最大支持5G文件的copy, 5G以上的文件copy请参照TransferManagerDemo中的copy示例
public static void copySmallFileDemo() {
// 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 region, 支持跨园区拷贝
Region srcBucketRegion = new Region("ap-shanghai");
// 源bucket, bucket名需包含appid
String srcBucketName = "srcBucket-1251668577";
// 要拷贝的源文件
String srcKey = "aaa/bbb.txt";
// 目的bucket, bucket名需包含appid
String destBucketName = "destBucket-1251668577";
// 要拷贝的目的文件
String destKey = "ccc/ddd.txt";
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
try {
CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
String crc64 = copyObjectResult.getCrc64Ecma();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
cosclient.shutdown();
}
Aggregations