Browse Source

Pull request #8: DEC-16411 feat: S3插件变为官方插件

Merge in PG/plugin-repository-s3 from ~FENG/plugin-repository-s3:release/10.0 to release/10.0

* commit '4bf15d218c11cfb3d60b4b3fedba040dd12ee084':
  DEC-16411 feat: S3插件变为官方插件
  DEC-16411 feat: S3插件变为官方插件
persist/10.0
Feng 4 years ago
parent
commit
dee24ab76e
  1. 3
      plugin.xml
  2. 60
      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>
<active>yes</active>
<hidden>no</hidden>
<version>1.2.2</version>
<version>1.2.3</version>
<env-version>10.0</env-version>
<jartime>2020-01-27</jartime>
<vendor>richie</vendor>
<description><![CDATA[使用支持S3协议的云存储文件系统作为文件服务器。]]></description>
<change-notes><![CDATA[
[2021-01-20]正式作为官方插件维护。<br/>
[2020-12-16]修复远程设计新增、修改模板问题。<br/>
[2020-10-29]修复连接不释放问题。<br/>
[2020-08-17]修复key不存在时报错的问题。<br/>

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

@ -59,11 +59,8 @@ public class S3ResourceRepository extends BaseResourceRepository {
public FineFileEntry getEntry(String path) {
GetObjectRequest request = new GetObjectRequest(bucket, path);
try {
S3Object s3Object = s3.getObject(request);
try {
try (S3Object s3Object = s3.getObject(request)) {
return s3Object2FileEntry(s3Object, path);
} finally {
s3Object.close();
}
} catch (Exception e) {
LogKit.info("{} not exist!", path);
@ -96,22 +93,6 @@ public class S3ResourceRepository extends BaseResourceRepository {
return result.toArray(new FineFileEntry[0]);
}
private FineFileEntry s3Object2FileEntry(S3ObjectSummary s3Object, String path) {
FineFileEntry entry = new FineFileEntry(path);
entry.setDirectory(s3Object.getKey().endsWith("/"));
entry.setSize(s3Object.getSize());
entry.setTimestamp(s3Object.getLastModified().getTime());
return entry;
}
private FineFileEntry s3Object2FileEntry(S3Object s3Object, String path) {
FineFileEntry entry = new FineFileEntry(path);
entry.setDirectory(s3Object.getKey().endsWith("/"));
entry.setSize(s3Object.getObjectMetadata().getContentLength());
entry.setTimestamp(s3Object.getObjectMetadata().getLastModified().getTime());
return entry;
}
@Override
public URL getResource(String path) {
throw new RuntimeException("Not support method.");
@ -138,6 +119,8 @@ public class S3ResourceRepository extends BaseResourceRepository {
if (mimeType != null) {
metadata.setContentType(mimeType);
}
}
if (metadata != null) {
metadata.setContentLength(data.length);
}
s3.putObject(bucket, path, new ByteArrayInputStream(data), metadata);
@ -146,7 +129,8 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean createFile(String path) {
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), new ObjectMetadata());
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try {
s3.putObject(request);
} catch (Exception e) {
@ -157,7 +141,11 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean createDirectory(String path) {
PutObjectRequest request = new PutObjectRequest(bucket, path + "/", new ByteArrayInputStream(new byte[0]), new ObjectMetadata());
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) {
@ -180,7 +168,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean exist(String path) {
try {
return s3.doesObjectExist(bucket, path);
return s3.doesObjectExist(bucket, path) || s3.doesObjectExist(bucket, path + DELIMITER);
} catch (Exception e) {
return false;
}
@ -297,4 +285,30 @@ public class S3ResourceRepository extends BaseResourceRepository {
return S3RepositoryFactory.IDENTITY;
}
/*--------------------------------------私有方法-----------------------------------------**/
private ObjectMetadata buildEmptyMetadata() {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
return metadata;
}
private FineFileEntry s3Object2FileEntry(S3ObjectSummary s3Object, String path) {
FineFileEntry entry = new FineFileEntry(path);
entry.setDirectory(s3Object.getKey().endsWith("/"));
entry.setSize(s3Object.getSize());
entry.setTimestamp(s3Object.getLastModified().getTime());
return entry;
}
private FineFileEntry s3Object2FileEntry(S3Object s3Object, String path) {
FineFileEntry entry = new FineFileEntry(path);
entry.setDirectory(s3Object.getKey().endsWith("/"));
entry.setSize(s3Object.getObjectMetadata().getContentLength());
entry.setTimestamp(s3Object.getObjectMetadata().getLastModified().getTime());
return entry;
}
}

Loading…
Cancel
Save