Search in sources :

Example 41 with COSClient

use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.

the class ObjectTaggingDemo method SetGetDelObjectTaggingDemo.

public static void SetGetDelObjectTaggingDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRET_ID", "COS_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);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    cosclient.putObject(bucketName, key, "data");
    List<Tag> tags = new LinkedList<>();
    tags.add(new Tag("key", "value"));
    tags.add(new Tag("key-1", "value-1"));
    ObjectTagging objectTagging = new ObjectTagging(tags);
    SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(bucketName, key, objectTagging);
    cosclient.setObjectTagging(setObjectTaggingRequest);
    GetObjectTaggingResult getObjectTaggingResult = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucketName, key));
    List<Tag> resultTagSet = getObjectTaggingResult.getTagSet();
    cosclient.deleteObjectTagging(new DeleteObjectTaggingRequest(bucketName, key));
    GetObjectTaggingResult getObjectTaggingResultSecond = cosclient.getObjectTagging(new GetObjectTaggingRequest(bucketName, key));
    List<Tag> resultTagSetSecond = getObjectTaggingResultSecond.getTagSet();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) LinkedList(java.util.LinkedList) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) Tag(com.qcloud.cos.model.Tag.Tag) ClientConfig(com.qcloud.cos.ClientConfig)

Example 42 with COSClient

use of com.qcloud.cos.COSClient 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();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 43 with COSClient

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

Example 44 with COSClient

use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.

the class SimpleUploadFileDemo method SimpleUploadFileFromLocal.

// 将本地文件上传到COS
public static void SimpleUploadFileFromLocal(boolean useTrafficLimit) {
    // 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";
    String key = "aaa/bbb.txt";
    File localFile = new File("src/test/resources/len10M.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    if (useTrafficLimit) {
        // 限流使用的单位是bit/s, 这里测试1MB/s的上传带宽限制
        putObjectRequest.setTrafficLimit(8 * 1024 * 1024);
    }
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
        String crc64 = putObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 45 with COSClient

use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.

the class SimpleUploadFileDemo method SimpleUploadFileFromStream.

// 从输入流进行读取并上传到COS
public static void SimpleUploadFileFromStream() {
    // 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";
    String key = "aaa/bbb.jpg";
    File localFile = new File("src/test/resources/len10M.txt");
    InputStream input = new ByteArrayInputStream(new byte[10]);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
    objectMetadata.setContentLength(10);
    // 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
    objectMetadata.setContentType("image/jpeg");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata);
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

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