From 582fd2d3c0162695dc4cad777082badac14dd04c Mon Sep 17 00:00:00 2001 From: Afly Date: Tue, 3 Jan 2023 17:21:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0jira=E4=BB=BB=E5=8A=A1=20perf:=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=99=E6=96=87=E4=BB=B6=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 3 +- .../repository/core/S3ResourceRepository.java | 106 +++++++++++++----- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/plugin.xml b/plugin.xml index 1065e7a..7cc4c5f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,12 +5,13 @@ com.fanruan.fs yes no - 1.3.5 + 1.3.6 11.0~11.0 2021-08-30 richie [2022-09-22]第三方组件升级。
[2022-07-25]修复http无法连接,而https和缺省的时候正常
[2022-06-30]新增enablePathStyleAccess、signerOverride后台配置
diff --git a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java index 51db511..ae4833a 100644 --- a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java +++ b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java @@ -119,17 +119,6 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public void write(String path, byte[] data) { - - List paths = getAllNecessaryPath(path); - for (int i = paths.size() - 1; i > 0; i--) { - String parent = paths.get(i); - PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(req); - } catch (Exception e) { - LogKit.error("[S3] Failed to create parent path {}", parent); - } - } ObjectMetadata metadata; try { metadata = s3.getObjectMetadata(bucket, path); @@ -150,16 +139,16 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean createFile(String path) { - List paths = getAllNecessaryPath(path); - for (int i = paths.size() - 1; i >= 0; i--) { - String parent = paths.get(i); - PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(req); - } catch (Exception e) { - LogKit.error("[S3] Failed to create parent path {}", parent); - return false; - } + String parent = path.substring(0, path.lastIndexOf(DELIMITER) + 1); + if (!dirExist(parent)) { + createDirectory(parent); + } + PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); + try { + s3.putObject(req); + } catch (Exception e) { + LogKit.error("[S3] Failed to create file path {}", path); + return false; } return true; } @@ -170,17 +159,23 @@ public class S3ResourceRepository extends BaseResourceRepository { if (!path.endsWith(DELIMITER)) { path += DELIMITER; } - List paths = getAllNecessaryPath(path); - for (int i = paths.size() - 1; i >= 0; i--) { - String parent = paths.get(i); - PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(req); - } catch (Exception e) { - LogKit.error("[S3] Failed to create parent path {}", parent); + String temp = path.substring(0, path.length() - 1); + String parent = temp.substring(0, temp.lastIndexOf(DELIMITER) + 1); + if (StringUtils.isEmpty(parent)) { + return true; + } + if (!dirExist(parent)) { + if (!createDirectory(parent)) { return false; } } + PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); + try { + s3.putObject(req); + } catch (Exception e) { + LogKit.error("[S3] Failed to create parent path {}", path); + return false; + } return true; } @@ -214,13 +209,49 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean exist(String path) { + return fileExist(path) || (!path.endsWith(DELIMITER) && dirExist(path)) || isParentPathAbsent(path); + } + + private boolean fileExist(String path) { try { - return s3.doesObjectExist(bucket, path) || s3.doesObjectExist(bucket, path + DELIMITER); + return s3.doesObjectExist(bucket, path); } catch (Exception e) { return false; } } + private boolean dirExist(String path) { + if (StringUtils.equals(path, DELIMITER)) { + return true; + } + if (!path.endsWith(DELIMITER)) { + path += DELIMITER; + } + return fileExist(path); + } + + /** + * 如果存在文件创建了,但是其父目录没有创建的场景,为其递归创建对象,返回创建结果 + */ + private boolean isParentPathAbsent(String path) { + if (path.startsWith(DELIMITER)) { + path = path.substring(1); + } + if (path.endsWith(DELIMITER)) { + path = path.substring(0, path.length() - 1); + } + ObjectListing objectListing = s3.listObjects( + new ListObjectsRequest() + .withBucketName(bucket) + .withPrefix(path)); + for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { + if (summary.getKey().startsWith(path + DELIMITER)) { + return createDirectory(path); + } + } + return false; + } + @Override public String[] list(String dir, final Filter filter) { List result = new ArrayList<>(); @@ -262,7 +293,20 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean isDirectory(String path) { - return exist(path.endsWith(DELIMITER) ? path : path + DELIMITER); + + if (path.endsWith(DELIMITER) && exist(path)) { + return true; + } + ObjectListing listing = s3.listObjects(bucket, path); + if (listing.getObjectSummaries().isEmpty()) { + return false; + } + if (listing.getObjectSummaries().size() > 1) { + return true; + } else { + S3ObjectSummary summary = listing.getObjectSummaries().get(0); + return !StringKit.equals(listing.getPrefix(), summary.getKey()); + } } @Override