华为云临时授权加上style处理

This commit is contained in:
xudawei 2024-05-09 11:06:50 +08:00
parent 0c338c4c23
commit cd7d28ebd5
8 changed files with 23 additions and 9 deletions

View File

@ -32,4 +32,9 @@ public class ApiSignUrlDownloadRequest {
*/
private String bizScene;
/**
* 图片样式比如x-oss-process=image/auto-orient,1/resize,p_50/quality,q_30
*/
private String style;
}

View File

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

View File

@ -17,6 +17,7 @@ import com.obs.services.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.InputStream;
import java.net.URL;
@ -332,13 +333,16 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
* 授权给第三方-下载
*/
@Override
public String downloadSignUrl(String bucketName, String key, Long expireSeconds, String fileName) {
public String downloadSignUrl(String bucketName, String key, Long expireSeconds, String fileName, String style) {
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") + "\"");
if (StringUtils.hasText(style)) {
queryParams.put("x-image-process", "image/auto-orient,1/resize,p_50/quality,q_30");
}
request.setQueryParams(queryParams);
log.info("huawei cloud downloadSignUrl params, bucketName:{}, key:{}, request:{}", bucketName, key, JsonUtil.obj2Str(request));
TemporarySignatureResponse response = huaWeiCloudObsClient.getClient().createTemporarySignature(request);

View File

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

View File

@ -34,4 +34,9 @@ public class SignUrlDownloadDto {
*/
private String bizScene;
/**
* 图片样式比如x-oss-process=image/auto-orient,1/resize,p_50/quality,q_30
*/
private String style;
}

View File

@ -148,11 +148,11 @@ public class FileManagerImpl implements FileManager {
* 根据华为云/阿里云授权给第三方下载
*/
@Override
public String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode, String fileName) {
public String signUrlDownload(String bucketName, String key, Long expireSecond, String channelCode, String fileName, String style) {
ChannelTypeEnum typeEnum = ChannelTypeEnum.getChannelTypeByChannelCode(channelCode);
switch (typeEnum) {
case OBS:// 华为云
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond, fileName);
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond, fileName, style);
case OSS:// 阿里云
return aliOssService.downloadSignUrl(bucketName, key, expireSecond, fileName);
default:
@ -196,7 +196,7 @@ public class FileManagerImpl implements FileManager {
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, fileName);
return huaWeiCloudService.downloadSignUrl(bucketName, key, expireSecond, fileName, null);
case PUBLIC_BUCKET:
return huaWeiCloudService.getUrl(bucketName, key);
default:

View File

@ -253,7 +253,7 @@ public class FileByUrlServiceImpl implements FileByUrlService {
fileUrl = this.fileManager.fetchDownloadUrl(fileUploadConfig.getBucketName(), tgtFileKey, channelCode);
break;
case PRIVATE_BUCKET:
fileUrl = this.fileManager.signUrlDownload(fileUploadConfig.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , channelCode, fileName);
fileUrl = this.fileManager.signUrlDownload(fileUploadConfig.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , channelCode, fileName, null);
break;
default:
BizException.error(CodeEnum.CHANNEL_TYPE_NOT_EXIST);

View File

@ -501,7 +501,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(), file.getFileName()));
response.setUrl(fileManager.signUrlDownload(file.getBucketName(), tgtFileKey, SIGN_URL_DOWNLOAD_EXPIRE_SECOND, file.getChannelCode(), file.getFileName(), null));
} else {
response.setUrl(fileUrl);
}
@ -857,7 +857,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(), item.getFileName());
String signUrl = this.fileManager.signUrlDownload(item.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , item.getChannelCode(), item.getFileName(), dto.getStyle());
return SignUrlDownloadResponse.builder()
.signUrl(UrlUtil.httpToHttps(signUrl))
.fileKey(item.getFileUuid())