Search in sources :

Example 1 with GeneratePresignedUrlRequest

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

the class GeneratePresignedUrlDemo method GeneratePresignedDownloadUrlAnonymous.

// 获取预签名的下载链接, 用于匿名bucket, 匿名bucket生成的预下载链接不包含签名
public static void GeneratePresignedDownloadUrlAnonymous() {
    // 1 初始化用户身份信息, 匿名身份不用传入ak sk
    COSCredentials cred = new AnonymousCOSCredentials();
    // 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);
    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) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig) URL(java.net.URL)

Example 2 with GeneratePresignedUrlRequest

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

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

use of com.qcloud.cos.model.GeneratePresignedUrlRequest in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method genPresignedDownloadUrl.

/**
 * {@inheritDoc}
 */
@Override
public URL genPresignedDownloadUrl(String bucketName, String objectName, long signExpired) throws CloudSDKException {
    try {
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethodName.GET);
        Date expirationDate = new Date(System.currentTimeMillis() + signExpired);
        req.setExpiration(expirationDate);
        URL downloadUrl = cosClient.generatePresignedUrl(req);
        return downloadUrl;
    } catch (Throwable e) {
        throw new CloudSDKException(e.getMessage(), e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) Date(java.util.Date) URL(java.net.URL)

Example 5 with GeneratePresignedUrlRequest

use of com.qcloud.cos.model.GeneratePresignedUrlRequest in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method genPresignedUploadUrl.

/**
 * {@inheritDoc}
 */
@Override
public URL genPresignedUploadUrl(String bucketName, String objectName, long signExpired, String contentMd5) throws CloudSDKException {
    try {
        GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethodName.PUT);
        req.setContentMd5(contentMd5);
        Date expirationDate = new Date(System.currentTimeMillis() + signExpired);
        req.setExpiration(expirationDate);
        URL downloadUrl = cosClient.generatePresignedUrl(req);
        return downloadUrl;
    } catch (Throwable e) {
        throw new CloudSDKException(e.getMessage(), e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) GeneratePresignedUrlRequest(com.qcloud.cos.model.GeneratePresignedUrlRequest) Date(java.util.Date) URL(java.net.URL)

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