diff --git a/plugin.xml b/plugin.xml index 391efbf..63de31a 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/S3ResourceRepository.java b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java index 73eb62d..4ea1913 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 @@ -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; + } + }