Search in sources :

Example 6 with COSClient

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;
}
Also used : COSClient(com.qcloud.cos.COSClient)

Example 7 with COSClient

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;
}
Also used : COSClient(com.qcloud.cos.COSClient)

Example 8 with COSClient

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;
}
Also used : COSClient(com.qcloud.cos.COSClient)

Example 9 with COSClient

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 10 with COSClient

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

COSClient (com.qcloud.cos.COSClient)142 ClientConfig (com.qcloud.cos.ClientConfig)103 Region (com.qcloud.cos.region.Region)99 COSCredentials (com.qcloud.cos.auth.COSCredentials)96 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)93 CosClientException (com.qcloud.cos.exception.CosClientException)38 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)15 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)7 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6 URL (java.net.URL)6 FileOperationException (run.halo.app.exception.FileOperationException)6 Date (java.util.Date)5