From 39e97de37f61884ab7d8ba63b41164bbbe9f91c4 Mon Sep 17 00:00:00 2001 From: Feng Date: Tue, 26 Jan 2021 09:47:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?DEC-17006=20fix:=20=E3=80=90S3=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E3=80=91=E9=85=8D=E7=BD=AE=E8=BE=93=E5=85=A5=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84=E4=B9=9F=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=BF=9E=E6=8E=A5=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fanruan/fs/s3/repository/core/S3ResourceRepository.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 4ea1913..621bcaa 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 @@ -210,7 +210,8 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean isDirectory(String path) { - if (path.endsWith(DELIMITER)) { + + if (path.endsWith(DELIMITER) && exist(path)) { return true; } ObjectListing listing = s3.listObjects(bucket, path); From 2c5773904be1d0ba6513a12706ca54ccd63a7e26 Mon Sep 17 00:00:00 2001 From: Feng Date: Tue, 26 Jan 2021 16:30:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?DEC-16997=20fix:=20=E3=80=90S3=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E3=80=91=E5=AE=9A=E6=97=B6=E8=B0=83=E5=BA=A6=E7=9A=84?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=96=87=E4=BB=B6=E9=A2=84=E8=A7=88=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/core/S3ResourceRepository.java | 93 ++++++++++++++++--- 1 file changed, 80 insertions(+), 13 deletions(-) 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 621bcaa..9c18ead 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 @@ -19,6 +19,7 @@ import com.fanruan.api.util.StringKit; import com.fr.io.repository.FineFileEntry; import com.fr.io.repository.base.BaseResourceRepository; import com.fr.stable.Filter; +import com.fr.stable.StringUtils; import com.fr.workspace.resource.ResourceIOException; import java.io.ByteArrayInputStream; @@ -110,6 +111,17 @@ 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); @@ -130,11 +142,16 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean createFile(String path) { - PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(request); - } catch (Exception e) { - return false; + 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; + } } return true; } @@ -145,23 +162,45 @@ public class S3ResourceRepository extends BaseResourceRepository { if (!path.endsWith(DELIMITER)) { path += DELIMITER; } - PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(request); - } catch (Exception e) { - return false; + 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; + } } return true; } @Override public boolean delete(String path) { + + s3.deleteObject(bucket, path); + String prefix = path; + if (!path.endsWith(DELIMITER)) { + prefix = path + DELIMITER; + } try { - s3.deleteObject(bucket, path); + ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) + .withPrefix(prefix).withDelimiter(DELIMITER); + ObjectListing objectListing = s3.listObjects(listObjectsRequest); + for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { + String key = summary.getKey(); + if (!key.endsWith(DELIMITER)) { + s3.deleteObject(bucket, key); + } + } + for (String pre : objectListing.getCommonPrefixes()) { + delete(pre); + } } catch (Exception e) { LogKit.error(e.getMessage(), e); - return false; } + s3.deleteObject(bucket, prefix); return true; } @@ -184,7 +223,12 @@ public class S3ResourceRepository extends BaseResourceRepository { .withPrefix(dir).withDelimiter(DELIMITER); ObjectListing objectListing = s3.listObjects(listObjectsRequest); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { - String[] arr = objectSummary.getKey().split(DELIMITER); + + String key = objectSummary.getKey(); + if (StringUtils.equals(key, dir)) { + continue; + } + String[] arr = key.split(DELIMITER); String name = arr[arr.length - 1]; if (filter == null) { result.add(name); @@ -312,4 +356,27 @@ public class S3ResourceRepository extends BaseResourceRepository { return entry; } + /** + * 递归创建父目录的metadata. 比如path是 {@code WEB-INF/reportlets/test/1.cpt} + * 则返回 {@code [WEB-INF/reportlets/test/1.cpt, WEB-INF/reportlets/test/, WEB-INF/reportlets/, WEB-INF/]} + */ + private List getAllNecessaryPath(String path) { + + // 获取所有path路径及其所有父路径 + List allPath = new ArrayList<>(); + allPath.add(path); + int lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); + // 以/结尾的先把末尾的/去掉 + if (lastIdxOfDelimiter == path.length() - 1) { + path = path.substring(0, lastIdxOfDelimiter); + lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); + } + while (lastIdxOfDelimiter > 0) { + allPath.add(path.substring(0, lastIdxOfDelimiter + 1)); + path = path.substring(0, lastIdxOfDelimiter); + lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); + } + return allPath; + } + } From f16a557bf09b53212e9a4875f906551ac0209f9a Mon Sep 17 00:00:00 2001 From: Feng Date: Tue, 26 Jan 2021 16:31:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?DEC-16997=20fix:=20=E3=80=90S3=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E3=80=91=E5=AE=9A=E6=97=B6=E8=B0=83=E5=BA=A6=E7=9A=84?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=96=87=E4=BB=B6=E9=A2=84=E8=A7=88=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 63de31a..1bff9bd 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,12 +5,13 @@ com.fanruan.fs yes no - 1.2.3 + 1.2.4 10.0 2020-01-27 richie [2021-01-20]正式作为官方插件维护。
[2020-12-16]修复远程设计新增、修改模板问题。
[2020-10-29]修复连接不释放问题。