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