Browse Source

QFX-3714 update:重写s3的copy方法

master
Seth.Tian 3 years ago
parent
commit
2d967fa5fe
  1. 3
      plugin.xml
  2. 30
      src/main/java/com/fanruan/fs/s3/repository/core/S3RepositoryFactory.java
  3. 33
      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-11]重写copy实现。<br/>
[2020-12-16]修复远程设计新增、修改模板问题。<br/>
[2020-10-29]修复连接不释放问题。<br/>
[2020-08-17]修复key不存在时报错的问题。<br/>

30
src/main/java/com/fanruan/fs/s3/repository/core/S3RepositoryFactory.java

@ -5,10 +5,15 @@ import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.fanruan.api.log.LogKit;
import com.fr.io.base.provider.impl.ConfigRepositoryFactory;
import com.fr.io.context.info.RepositoryProfile;
import com.fr.io.repository.ResourceRepository;
import com.fr.stable.AssistUtils;
import java.io.IOException;
/**
* @author richie
@ -18,6 +23,8 @@ import com.fr.io.repository.ResourceRepository;
public class S3RepositoryFactory extends ConfigRepositoryFactory<S3Config> {
static final String IDENTITY = "S3";
private static final String TEST_KEY = "test_key";
private static final String TEST_CONTENT = "test content";
public S3RepositoryFactory() {
super(IDENTITY);
@ -40,16 +47,35 @@ public class S3RepositoryFactory extends ConfigRepositoryFactory<S3Config> {
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(config.getEndPoint(), config.getRegion()))
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
s3.listObjects(config.getBucket());
// listObjects方法存在问题,如果key中包含URL编码的内容,将会抛异常,修改为读写验证是否可用
s3.putObject(config.getBucket(), TEST_KEY, TEST_CONTENT);
try (S3Object object = s3.getObject(config.getBucket(), TEST_KEY)) {
final String content = getContent(object);
s3.deleteObject(config.getBucket(), TEST_KEY);
return AssistUtils.equals(TEST_CONTENT, content);
}
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
return false;
}
return true;
}
@Override
public ResourceRepository produce(String repoName, String workRoot, S3Config config) {
return new S3ResourceRepository(repoName, workRoot, config);
}
private String getContent(S3Object object) {
final StringBuilder stringBuilder = new StringBuilder();
try (S3ObjectInputStream objectContent = object.getObjectContent()) {
final byte[] bytes = new byte[4096];
int i = 0;
while ((i = objectContent.read(bytes)) > -1) {
stringBuilder.append(new String(bytes, 0, i));
}
} catch (IOException e) {
LogKit.error(e.getMessage(), e);
}
return stringBuilder.toString();
}
}

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

@ -16,9 +16,13 @@ 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.common.util.Strings;
import com.fr.io.repository.FineFileEntry;
import com.fr.io.repository.base.BaseResourceRepository;
import com.fr.stable.ArrayUtils;
import com.fr.stable.AssistUtils;
import com.fr.stable.Filter;
import com.fr.stable.StableUtils;
import com.fr.workspace.resource.ResourceIOException;
import java.io.ByteArrayInputStream;
@ -96,6 +100,35 @@ public class S3ResourceRepository extends BaseResourceRepository {
return result.toArray(new FineFileEntry[0]);
}
@Override
public boolean copy(String source, String target) throws ResourceIOException {
if (AssistUtils.equals(source, target)
|| Strings.isEmpty(source)
|| Strings.isEmpty(target)) {
return true;
}
boolean result = true;
if (this.isDirectory(source)) {
final String[] list = list(source);
if (!ArrayUtils.isEmpty(list)) {
for (String s : list) {
String targetKey = StableUtils.pathJoin(target, s);
String sourceKey = StableUtils.pathJoin(source, s);
result = result && this.copy(sourceKey, targetKey);
}
}
} else {
try {
s3.copyObject(bucket, source, bucket, target);
return true;
} catch (Exception e) {
LogKit.error(e, "copy {} to {} failed, because : ", source, target, e.getMessage());
return false;
}
}
return result;
}
private FineFileEntry s3Object2FileEntry(S3ObjectSummary s3Object, String path) {
FineFileEntry entry = new FineFileEntry(path);
entry.setDirectory(s3Object.getKey().endsWith("/"));

Loading…
Cancel
Save