Search in sources :

Example 6 with GeneratePresignedUrlRequest

use of com.qcloud.cos.model.GeneratePresignedUrlRequest 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

GeneratePresignedUrlRequest (com.qcloud.cos.model.GeneratePresignedUrlRequest)6 URL (java.net.URL)6 Date (java.util.Date)5 COSClient (com.qcloud.cos.COSClient)3 ClientConfig (com.qcloud.cos.ClientConfig)3 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)3 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)3 COSCredentials (com.qcloud.cos.auth.COSCredentials)3 Region (com.qcloud.cos.region.Region)3 CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)2 ResponseHeaderOverrides (com.qcloud.cos.model.ResponseHeaderOverrides)2 File (java.io.File)1 HttpURLConnection (java.net.HttpURLConnection)1 Test (org.junit.Test)1