| | |
| | | cosSTS.setStsEndTime(cosSTS.getStsStartTime() + cosConfigProperty.getDurationSeconds() - 30); |
| | | cosSTS.setBucket(cosConfigProperty.getBucket()); |
| | | cosSTS.setRegion(cosConfigProperty.getRegion()); |
| | | cosSTS.setEndpoint(cosConfigProperty.getEndpoint()); |
| | | return cosSTS; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | * @return |
| | | */ |
| | | public String getPreviewUrl(String fileKey) { |
| | | COSClient cosClient = this.initClient(); |
| | | // 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时) |
| | | // 这里设置签名在半个小时后过期 |
| | | Date expirationDate = new Date(System.currentTimeMillis() + cosConfigProperty.getUrlExpireMinute() * 60 * 1000); |
| | | // 填写本次请求的参数,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的参数 |
| | | Map<String, String> params = new HashMap<String, String>(); |
| | | // 填写本次请求的头部,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的头部 |
| | | Map<String, String> headers = new HashMap<String, String>(); |
| | | // 请求的 HTTP 方法,上传请求用 PUT,下载请求用 GET,删除请求用 DELETE |
| | | HttpMethodName method = HttpMethodName.GET; |
| | | URL url = cosClient.generatePresignedUrl(cosConfigProperty.getBucket(), fileKey, expirationDate, method, headers, params); |
| | | System.out.println(url.toString()); |
| | | // 确认本进程不再使用 cosClient 实例之后,关闭即可 |
| | | cosClient.shutdown(); |
| | | return url.toString(); |
| | | return cosConfigProperty.getEndpoint() + "/" + fileKey; |
| | | } |
| | | |
| | | |