阿里云与华为云下载时增加Content-disposition:文件名称

This commit is contained in:
xudawei 2024-04-06 13:08:22 +08:00
parent dfc692ca33
commit 42417e2a83
7 changed files with 26 additions and 17 deletions

View File

@ -13,7 +13,7 @@ public interface AliOssService extends BaseS3Service {
/**
* 授权给第三方下载
*/
String downloadSignUrl(String bucketName, String key, Long expireSecond);
String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName);
/**
* 授权给第三方-上传

View File

@ -20,7 +20,7 @@ public interface HuaWeiCloudService {
/**
* 授权给第三方-下载
*/
String downloadSignUrl(String bucketName, String key, Long expireSecond);
String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName);
/**
* 授权给第三方-上传

View File

@ -272,12 +272,17 @@ public class AliOssServiceImpl implements AliOssService {
* 授权给第三方-下载
*/
@Override
public String downloadSignUrl(String bucketName, String key, Long expireSecond) {
public String downloadSignUrl(String bucketName, String key, Long expireSecond, String fileName) {
Date date = new Date();
date.setTime(date.getTime() + expireSecond * 1000);
try {
log.info("aliyun downloadSignUrl params, bucketName:{}, key:{}, date:{}", bucketName, key, date);
URL url = aliOssClient.getClient().generatePresignedUrl(bucketName, key, date);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
request.setExpiration(date);
ResponseHeaderOverrides responseHeaderOverrides = new ResponseHeaderOverrides();
responseHeaderOverrides.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
request.setResponseHeaders(responseHeaderOverrides);
URL url = aliOssClient.getClient().generatePresignedUrl(request);
log.info("aliyun downloadSignUrl result, bucketName:{}, key:{}, url:{}", bucketName, key, JsonUtil.obj2Str(url));
return url.toString();
} catch (Exception e) {

View File

@ -8,6 +8,7 @@ import cn.axzo.oss.integration.s3.client.HuaWeiCloudObsClient;
import cn.axzo.oss.integration.s3.config.HuaWeiCloudObsConfig;
import cn.azxo.framework.common.utils.LogUtil;
import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Maps;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
@ -235,11 +236,14 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
* 授权给第三方-下载
*/
@Override
public String downloadSignUrl(String bucketName, String key, Long expireSeconds) {
public String downloadSignUrl(String bucketName, String key, Long expireSeconds, String fileName) {
TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
request.setBucketName(bucketName);
request.setObjectKey(key);
Map<String, Object> queryParams = Maps.newHashMap();
try {
queryParams.put("response-content-disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
request.setQueryParams(queryParams);
log.info("huawei cloud downloadSignUrl params, bucketName:{}, key:{}, request:{}", bucketName, key, JsonUtil.obj2Str(request));
TemporarySignatureResponse response = huaWeiCloudObsClient.getClient().createTemporarySignature(request);
log.info("huawei cloud downloadSignUrl result, bucketName:{}, key:{}, request:{}", bucketName, key, JsonUtil.obj2Str(response));

View File

@ -56,7 +56,7 @@ public interface FileManager {
/**
* 根据华为云/阿里云授权给第三方下载
*/
String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode);
String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode, String fileName);
/**
* 根据华为云/阿里云授权给第三方上传

View File

@ -135,13 +135,13 @@ public class FileManagerImpl implements FileManager {
* 根据华为云/阿里云授权给第三方下载
*/
@Override
public String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode) {
public String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode, String fileName) {
ChannelTypeEnum typeEnum = ChannelTypeEnum.getChannelTypeByChannelCode(channelCode);
switch (typeEnum) {
case OBS:// 华为云
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond);
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond, fileName);
case OSS:// 阿里云
return aliOssService.downloadSignUrl(bucketName, key, expireSecond);
return aliOssService.downloadSignUrl(bucketName, key, expireSecond, fileName);
default:
BizException.error(CodeEnum.CHANNEL_TYPE_NOT_EXIST);
}
@ -163,13 +163,13 @@ public class FileManagerImpl implements FileManager {
.host(response.getActualSignedRequestHeaders().get(HUAWEI_CLOUD_SIGNURL_UPLOAD_HOST_KEY))
.contentType(response.getActualSignedRequestHeaders().get(HUAWEI_CLOUD_SIGNURL_UPLOAD_CONTENT_TYPE_KEY))
.channelCode(channelCode)
.downloadSignUrl(this.downloadHuaweiSignUrl(bucketName, key, expireSecond, bucketType))
.downloadSignUrl(this.downloadHuaweiSignUrl(bucketName, key, expireSecond, bucketType, fileName))
.build();
case OSS:// 阿里云
return SignUrlUploadVo.builder()
.signUrl(aliOssService.uploadSignUrl(bucketName, key, fileName,expireSecond))
.channelCode(channelCode)
.downloadSignUrl(this.downloadAliyunSignUrl(bucketName, key, expireSecond, bucketType))
.downloadSignUrl(this.downloadAliyunSignUrl(bucketName, key, expireSecond, bucketType, fileName))
.build();
default:
BizException.error(CodeEnum.CHANNEL_TYPE_NOT_EXIST);
@ -180,10 +180,10 @@ public class FileManagerImpl implements FileManager {
/**
* 构建华为云下载地址
*/
private String downloadHuaweiSignUrl(String bucketName, String key, Long expireSecond, String bucketType) {
private String downloadHuaweiSignUrl(String bucketName, String key, Long expireSecond, String bucketType, String fileName) {
switch (BucketTypeEnum.getByCode(bucketType)) {
case PRIVATE_BUCKET:
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond);
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond, fileName);
case PUBLIC_BUCKET:
return huaWeiCloudService.getUrl(bucketName, key);
default:
@ -195,10 +195,10 @@ public class FileManagerImpl implements FileManager {
/**
* 构建阿里云下载地址
*/
private String downloadAliyunSignUrl(String bucketName, String key, Long expireSecond, String bucketType) {
private String downloadAliyunSignUrl(String bucketName, String key, Long expireSecond, String bucketType, String fileName) {
switch (BucketTypeEnum.getByCode(bucketType)) {
case PRIVATE_BUCKET:
return aliOssService.downloadSignUrl(bucketName, key, expireSecond);
return aliOssService.downloadSignUrl(bucketName, key, expireSecond, fileName);
case PUBLIC_BUCKET:
return aliOssService.getUrl(bucketName, key);
default:

View File

@ -463,7 +463,7 @@ public class FileServiceImpl implements FileService {
response.setFileKey(fileKey);
if (Objects.nonNull(bucketTypeMap.get(file.getAppChannelBucketNo())) && BucketTypeEnum.PRIVATE_BUCKET.getCode().equals(bucketTypeMap.get(file.getAppChannelBucketNo()))) {
String tgtFileKey = Utility.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
response.setUrl(fileManager.signUrlDownload(file.getBucketName(), tgtFileKey, SIGN_URL_DOWNLOAD_EXPIRE_SECOND, file.getChannelCode()));
response.setUrl(fileManager.signUrlDownload(file.getBucketName(), tgtFileKey, SIGN_URL_DOWNLOAD_EXPIRE_SECOND, file.getChannelCode(), file.getFileName()));
} else {
response.setUrl(fileUrl);
}
@ -784,7 +784,7 @@ public class FileServiceImpl implements FileService {
.fileKey(item.getFileUuid())
.build();
case PRIVATE_BUCKET:
String signUrl = this.fileManager.signUrlDownload(item.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , item.getChannelCode());
String signUrl = this.fileManager.signUrlDownload(item.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , item.getChannelCode(), item.getFileName());
return SignUrlDownloadResponse.builder()
.signUrl(signUrl)
.fileKey(item.getFileUuid())