|
|
|
@ -13,7 +13,6 @@ import com.amazonaws.services.s3.model.PutObjectRequest;
|
|
|
|
|
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.io.repository.FineFileEntry; |
|
|
|
|
import com.fr.io.repository.base.BaseResourceRepository; |
|
|
|
|
import com.fr.stable.Filter; |
|
|
|
@ -32,9 +31,12 @@ import java.util.List;
|
|
|
|
|
*/ |
|
|
|
|
public class S3ResourceRepository extends BaseResourceRepository { |
|
|
|
|
|
|
|
|
|
private static final String DELIMITER = "/"; |
|
|
|
|
|
|
|
|
|
private final AmazonS3 s3; |
|
|
|
|
private final String bucket; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public S3ResourceRepository(String repoName, String workRoot, S3Config config) { |
|
|
|
|
super(repoName, workRoot); |
|
|
|
|
BasicAWSCredentials credentials = new BasicAWSCredentials(config.getAccessKeyId(), config.getAccessKeySecret()); |
|
|
|
@ -59,13 +61,22 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
@Override |
|
|
|
|
public FineFileEntry[] listEntry(String dir) { |
|
|
|
|
List<FineFileEntry> result = new ArrayList<>(); |
|
|
|
|
if (!dir.endsWith(DELIMITER)) { |
|
|
|
|
dir += DELIMITER; |
|
|
|
|
} |
|
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) |
|
|
|
|
.withPrefix(dir).withDelimiter("/"); |
|
|
|
|
.withPrefix(dir).withDelimiter(DELIMITER); |
|
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
|
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) { |
|
|
|
|
String key = summary.getKey(); |
|
|
|
|
result.add(s3Object2FileEntry(summary, key)); |
|
|
|
|
} |
|
|
|
|
for (String prefix : objectListing.getCommonPrefixes()) { |
|
|
|
|
FineFileEntry entry = new FineFileEntry(prefix); |
|
|
|
|
entry.setDirectory(true); |
|
|
|
|
result.add(entry); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result.toArray(new FineFileEntry[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -98,13 +109,18 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void write(String path, byte[] data) { |
|
|
|
|
ObjectMetadata metadata = s3.getObject(bucket, path).getObjectMetadata(); |
|
|
|
|
ObjectMetadata metadata; |
|
|
|
|
try { |
|
|
|
|
metadata = s3.getObjectMetadata(bucket, path); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
metadata = new ObjectMetadata(); |
|
|
|
|
} |
|
|
|
|
s3.putObject(bucket, path, new ByteArrayInputStream(data), metadata); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean createFile(String path) { |
|
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path, StringKit.EMPTY); |
|
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), new ObjectMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(request); |
|
|
|
|
} catch (Exception e) { |
|
|
|
@ -115,7 +131,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean createDirectory(String path) { |
|
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path + "/", StringKit.EMPTY); |
|
|
|
|
PutObjectRequest request = new PutObjectRequest(bucket, path + "/", new ByteArrayInputStream(new byte[0]), new ObjectMetadata()); |
|
|
|
|
try { |
|
|
|
|
s3.putObject(request); |
|
|
|
|
} catch (Exception e) { |
|
|
|
@ -138,7 +154,7 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
@Override |
|
|
|
|
public boolean exist(String path) { |
|
|
|
|
try { |
|
|
|
|
return s3.getObject(bucket, path) != null; |
|
|
|
|
return s3.doesObjectExist(bucket, path); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -147,14 +163,22 @@ public class S3ResourceRepository extends BaseResourceRepository {
|
|
|
|
|
@Override |
|
|
|
|
public String[] list(String dir, final Filter<String> filter) { |
|
|
|
|
List<String> result = new ArrayList<>(); |
|
|
|
|
if (!dir.endsWith(DELIMITER)) { |
|
|
|
|
dir += DELIMITER; |
|
|
|
|
} |
|
|
|
|
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) |
|
|
|
|
.withPrefix(dir).withDelimiter("/"); |
|
|
|
|
.withPrefix(dir).withDelimiter(DELIMITER); |
|
|
|
|
ObjectListing objectListing = s3.listObjects(listObjectsRequest); |
|
|
|
|
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { |
|
|
|
|
if (filter.accept(objectSummary.getKey())) { |
|
|
|
|
result.add(objectSummary.getKey()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (String prefix : objectListing.getCommonPrefixes()) { |
|
|
|
|
if (filter.accept(prefix)) { |
|
|
|
|
result.add(prefix); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result.toArray(new String[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|