diff --git a/plugin.xml b/plugin.xml index 391efbf..0236b46 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,12 +5,13 @@ com.fanruan.fs yes no - 1.2.2 + 1.2.3 10.0 2020-01-27 richie [2020-12-16]修复远程设计新增、修改模板问题。
[2020-10-29]修复连接不释放问题。
[2020-08-17]修复key不存在时报错的问题。
diff --git a/src/main/java/com/fanruan/fs/s3/repository/core/S3RepositoryFactory.java b/src/main/java/com/fanruan/fs/s3/repository/core/S3RepositoryFactory.java index 90e6600..0b7125e 100644 --- a/src/main/java/com/fanruan/fs/s3/repository/core/S3RepositoryFactory.java +++ b/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 { 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 { 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(); + } } 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 5ab689b..540d27c 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 @@ -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("/"));