Search in sources :

Example 1 with ResponseHeaderOverrides

use of com.qcloud.cos.model.ResponseHeaderOverrides 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 2 with ResponseHeaderOverrides

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

the class AbstractCOSClientTest method getObject.

// 下载COS的object
protected static void getObject(String key, File localDownFile, long[] range, long expectedLength, String expectedMd5) {
    System.setProperty(SkipMd5CheckStrategy.DISABLE_GET_OBJECT_MD5_VALIDATION_PROPERTY, "true");
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
    ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
    String responseContentType = "image/x-icon";
    String responseContentLanguage = "zh-CN";
    String responseContentDispositon = "filename=\"abc.txt\"";
    String responseCacheControl = "no-cache";
    String expireStr = DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24 * 3600 * 1000));
    responseHeaders.setContentType(responseContentType);
    responseHeaders.setContentLanguage(responseContentLanguage);
    responseHeaders.setContentDisposition(responseContentDispositon);
    responseHeaders.setCacheControl(responseCacheControl);
    responseHeaders.setExpires(expireStr);
    getObjectRequest.setResponseHeaders(responseHeaders);
    if (range != null) {
        if (range[1] == range[0] && range[1] == 0) {
            assertEquals(expectedLength, 1);
            getObjectRequest.setRange(range[0], range[1]);
        } else {
            assertEquals(expectedLength, range[1] - range[0] + 1);
            getObjectRequest.setRange(range[0], range[1]);
        }
    }
    try {
        ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localDownFile);
        assertEquals(responseContentType, objectMetadata.getContentType());
        assertEquals(responseContentLanguage, objectMetadata.getContentLanguage());
        assertEquals(responseContentDispositon, objectMetadata.getContentDisposition());
        assertEquals(responseCacheControl, objectMetadata.getCacheControl());
        assertEquals(expectedLength, localDownFile.length());
        assertEquals(expectedMd5, Md5Utils.md5Hex(localDownFile));
    } catch (SecurityException se) {
        if (cosclient instanceof COSEncryptionClient && cryptoConfiguration != null && cryptoConfiguration.getCryptoMode() == CryptoMode.StrictAuthenticatedEncryption && range != null) {
            return;
        }
        fail(se.toString());
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : ResponseHeaderOverrides(com.qcloud.cos.model.ResponseHeaderOverrides) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Date(java.util.Date) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException)

Example 3 with ResponseHeaderOverrides

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

the class GeneratePresignedUrlTest method testGetFile.

@Test
public void testGetFile() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    long localFileLen = 1024;
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    putObjectFromLocalFile(localFile, key);
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key, HttpMethodName.GET);
    ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
    String responseContentType = "image/x-icon";
    String responseContentLanguage = "zh-CN";
    String responseContentDispositon = "filename=\"abc.txt\"";
    String responseCacheControl = "no-cache";
    String expireStr = DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24 * 3600 * 1000));
    responseHeaders.setContentType(responseContentType);
    responseHeaders.setContentLanguage(responseContentLanguage);
    responseHeaders.setContentDisposition(responseContentDispositon);
    responseHeaders.setCacheControl(responseCacheControl);
    responseHeaders.setExpires(expireStr);
    req.setResponseHeaders(responseHeaders);
    URL url = cosclient.generatePresignedUrl(req);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    int responseCode = connection.getResponseCode();
    assertEquals(200, responseCode);
    assertEquals(responseContentType, connection.getContentType());
    assertEquals(localFileLen, connection.getContentLength());
    assertEquals(responseContentLanguage, connection.getHeaderField("Content-Language"));
    assertEquals(responseContentDispositon, connection.getHeaderField("Content-Disposition"));
    assertEquals(responseCacheControl, connection.getHeaderField("Cache-Control"));
    clearObject(key);
    assertTrue(localFile.delete());
}
Also used : GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) HttpURLConnection(java.net.HttpURLConnection) ResponseHeaderOverrides(com.qcloud.cos.model.ResponseHeaderOverrides) File(java.io.File) Date(java.util.Date) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ResponseHeaderOverrides (com.qcloud.cos.model.ResponseHeaderOverrides)3 Date (java.util.Date)3 GeneratePresignedUrlRequest (com.qcloud.cos.model.GeneratePresignedUrlRequest)2 URL (java.net.URL)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 Region (com.qcloud.cos.region.Region)1 File (java.io.File)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 Test (org.junit.Test)1