Search in sources :

Example 6 with Region

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

the class DelFileDemo method BatchDelFileWithVersion.

// 批量删除带有版本号的文件(即bucket开启了多版本)
public static void BatchDelFileWithVersion() {
    // 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";
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    // 设置要删除的key列表, 最多一次删除1000个
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    // 传入要删除的文件名
    keyList.add(new KeyVersion("aaa.txt", "axbefagagaxxfafa"));
    keyList.add(new KeyVersion("bbb.mp4", "awcafa1faxg0lx"));
    keyList.add(new KeyVersion("ccc/ddd.jpg", "kafa1kxxaa2ymh"));
    deleteObjectsRequest.setKeys(keyList);
    // 批量删除文件
    try {
        DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
        List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
    } catch (MultiObjectDeleteException mde) {
        // 如果部分产出成功部分失败, 返回MultiObjectDeleteException
        List<DeletedObject> deleteObjects = mde.getDeletedObjects();
        List<DeleteError> deleteErrors = mde.getErrors();
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) Region(com.qcloud.cos.region.Region) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(com.qcloud.cos.ClientConfig) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject)

Example 7 with Region

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

the class GeneratePresignedUrlDemo method GeneratePresignedDownloadUrlAnonymous.

// 获取预签名的下载链接, 用于匿名bucket, 匿名bucket生成的预下载链接不包含签名
public static void GeneratePresignedDownloadUrlAnonymous() {
    // 1 初始化用户身份信息, 匿名身份不用传入ak sk
    COSCredentials cred = new AnonymousCOSCredentials();
    // 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.txt";
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
    URL url = cosclient.generatePresignedUrl(req);
    System.out.println(url.toString());
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig) URL(java.net.URL)

Example 8 with Region

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

the class GeneratePresignedUrlDemo method GeneratePresignedDownloadUrlWithOverrideResponseHeader.

// 获取预签名的下载链接, 并设置返回的content-type, cache-control等http头
public static void GeneratePresignedDownloadUrlWithOverrideResponseHeader() {
    // 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.txt";
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
    // 设置下载时返回的http头
    ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
    String responseContentType = "image/x-icon";
    String responseContentLanguage = "zh-CN";
    String responseContentDispositon = "filename=\"abc.txt\"";
    String responseCacheControl = "no-cache";
    String cacheExpireStr = DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24 * 3600 * 1000));
    responseHeaders.setContentType(responseContentType);
    responseHeaders.setContentLanguage(responseContentLanguage);
    responseHeaders.setContentDisposition(responseContentDispositon);
    responseHeaders.setCacheControl(responseCacheControl);
    responseHeaders.setExpires(cacheExpireStr);
    req.setResponseHeaders(responseHeaders);
    // 设置签名过期时间(可选), 若未进行设置则默认使用ClientConfig中的签名过期时间(1小时)
    // 这里设置签名在半个小时后过期
    Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    req.setExpiration(expirationDate);
    // 填写本次请求的参数
    req.addRequestParameter("param1", "value1");
    // 填写本次请求的头部。Host 头部会自动补全,不需要填写
    req.putCustomRequestHeader("header1", "value1");
    URL url = cosclient.generatePresignedUrl(req);
    System.out.println(url.toString());
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ResponseHeaderOverrides(com.qcloud.cos.model.ResponseHeaderOverrides) ClientConfig(com.qcloud.cos.ClientConfig) Date(java.util.Date) URL(java.net.URL)

Example 9 with Region

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

the class GeneratePresignedUrlDemo method GenerateSimplePresignedDownloadUrl.

// 获取下载的预签名连接
public static void GenerateSimplePresignedDownloadUrl() {
    // 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"));
    // 如果要获取 https 的 url 则在此设置,否则默认获取的是 http url
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa.txt";
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
    // 设置签名过期时间(可选), 若未进行设置则默认使用ClientConfig中的签名过期时间(1小时)
    // 这里设置签名在半个小时后过期
    Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    req.setExpiration(expirationDate);
    // 填写本次请求的参数
    req.addRequestParameter("param1", "value1");
    // 填写本次请求的头部。Host 头部会自动补全,不需要填写
    req.putCustomRequestHeader("header1", "value1");
    URL url = cosclient.generatePresignedUrl(req);
    System.out.println(url.toString());
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) Date(java.util.Date) URL(java.net.URL)

Example 10 with Region

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

the class GeneratePresignedUrlDemo method GeneratePresignedUploadUrl.

// 生成预签名的上传连接
public static void GeneratePresignedUploadUrl() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "********************************");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa.txt";
    Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
    // 填写本次请求的 header。Host 头部会自动补全,只需填入其他头部
    Map<String, String> headers = new HashMap<String, String>();
    // 填写本次请求的 params。
    Map<String, String> params = new HashMap<String, String>();
    URL url = cosclient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT, headers, params);
    try {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        // 写入要上传的数据
        out.write("This text uploaded as object.");
        out.close();
        int responseCode = connection.getResponseCode();
        System.out.println("Service returned response code " + responseCode);
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : ProtocolException(java.net.ProtocolException) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) URL(java.net.URL) COSClient(com.qcloud.cos.COSClient) HttpURLConnection(java.net.HttpURLConnection) Region(com.qcloud.cos.region.Region) OutputStreamWriter(java.io.OutputStreamWriter) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

Region (com.qcloud.cos.region.Region)121 COSCredentials (com.qcloud.cos.auth.COSCredentials)105 ClientConfig (com.qcloud.cos.ClientConfig)103 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)101 COSClient (com.qcloud.cos.COSClient)99 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)23 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 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 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)8 Copy (com.qcloud.cos.transfer.Copy)8 Test (org.junit.Test)8 CopyResult (com.qcloud.cos.model.CopyResult)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6