Browse Source

Pull request #82: 无jira任务 perf: 优化写文件性能

Merge in PG/plugin-repository-s3 from ~AFLY/plugin-repository-s3:release/11.0 to release/11.0

* commit '582fd2d3c0162695dc4cad777082badac14dd04c':
  无jira任务 perf: 优化写文件性能
release/11.0
Afly-储泓飞 2 years ago
parent
commit
e0bb7ccb9b
  1. 3
      plugin.xml
  2. 94
      src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

3
plugin.xml

@ -5,12 +5,13 @@
<main-package>com.fanruan.fs</main-package> <main-package>com.fanruan.fs</main-package>
<active>yes</active> <active>yes</active>
<hidden>no</hidden> <hidden>no</hidden>
<version>1.3.5</version> <version>1.3.6</version>
<env-version>11.0~11.0</env-version> <env-version>11.0~11.0</env-version>
<jartime>2021-08-30</jartime> <jartime>2021-08-30</jartime>
<vendor>richie</vendor> <vendor>richie</vendor>
<description><![CDATA[使用支持S3协议的云存储文件系统作为文件服务器。]]></description> <description><![CDATA[使用支持S3协议的云存储文件系统作为文件服务器。]]></description>
<change-notes><![CDATA[ <change-notes><![CDATA[
[2023-01-03]优化写文件性能。 <br/>
[2022-09-22]第三方组件升级。 <br/> [2022-09-22]第三方组件升级。 <br/>
[2022-07-25]修复http无法连接,而https和缺省的时候正常<br/> [2022-07-25]修复http无法连接,而https和缺省的时候正常<br/>
[2022-06-30]新增enablePathStyleAccess、signerOverride后台配置<br/> [2022-06-30]新增enablePathStyleAccess、signerOverride后台配置<br/>

94
src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

@ -119,17 +119,6 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override @Override
public void write(String path, byte[] data) { public void write(String path, byte[] data) {
List<String> 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; ObjectMetadata metadata;
try { try {
metadata = s3.getObjectMetadata(bucket, path); metadata = s3.getObjectMetadata(bucket, path);
@ -150,17 +139,17 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override @Override
public boolean createFile(String path) { public boolean createFile(String path) {
List<String> paths = getAllNecessaryPath(path); String parent = path.substring(0, path.lastIndexOf(DELIMITER) + 1);
for (int i = paths.size() - 1; i >= 0; i--) { if (!dirExist(parent)) {
String parent = paths.get(i); createDirectory(parent);
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); }
PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try { try {
s3.putObject(req); s3.putObject(req);
} catch (Exception e) { } catch (Exception e) {
LogKit.error("[S3] Failed to create parent path {}", parent); LogKit.error("[S3] Failed to create file path {}", path);
return false; return false;
} }
}
return true; return true;
} }
@ -170,17 +159,23 @@ public class S3ResourceRepository extends BaseResourceRepository {
if (!path.endsWith(DELIMITER)) { if (!path.endsWith(DELIMITER)) {
path += DELIMITER; path += DELIMITER;
} }
List<String> paths = getAllNecessaryPath(path); String temp = path.substring(0, path.length() - 1);
for (int i = paths.size() - 1; i >= 0; i--) { String parent = temp.substring(0, temp.lastIndexOf(DELIMITER) + 1);
String parent = paths.get(i); if (StringUtils.isEmpty(parent)) {
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); return true;
}
if (!dirExist(parent)) {
if (!createDirectory(parent)) {
return false;
}
}
PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try { try {
s3.putObject(req); s3.putObject(req);
} catch (Exception e) { } catch (Exception e) {
LogKit.error("[S3] Failed to create parent path {}", parent); LogKit.error("[S3] Failed to create parent path {}", path);
return false; return false;
} }
}
return true; return true;
} }
@ -214,13 +209,49 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override @Override
public boolean exist(String path) { public boolean exist(String path) {
return fileExist(path) || (!path.endsWith(DELIMITER) && dirExist(path)) || isParentPathAbsent(path);
}
private boolean fileExist(String path) {
try { try {
return s3.doesObjectExist(bucket, path) || s3.doesObjectExist(bucket, path + DELIMITER); return s3.doesObjectExist(bucket, path);
} catch (Exception e) { } catch (Exception e) {
return false; 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 @Override
public String[] list(String dir, final Filter<String> filter) { public String[] list(String dir, final Filter<String> filter) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
@ -262,7 +293,20 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override @Override
public boolean isDirectory(String path) { 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 @Override

Loading…
Cancel
Save