|
|
|
@ -87,6 +87,15 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) |
|
|
|
|
.withPrefix(dir).withDelimiter(DELIMITER); |
|
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
|
collectFileEntry(result, objectListing); |
|
|
|
|
while (objectListing.isTruncated()) { |
|
|
|
|
objectListing = s3.listNextBatchOfObjects(objectListing); |
|
|
|
|
collectFileEntry(result, objectListing); |
|
|
|
|
} |
|
|
|
|
return result.toArray(new FineFileEntry[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void collectFileEntry(List<FineFileEntry> result, ObjectListing objectListing) { |
|
|
|
|
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { |
|
|
|
|
String key = summary.getKey(); |
|
|
|
|
if (!key.endsWith(DELIMITER)) { |
|
|
|
@ -98,8 +107,6 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
entry.setDirectory(true); |
|
|
|
|
result.add(entry); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result.toArray(new FineFileEntry[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@ -119,17 +126,6 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void write(String path, byte[] data) { |
|
|
|
|
|
|
|
|
|
List<String> paths = getAllNecessaryPath(path); |
|
|
|
|
for (int i = paths.size() - 1; i > 0; i--) { |
|
|
|
|
String parent = paths.get(i); |
|
|
|
|
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(req); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", parent); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
ObjectMetadata metadata; |
|
|
|
|
try { |
|
|
|
|
metadata = s3.getObjectMetadata(bucket, path); |
|
|
|
@ -150,16 +146,16 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
@Override |
|
|
|
|
public boolean createFile(String path) { |
|
|
|
|
|
|
|
|
|
List<String> paths = getAllNecessaryPath(path); |
|
|
|
|
for (int i = paths.size() - 1; i >= 0; i--) { |
|
|
|
|
String parent = paths.get(i); |
|
|
|
|
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(req); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", parent); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
String parent = path.substring(0, path.lastIndexOf(DELIMITER) + 1); |
|
|
|
|
if (!dirExist(parent)) { |
|
|
|
|
createDirectory(parent); |
|
|
|
|
} |
|
|
|
|
PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(req); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
LogKit.error("[S3] Failed to create file path {}", path); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
@ -170,17 +166,23 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
if (!path.endsWith(DELIMITER)) { |
|
|
|
|
path += DELIMITER; |
|
|
|
|
} |
|
|
|
|
List<String> paths = getAllNecessaryPath(path); |
|
|
|
|
for (int i = paths.size() - 1; i >= 0; i--) { |
|
|
|
|
String parent = paths.get(i); |
|
|
|
|
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(req); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", parent); |
|
|
|
|
String temp = path.substring(0, path.length() - 1); |
|
|
|
|
String parent = temp.substring(0, temp.lastIndexOf(DELIMITER) + 1); |
|
|
|
|
if (StringUtils.isEmpty(parent)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
if (!dirExist(parent)) { |
|
|
|
|
if (!createDirectory(parent)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
PutObjectRequest req = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(req); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", path); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -214,13 +216,49 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean exist(String path) { |
|
|
|
|
return fileExist(path) || (!path.endsWith(DELIMITER) && dirExist(path)) || isParentPathAbsent(path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean fileExist(String path) { |
|
|
|
|
try { |
|
|
|
|
return s3.doesObjectExist(bucket, path) || s3.doesObjectExist(bucket, path + DELIMITER); |
|
|
|
|
return s3.doesObjectExist(bucket, path); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean dirExist(String path) { |
|
|
|
|
if (StringUtils.equals(path, DELIMITER)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
if (!path.endsWith(DELIMITER)) { |
|
|
|
|
path += DELIMITER; |
|
|
|
|
} |
|
|
|
|
return fileExist(path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 如果存在文件创建了,但是其父目录没有创建的场景,为其递归创建对象,返回创建结果 |
|
|
|
|
*/ |
|
|
|
|
private boolean isParentPathAbsent(String path) { |
|
|
|
|
if (path.startsWith(DELIMITER)) { |
|
|
|
|
path = path.substring(1); |
|
|
|
|
} |
|
|
|
|
if (path.endsWith(DELIMITER)) { |
|
|
|
|
path = path.substring(0, path.length() - 1); |
|
|
|
|
} |
|
|
|
|
ObjectListing objectListing = s3.listObjects( |
|
|
|
|
new ListObjectsRequest() |
|
|
|
|
.withBucketName(bucket) |
|
|
|
|
.withPrefix(path)); |
|
|
|
|
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { |
|
|
|
|
if (summary.getKey().startsWith(path + DELIMITER)) { |
|
|
|
|
return createDirectory(path); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public String[] list(String dir, final Filter<String> filter) { |
|
|
|
|
List<String> result = new ArrayList<>(); |
|
|
|
|