|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|