Search in sources :

Example 6 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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 7 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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 8 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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)

Example 9 with BasicCOSCredentials

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

the class GetObjectDemo method GetObjectToFileDemo.

public static void GetObjectToFileDemo() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test.json";
    String bucketName = "mybucket-1251668577";
    boolean useTrafficLimit = false;
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    if (useTrafficLimit) {
        getObjectRequest.setTrafficLimit(8 * 1024 * 1024);
    }
    File localFile = new File("my_test.json");
    ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localFile);
    System.out.println(objectMetadata.getContentLength());
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 10 with BasicCOSCredentials

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

the class RestoreObjectDemo method restoreObjectDemo.

public static void restoreObjectDemo() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_data.txt";
    String bucketName = "mybucket-1251668577";
    // 上传一个类型为归档的文件
    File localFile = new File("test/my_data.txt");
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setHeader("x-cos-storage-class", "Archive");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.setMetadata(metadata);
    PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
    // 设置restore得到的临时副本过期天数为1天
    RestoreObjectRequest restoreObjectRequest = new RestoreObjectRequest(bucketName, key, 1);
    // 设置恢复模式为Standard,其他的可选模式包括Expedited和Bulk,三种恢复模式在费用和速度上不一样
    CASJobParameters casJobParameters = new CASJobParameters();
    casJobParameters.setTier(Tier.Standard);
    restoreObjectRequest.setCASJobParameters(casJobParameters);
    cosclient.restoreObject(restoreObjectRequest);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File)

Aggregations

BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)103 Region (com.qcloud.cos.region.Region)99 ClientConfig (com.qcloud.cos.ClientConfig)94 COSClient (com.qcloud.cos.COSClient)91 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)21 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)11 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 LinkedList (java.util.LinkedList)7 FileOperationException (run.halo.app.exception.FileOperationException)6 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)5