Browse Source

S3插件

pull/1/head
richie 4 years ago
parent
commit
15c2c16f49
  1. 3
      plugin.xml
  2. 4
      readme.md
  3. BIN
      screenshots/s3.png
  4. 12
      src/main/java/com/fanruan/fs/s3/repository/core/S3Config.java
  5. 40
      src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java
  6. 4
      src/main/resources/com/fanruan/fs/s3/repository/web/js/bundle.js

3
plugin.xml

@ -5,12 +5,13 @@
<main-package>com.fanruan.fs</main-package>
<active>yes</active>
<hidden>no</hidden>
<version>1.0</version>
<version>1.1.1</version>
<env-version>10.0</env-version>
<jartime>2020-01-27</jartime>
<vendor>richie</vendor>
<description><![CDATA[使用支持S3协议的云存储文件系统作为文件服务器。]]></description>
<change-notes><![CDATA[
[2020-06-15]支持区域设置。<br/>
[2020-06-12]初始化插件。<br/>
]]></change-notes>
<extra-core>

4
readme.md

@ -1,3 +1,5 @@
# 文件服务器(S3协议)
能够使用支持S3协议的云存储,作为文件服务器
能够使用支持S3协议的云存储,作为文件服务器,安装好插件后在决策平台的智能运维->集群配置->文件一致性设置选项下,按下图设置即可:
![ui](screenshots/s3.png)

BIN
screenshots/s3.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

12
src/main/java/com/fanruan/fs/s3/repository/core/S3Config.java

@ -4,6 +4,8 @@ import com.fr.config.Identifier;
import com.fr.config.holder.Conf;
import com.fr.config.holder.factory.Holders;
import com.fr.io.config.CommonRepoConfig;
import com.fr.io.context.info.GetConfig;
import com.fr.io.context.info.SetConfig;
import com.fr.stable.StringUtils;
/**
@ -32,42 +34,52 @@ public class S3Config extends CommonRepoConfig {
@Identifier("bucket")
private Conf<String> bucket = Holders.simple(StringUtils.EMPTY);
@GetConfig("endPoint")
public String getEndPoint() {
return endPoint.get();
}
@SetConfig("endPoint")
public void setEndPoint(String endPoint) {
this.endPoint.set(endPoint);
}
@GetConfig("region")
public String getRegion() {
return region.get();
}
@SetConfig("region")
public void setRegion(String region) {
this.region.set(region);
}
@GetConfig("accessKeyId")
public String getAccessKeyId() {
return accessKeyId.get();
}
@SetConfig("accessKeyId")
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId.set(accessKeyId);
}
@GetConfig("accessKeySecret")
public String getAccessKeySecret() {
return accessKeySecret.get();
}
@SetConfig("accessKeySecret")
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret.set(accessKeySecret);
}
@GetConfig("bucket")
public String getBucket() {
return bucket.get();
}
@SetConfig("bucket")
public void setBucket(String bucket) {
this.bucket.set(bucket);
}

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

@ -13,7 +13,6 @@ import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.io.repository.FineFileEntry;
import com.fr.io.repository.base.BaseResourceRepository;
import com.fr.stable.Filter;
@ -32,15 +31,18 @@ import java.util.List;
*/
public class S3ResourceRepository extends BaseResourceRepository {
private static final String DELIMITER = "/";
private final AmazonS3 s3;
private final String bucket;
public S3ResourceRepository(String repoName, String workRoot, S3Config config) {
super(repoName, workRoot);
BasicAWSCredentials credentials = new BasicAWSCredentials(config.getAccessKeyId(), config.getAccessKeySecret());
this.s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(config.getEndPoint(), config.getRegion()))
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
this.bucket = config.getBucket();
}
@ -59,13 +61,22 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public FineFileEntry[] listEntry(String dir) {
List<FineFileEntry> result = new ArrayList<>();
if (!dir.endsWith(DELIMITER)) {
dir += DELIMITER;
}
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket)
.withPrefix(dir).withDelimiter("/");
.withPrefix(dir).withDelimiter(DELIMITER);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
String key = summary.getKey();
result.add(s3Object2FileEntry(summary, key));
}
for (String prefix : objectListing.getCommonPrefixes()) {
FineFileEntry entry = new FineFileEntry(prefix);
entry.setDirectory(true);
result.add(entry);
}
return result.toArray(new FineFileEntry[0]);
}
@ -98,13 +109,18 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public void write(String path, byte[] data) {
ObjectMetadata metadata = s3.getObject(bucket, path).getObjectMetadata();
ObjectMetadata metadata;
try {
metadata = s3.getObjectMetadata(bucket, path);
} catch (Exception e) {
metadata = new ObjectMetadata();
}
s3.putObject(bucket, path, new ByteArrayInputStream(data), metadata);
}
@Override
public boolean createFile(String path) {
PutObjectRequest request = new PutObjectRequest(bucket, path, StringKit.EMPTY);
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), new ObjectMetadata());
try {
s3.putObject(request);
} catch (Exception e) {
@ -115,7 +131,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean createDirectory(String path) {
PutObjectRequest request = new PutObjectRequest(bucket, path + "/", StringKit.EMPTY);
PutObjectRequest request = new PutObjectRequest(bucket, path + "/", new ByteArrayInputStream(new byte[0]), new ObjectMetadata());
try {
s3.putObject(request);
} catch (Exception e) {
@ -138,7 +154,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean exist(String path) {
try {
return s3.getObject(bucket, path) != null;
return s3.doesObjectExist(bucket, path);
} catch (Exception e) {
return false;
}
@ -147,14 +163,22 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public String[] list(String dir, final Filter<String> filter) {
List<String> result = new ArrayList<>();
if (!dir.endsWith(DELIMITER)) {
dir += DELIMITER;
}
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket)
.withPrefix(dir).withDelimiter("/");
.withPrefix(dir).withDelimiter(DELIMITER);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
if (filter.accept(objectSummary.getKey())) {
result.add(objectSummary.getKey());
}
}
for (String prefix : objectListing.getCommonPrefixes()) {
if (filter.accept(prefix)) {
result.add(prefix);
}
}
return result.toArray(new String[0]);
}

4
src/main/resources/com/fanruan/fs/s3/repository/web/js/bundle.js

@ -70,7 +70,7 @@ BI.config("dec.constant.intelligence.cluster.file.server", function (items) {
editorWidth: EDITOR_WIDTH,
watermark: BI.i18nText("Plugin-S3_Input"),
text: BI.i18nText("Plugin-S3_Access_Key_Id"),
value: this.model.port,
value: this.model.accessKeyId,
ref: function (_ref) {
self.portRow = _ref;
},
@ -87,7 +87,7 @@ BI.config("dec.constant.intelligence.cluster.file.server", function (items) {
editorWidth: EDITOR_WIDTH,
watermark: BI.i18nText("Plugin-S3_Access_Key_Secret"),
text: BI.i18nText("Plugin-S3_Access_Key_Secret"),
value: this.model.accessKeySecret,
value: "******",
listeners: [{
eventName: BI.Editor.EVENT_CHANGE,
action: function () {

Loading…
Cancel
Save