|
|
@ -19,6 +19,7 @@ import com.fanruan.api.util.StringKit; |
|
|
|
import com.fr.io.repository.FineFileEntry; |
|
|
|
import com.fr.io.repository.FineFileEntry; |
|
|
|
import com.fr.io.repository.base.BaseResourceRepository; |
|
|
|
import com.fr.io.repository.base.BaseResourceRepository; |
|
|
|
import com.fr.stable.Filter; |
|
|
|
import com.fr.stable.Filter; |
|
|
|
|
|
|
|
import com.fr.stable.StringUtils; |
|
|
|
import com.fr.workspace.resource.ResourceIOException; |
|
|
|
import com.fr.workspace.resource.ResourceIOException; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
@ -110,6 +111,17 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void write(String path, byte[] data) { |
|
|
|
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; |
|
|
|
ObjectMetadata metadata; |
|
|
|
try { |
|
|
|
try { |
|
|
|
metadata = s3.getObjectMetadata(bucket, path); |
|
|
|
metadata = s3.getObjectMetadata(bucket, path); |
|
|
@ -130,12 +142,17 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean createFile(String path) { |
|
|
|
public boolean createFile(String path) { |
|
|
|
|
|
|
|
|
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
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 { |
|
|
|
try { |
|
|
|
s3.putObject(request); |
|
|
|
s3.putObject(req); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", parent); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -145,23 +162,45 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
if (!path.endsWith(DELIMITER)) { |
|
|
|
if (!path.endsWith(DELIMITER)) { |
|
|
|
path += DELIMITER; |
|
|
|
path += DELIMITER; |
|
|
|
} |
|
|
|
} |
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); |
|
|
|
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 { |
|
|
|
try { |
|
|
|
s3.putObject(request); |
|
|
|
s3.putObject(req); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
LogKit.error("[S3] Failed to create parent path {}", parent); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean delete(String path) { |
|
|
|
public boolean delete(String path) { |
|
|
|
try { |
|
|
|
|
|
|
|
s3.deleteObject(bucket, path); |
|
|
|
s3.deleteObject(bucket, path); |
|
|
|
|
|
|
|
String prefix = path; |
|
|
|
|
|
|
|
if (!path.endsWith(DELIMITER)) { |
|
|
|
|
|
|
|
prefix = path + DELIMITER; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) |
|
|
|
|
|
|
|
.withPrefix(prefix).withDelimiter(DELIMITER); |
|
|
|
|
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
|
|
|
|
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { |
|
|
|
|
|
|
|
String key = summary.getKey(); |
|
|
|
|
|
|
|
if (!key.endsWith(DELIMITER)) { |
|
|
|
|
|
|
|
s3.deleteObject(bucket, key); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (String pre : objectListing.getCommonPrefixes()) { |
|
|
|
|
|
|
|
delete(pre); |
|
|
|
|
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
LogKit.error(e.getMessage(), e); |
|
|
|
LogKit.error(e.getMessage(), e); |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
s3.deleteObject(bucket, prefix); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -184,7 +223,12 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
.withPrefix(dir).withDelimiter(DELIMITER); |
|
|
|
.withPrefix(dir).withDelimiter(DELIMITER); |
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { |
|
|
|
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { |
|
|
|
String[] arr = objectSummary.getKey().split(DELIMITER); |
|
|
|
|
|
|
|
|
|
|
|
String key = objectSummary.getKey(); |
|
|
|
|
|
|
|
if (StringUtils.equals(key, dir)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String[] arr = key.split(DELIMITER); |
|
|
|
String name = arr[arr.length - 1]; |
|
|
|
String name = arr[arr.length - 1]; |
|
|
|
if (filter == null) { |
|
|
|
if (filter == null) { |
|
|
|
result.add(name); |
|
|
|
result.add(name); |
|
|
@ -210,7 +254,8 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean isDirectory(String path) { |
|
|
|
public boolean isDirectory(String path) { |
|
|
|
if (path.endsWith(DELIMITER)) { |
|
|
|
|
|
|
|
|
|
|
|
if (path.endsWith(DELIMITER) && exist(path)) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
ObjectListing listing = s3.listObjects(bucket, path); |
|
|
|
ObjectListing listing = s3.listObjects(bucket, path); |
|
|
@ -311,4 +356,27 @@ public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
return entry; |
|
|
|
return entry; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 递归创建父目录的metadata. 比如path是 {@code WEB-INF/reportlets/test/1.cpt} |
|
|
|
|
|
|
|
* 则返回 {@code [WEB-INF/reportlets/test/1.cpt, WEB-INF/reportlets/test/, WEB-INF/reportlets/, WEB-INF/]} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private List<String> getAllNecessaryPath(String path) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有path路径及其所有父路径
|
|
|
|
|
|
|
|
List<String> allPath = new ArrayList<>(); |
|
|
|
|
|
|
|
allPath.add(path); |
|
|
|
|
|
|
|
int lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); |
|
|
|
|
|
|
|
// 以/结尾的先把末尾的/去掉
|
|
|
|
|
|
|
|
if (lastIdxOfDelimiter == path.length() - 1) { |
|
|
|
|
|
|
|
path = path.substring(0, lastIdxOfDelimiter); |
|
|
|
|
|
|
|
lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
while (lastIdxOfDelimiter > 0) { |
|
|
|
|
|
|
|
allPath.add(path.substring(0, lastIdxOfDelimiter + 1)); |
|
|
|
|
|
|
|
path = path.substring(0, lastIdxOfDelimiter); |
|
|
|
|
|
|
|
lastIdxOfDelimiter = path.lastIndexOf(DELIMITER); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return allPath; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|