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());
}
Aggregations