From 9bded7ff2b363e8b03ce9e37e864d666949aacef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E5=AE=87=E5=A5=87?= Date: Fri, 11 Jul 2025 19:23:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=9C=A8=E5=85=AC=E6=9C=89?= =?UTF-8?q?=E6=A1=B6=E4=B8=AD=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/OSSAutoConfiguration.java | 1 + .../ruoyi/framework/oss/OSSClientWrapper.java | 58 ++++++++----------- .../ruoyi/framework/oss/OSSConfiguration.java | 2 + .../ruoyi/xkt/service/IPictureService.java | 6 +- .../xkt/service/impl/PictureServiceImpl.java | 18 +++--- 5 files changed, 42 insertions(+), 43 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/OSSAutoConfiguration.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/OSSAutoConfiguration.java index 76f95138e..ddcbe1edc 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/OSSAutoConfiguration.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/OSSAutoConfiguration.java @@ -33,6 +33,7 @@ public class OSSAutoConfiguration { configuration.setAccessKeyId(properties.getAccessKeyId()); configuration.setAccessKeySecret(properties.getAccessKeySecret()); configuration.setBucketName(properties.getBucketName()); + configuration.setPublicBucketName(properties.getPublicBucketName()); configuration.setRegionId(properties.getRegionId()); configuration.setRoleArn(properties.getRoleArn()); configuration.setHttps(properties.isHttps()); diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSClientWrapper.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSClientWrapper.java index 3e15f755f..81af0f435 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSClientWrapper.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSClientWrapper.java @@ -70,9 +70,9 @@ public class OSSClientWrapper { * @param key key * @param inputStream 文件流 */ - public void upload(String key, InputStream inputStream) + public void upload(String bucketName, String key, InputStream inputStream) throws Exception { - upload(key, inputStream, null); + upload(bucketName, key, inputStream, null); } /** @@ -82,20 +82,21 @@ public class OSSClientWrapper { * @param inputStream 文件流 * @param contentType 文件类型 */ - public void upload(String key, InputStream inputStream, String contentType) + public void upload(String bucketName, String key, InputStream inputStream, String contentType) throws Exception { - upload(key, inputStream, contentType, null); + upload(bucketName, key, inputStream, contentType, null); } /** * 文件上传 * + * @param bucketName bucketName * @param key key * @param inputStream 文件流 * @param contentType 文件类型 * @param contentDisposition 下载描述信息 */ - public void upload(String key, InputStream inputStream, String contentType, String contentDisposition) throws Exception { + public void upload(String bucketName, String key, InputStream inputStream, String contentType, String contentDisposition) throws Exception { ObjectMetadata objectMeta = new ObjectMetadata(); objectMeta.setContentLength(inputStream.available()); // 可以在metadata中标记文件类型 @@ -105,8 +106,8 @@ public class OSSClientWrapper { objectMeta.setContentType(contentType); // 设置下载名 objectMeta.setContentDisposition(contentDisposition); - client.putObject(configuration.getBucketName(), key, inputStream, objectMeta); - if (!client.doesObjectExist(configuration.getBucketName(), key)) { + client.putObject(bucketName, key, inputStream, objectMeta); + if (!client.doesObjectExist(bucketName, key)) { throw new IllegalStateException("文件上传失败"); } } @@ -117,15 +118,15 @@ public class OSSClientWrapper { * @param sourceKey 源KEY * @param targetKey 目标KEY */ - public void move(String sourceKey, String targetKey) { - if (!client.doesObjectExist(configuration.getBucketName(), sourceKey)) { + public void move(String bucketName, String sourceKey, String targetKey) { + if (!client.doesObjectExist(bucketName, sourceKey)) { throw new IllegalStateException("源文件信息不存在"); } - client.copyObject(configuration.getBucketName(), sourceKey, configuration.getBucketName(), targetKey); - if (!client.doesObjectExist(configuration.getBucketName(), targetKey)) { + client.copyObject(bucketName, sourceKey, bucketName, targetKey); + if (!client.doesObjectExist(bucketName, targetKey)) { throw new IllegalStateException("移动文件失败[1]"); } - if (!this.delete(sourceKey)) { + if (!this.delete(bucketName, sourceKey)) { throw new IllegalStateException("移动文件失败[2]"); } } @@ -133,25 +134,16 @@ public class OSSClientWrapper { /** * 根据key,获取访问路径 * - * @param key key(文件相对路径) - * @param expireTime 过期时间,单位毫秒 - */ - public URL generateUrl(String key, Long expireTime) throws Exception { - return generateUrl(key, expireTime, false); - } - - /** - * 根据key,获取访问路径 - * + * @param bucketName bucketName * @param key key(文件相对路径) * @param expireTime 过期时间,单位毫秒 * @param down 是否生成直接下载的url */ - public URL generateUrl(String key, Long expireTime, boolean down) throws Exception { + public URL generateUrl(String bucketName, String key, Long expireTime, boolean down) throws Exception { Assert.notNull(expireTime, "过期时间为空"); GeneratePresignedUrlRequest request; Date expiration = new Date(System.currentTimeMillis() + expireTime); - request = new GeneratePresignedUrlRequest(this.configuration.getBucketName(), key); + request = new GeneratePresignedUrlRequest(bucketName, key); request.setExpiration(expiration); request.setMethod(HttpMethod.GET); ResponseHeaderOverrides header = new ResponseHeaderOverrides(); @@ -164,12 +156,12 @@ public class OSSClientWrapper { return this.client.generatePresignedUrl(request); } - public String generateBase64(String key) { + public String generateBase64(String bucketName, String key) { if (key == null) { return null; } try { - OSSObject ossObject = this.client.getObject(this.configuration.getBucketName(), key); + OSSObject ossObject = this.client.getObject(bucketName, key); if (ossObject == null) { return null; } @@ -242,17 +234,17 @@ public class OSSClientWrapper { return "image/jpeg"; } - public InputStream getObject(String key) { - OSSObject ossObject = client.getObject(configuration.getBucketName(), key); + public InputStream getObject(String bucketName, String key) { + OSSObject ossObject = client.getObject(bucketName, key); return ossObject.getObjectContent(); } /** * 下载文件 */ - public void download(String key, String filename) + public void download(String bucketName, String key, String filename) throws OSSException, ClientException { - client.getObject(new GetObjectRequest(configuration.getBucketName(), key), new File(filename)); + client.getObject(new GetObjectRequest(bucketName, key), new File(filename)); } /** @@ -260,11 +252,11 @@ public class OSSClientWrapper { * * @return 成功删除返回true 不存在返回false */ - public boolean delete(String key) { + public boolean delete(String bucketName, String key) { //检查是否是有效文件 - boolean exists = client.doesObjectExist(configuration.getBucketName(), key); + boolean exists = client.doesObjectExist(bucketName, key); if (exists) { - client.deleteObject(configuration.getBucketName(), key); + client.deleteObject(bucketName, key); } return exists; } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSConfiguration.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSConfiguration.java index eca66df14..fea38a2f0 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSConfiguration.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/oss/OSSConfiguration.java @@ -12,6 +12,7 @@ public class OSSConfiguration { private String accessKeyId; private String accessKeySecret; private String bucketName; + private String publicBucketName; private String regionId; private String roleArn; private boolean https; @@ -24,6 +25,7 @@ public class OSSConfiguration { configuration.setAccessKeyId(this.accessKeyId); configuration.setAccessKeySecret(this.getAccessKeySecret()); configuration.setBucketName(this.bucketName); + configuration.setPublicBucketName(this.publicBucketName); configuration.setRegionId(this.regionId); configuration.setRoleArn(this.roleArn); configuration.setHttps(this.https); diff --git a/xkt/src/main/java/com/ruoyi/xkt/service/IPictureService.java b/xkt/src/main/java/com/ruoyi/xkt/service/IPictureService.java index 1fd70c81a..5e4bc89b5 100644 --- a/xkt/src/main/java/com/ruoyi/xkt/service/IPictureService.java +++ b/xkt/src/main/java/com/ruoyi/xkt/service/IPictureService.java @@ -14,7 +14,7 @@ import java.util.List; public interface IPictureService { /** - * 处理图包 + * 处理图包(私有桶) * * @param key * @return @@ -22,7 +22,7 @@ public interface IPictureService { PicZipDTO processPicZip(String key); /** - * 同步图片到搜图服务器 + * 同步图片到搜图服务器(公有桶) * * @param productPicSyncDTO * @return @@ -30,7 +30,7 @@ public interface IPictureService { ProductPicSyncResultDTO sync2ImgSearchServer(ProductPicSyncDTO productPicSyncDTO); /** - * 以图搜商品 + * 以图搜商品(公有桶) * * @param picKey * @param num diff --git a/xkt/src/main/java/com/ruoyi/xkt/service/impl/PictureServiceImpl.java b/xkt/src/main/java/com/ruoyi/xkt/service/impl/PictureServiceImpl.java index 439378c4a..3b3e14328 100644 --- a/xkt/src/main/java/com/ruoyi/xkt/service/impl/PictureServiceImpl.java +++ b/xkt/src/main/java/com/ruoyi/xkt/service/impl/PictureServiceImpl.java @@ -50,9 +50,10 @@ public class PictureServiceImpl implements IPictureService { @Override public PicZipDTO processPicZip(String key) { + String bucketName = ossClient.getConfiguration().getBucketName(); checkProcessAccess(key); //下载至系统临时文件夹 - DownloadResultDTO downloadResult = downloadFile2TempDir(key); + DownloadResultDTO downloadResult = downloadFile2TempDir(bucketName, key); String tempDirPath = downloadResult.getTempDirPath(); try { String midDirName = "unzip_" + downloadResult.getTempDirName(); @@ -106,10 +107,10 @@ public class PictureServiceImpl implements IPictureService { String keyOf450 = StrUtil.replaceLast(key, downloadResult.getFileName(), nameOf450); String keyOf750 = StrUtil.replaceLast(key, downloadResult.getFileName(), nameOf750); try (InputStream is = new FileInputStream(pathOf450)) { - ossClient.upload(keyOf450, is); + ossClient.upload(bucketName, keyOf450, is); } try (InputStream is = new FileInputStream(pathOf750)) { - ossClient.upload(keyOf750, is); + ossClient.upload(bucketName, keyOf750, is); } return new PicZipDTO(key, keyOf450, keyOf750); } catch (Exception e) { @@ -124,6 +125,7 @@ public class PictureServiceImpl implements IPictureService { @Override public ProductPicSyncResultDTO sync2ImgSearchServer(ProductPicSyncDTO productPicSyncDTO) { Assert.notNull(productPicSyncDTO); + String bucketName = ossClient.getConfiguration().getPublicBucketName(); String productId = productPicSyncDTO.getStoreProductId().toString(); //删除商品图片 boolean allSuccess = imgSearchClient.deleteImg(productId); @@ -135,7 +137,7 @@ public class PictureServiceImpl implements IPictureService { imgAdd.setPicName(FileUtil.getName(picKey)); imgAdd.setCategoryId(Constants.IMG_SEARCH_CATEGORY_ID); try { - imgAdd.setPicInputStream(ossClient.getObject(picKey)); + imgAdd.setPicInputStream(ossClient.getObject(bucketName, picKey)); } catch (Exception e) { log.error("获取图片流异常: " + picKey, e); allSuccess = false; @@ -155,6 +157,7 @@ public class PictureServiceImpl implements IPictureService { @Override public List searchProductByPicKey(String picKey, Integer num) { Assert.notEmpty(picKey); + String bucketName = ossClient.getConfiguration().getPublicBucketName(); if (num == null || num < 0) { //默认返回30条数据 num = Constants.IMG_SEARCH_DEFAULT_REQUEST_NUM; @@ -168,7 +171,7 @@ public class PictureServiceImpl implements IPictureService { imgSearchReq.setStart(i); imgSearchReq.setDistinctProductId(true); try { - imgSearchReq.setPicInputStream(ossClient.getObject(picKey)); + imgSearchReq.setPicInputStream(ossClient.getObject(bucketName, picKey)); } catch (Exception e) { log.error("获取图片流异常: " + picKey, e); return rtnList; @@ -216,10 +219,11 @@ public class PictureServiceImpl implements IPictureService { /** * OSS下载文件到系统临时文件夹 * + * @param bucketName * @param ossKey * @return */ - private DownloadResultDTO downloadFile2TempDir(String ossKey) { + private DownloadResultDTO downloadFile2TempDir(String bucketName, String ossKey) { Assert.notEmpty("ossKey不能为空"); try { String baseTempDir = ossProperties.getTempDir(); @@ -229,7 +233,7 @@ public class PictureServiceImpl implements IPictureService { FileUtil.mkdir(tempDirPath); String fileName = FileNameUtil.getName(ossKey); String filePath = tempDirPath + fileName; - ossClient.download(ossKey, filePath); + ossClient.download(bucketName, ossKey, filePath); return DownloadResultDTO .builder() .fileName(fileName)