From b3a966b77fae63ae45b7da4d9596029fd0b187a5 Mon Sep 17 00:00:00 2001 From: zhuxiao Date: Mon, 20 Feb 2023 11:29:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E9=98=BF=E9=87=8C=E4=BA=91os?= =?UTF-8?q?s=E4=B8=8A=E4=BC=A0=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=BA=94=E5=92=8C=E6=9C=AC=E5=9C=B0=E4=B8=8A=E4=BC=A0=E6=B7=B7?= =?UTF-8?q?=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oss/service/AliyunOssService.java | 74 +++---------------- 1 file changed, 11 insertions(+), 63 deletions(-) diff --git a/jeepay-components/jeepay-components-oss/src/main/java/com/jeequan/jeepay/components/oss/service/AliyunOssService.java b/jeepay-components/jeepay-components-oss/src/main/java/com/jeequan/jeepay/components/oss/service/AliyunOssService.java index 7507302..69c294a 100644 --- a/jeepay-components/jeepay-components-oss/src/main/java/com/jeequan/jeepay/components/oss/service/AliyunOssService.java +++ b/jeepay-components/jeepay-components-oss/src/main/java/com/jeequan/jeepay/components/oss/service/AliyunOssService.java @@ -19,7 +19,6 @@ import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.GetObjectRequest; import com.jeequan.jeepay.components.oss.config.AliyunOssYmlConfig; -import com.jeequan.jeepay.components.oss.config.OssYmlConfig; import com.jeequan.jeepay.components.oss.constant.OssSavePlaceEnum; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -40,70 +39,31 @@ import java.io.File; @Service @Slf4j @ConditionalOnProperty(name = "isys.oss.service-type", havingValue = "aliyun-oss") -public class AliyunOssService implements IOssService { +public class AliyunOssService implements IOssService{ - @Autowired - private AliyunOssYmlConfig aliyunOssYmlConfig; - - @Autowired - private OssYmlConfig ossYmlConfig; + @Autowired private AliyunOssYmlConfig aliyunOssYmlConfig; // ossClient 初始化 private OSS ossClient = null; @PostConstruct - public void init() { - ossClient = new OSSClientBuilder().build(aliyunOssYmlConfig.getEndpoint(), aliyunOssYmlConfig.getAccessKeyId(), - aliyunOssYmlConfig.getAccessKeySecret()); - } - - /** - * 处理文件保存路径 - * - * @param ossSavePlaceEnum 保存位置 - * @param filePath 文件路径 - * @return 完整路径 - */ - private String getFileKey(OssSavePlaceEnum ossSavePlaceEnum, String filePath) { - // 上传的时候需要考虑 OSS 存储空间的访问权限,并拼接路径前缀 - String filePrefix = ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC ? ossYmlConfig.getOss() - .getFilePublicPath() : ossYmlConfig.getOss().getFilePrivatePath(); - - // 如果路径包含设置的路径前缀,则跳过 - if (filePath.startsWith(filePrefix)) { - // OSS 不允许路径第一个字符为 / - if (filePath.indexOf("/") == 0) { - filePath = filePath.replaceFirst("/", ""); - } - return filePath; - } - - String fullPath = (filePrefix + "/" + filePath); - - // OSS 不允许路径第一个字符为 / - if (fullPath.indexOf("/") == 0) { - fullPath = fullPath.replaceFirst("/", ""); - } - - return fullPath; + public void init(){ + ossClient = new OSSClientBuilder().build(aliyunOssYmlConfig.getEndpoint(), aliyunOssYmlConfig.getAccessKeyId(), aliyunOssYmlConfig.getAccessKeySecret()); } @Override - public String upload2PreviewUrl(OssSavePlaceEnum ossSavePlaceEnum, MultipartFile multipartFile, - String saveDirAndFileName) { + public String upload2PreviewUrl(OssSavePlaceEnum ossSavePlaceEnum, MultipartFile multipartFile, String saveDirAndFileName) { try { - String fullPath = getFileKey(ossSavePlaceEnum, saveDirAndFileName); + this.ossClient.putObject(ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC ? aliyunOssYmlConfig.getPublicBucketName() : aliyunOssYmlConfig.getPrivateBucketName() + , saveDirAndFileName, multipartFile.getInputStream()); - this.ossClient.putObject( - ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC ? aliyunOssYmlConfig.getPublicBucketName() : aliyunOssYmlConfig.getPrivateBucketName() - , fullPath, multipartFile.getInputStream()); - - if (ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC) { + if(ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC){ // 文档:https://www.alibabacloud.com/help/zh/doc-detail/39607.htm example: https://BucketName.Endpoint/ObjectName - return "https://" + aliyunOssYmlConfig.getPublicBucketName() + "." + aliyunOssYmlConfig.getEndpoint() + "/" + fullPath; + return "https://" + aliyunOssYmlConfig.getPublicBucketName() + "." + aliyunOssYmlConfig.getEndpoint() + "/" + saveDirAndFileName; } + return saveDirAndFileName; } catch (Exception e) { @@ -116,21 +76,9 @@ public class AliyunOssService implements IOssService { public boolean downloadFile(OssSavePlaceEnum ossSavePlaceEnum, String source, String target) { try { - String fullPath = getFileKey(ossSavePlaceEnum, source); - - File downloadFile = new File(target); - - // 当本地路径的上层目录不存在时,自动创建 - // OSS SDK 在 Docker 内部可能出现 UnknownHost 错误具体表现为 - // com.aliyun.oss.ClientException: Cannot read the content input stream. - if (!downloadFile.getParentFile().exists()) { - log.info("downloadFile parent dir not exists create it: {}", - downloadFile.getParentFile().getAbsolutePath()); - downloadFile.getParentFile().mkdirs(); - } String bucket = ossSavePlaceEnum == OssSavePlaceEnum.PRIVATE ? aliyunOssYmlConfig.getPrivateBucketName() : aliyunOssYmlConfig.getPublicBucketName(); - this.ossClient.getObject(new GetObjectRequest(bucket, source), downloadFile); + this.ossClient.getObject(new GetObjectRequest(bucket, source), new File(target)); return true; } catch (Exception e) {