從Scala或Java程序檢查S3存儲桶是否具有匹配某個特定鍵模式的對象的好方法是什麼?也就是說,如果我有一個名爲「CsvBucket」的桶,我該如何檢查它是否包含鍵匹配模式「processed/files/2015/8/*。csv」的對象?Java中的方式檢查S3存儲桶是否具有與通配符模式匹配的鍵的對象
感謝
從Scala或Java程序檢查S3存儲桶是否具有匹配某個特定鍵模式的對象的好方法是什麼?也就是說,如果我有一個名爲「CsvBucket」的桶,我該如何檢查它是否包含鍵匹配模式「processed/files/2015/8/*。csv」的對象?Java中的方式檢查S3存儲桶是否具有與通配符模式匹配的鍵的對象
感謝
由於S3對象鍵只是String
是你可以只在它們之間迭代,並使用正則表達式測試每個。也許這樣的事情(使用jets3t
庫):
Pattern pattern = Pattern.compile(".*\\.csv");
// 'service' is an instance of S3Service
S3Bucket bucket = service.getBucket(bucketName);
S3Object[] files = service.listObjects(bucket, "processed/files/2015/8", null);
for (int i = 0; i < files.length; i++)
{
if (pattern.matches(files[i].getKey()))
{
// ... work with the file ...
}
}
另一種方式來做到這一點 - http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html
import java.io.IOException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
public class ListKeys {
private static String bucketName = "***bucket name***";
public static void main(String[] args) throws IOException {
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
try {
System.out.println("Listing objects");
final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName).withMaxKeys(2);
ListObjectsV2Result result;
do {
result = s3client.listObjectsV2(req);
for (S3ObjectSummary objectSummary :
result.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() +
")");
}
System.out.println("Next Continuation Token : " + result.getNextContinuationToken());
req.setContinuationToken(result.getNextContinuationToken());
} while(result.isTruncated() == true);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, " +
"which means your request made it " +
"to Amazon S3, but was rejected with an error response " +
"for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, " +
"which means the client encountered " +
"an internal error while trying to communicate" +
" with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
}
我會說這http://codereview.stackexchange.com/questions/6847/list-objects-in-a-amazon-s3-folder-without-also-listing-objects-in-sub-folders,然後通過模式過濾器手動運行結果。 – zapl
@zapl尚未編寫的代碼是題外話 – Caridorc
@Caridorc我從來沒有聽說過這個規則。但我知道我不應該在評論中回答問題。隨意用書面的代碼寫出一個很好的答案,並獲得一些積分。 – zapl