如何使用Java列出bucket中的所有AWS S3对象
使用Java获取S3存储桶中所有项目列表的最简单方法是什么如何使用Java列出bucket中的所有AWS S3对象,java,amazon-s3,Java,Amazon S3,使用Java获取S3存储桶中所有项目列表的最简单方法是什么 List<S3ObjectSummary> s3objects = s3.listObjects(bucketName,prefix).getObjectSummaries(); List s3objects=s3.listObjects(bucketName,前缀).getObjectSummaries(); 此示例仅返回1000项。这可能是一个解决方法,但解决了我的问题: ObjectListing listing
List<S3ObjectSummary> s3objects = s3.listObjects(bucketName,prefix).getObjectSummaries();
List s3objects=s3.listObjects(bucketName,前缀).getObjectSummaries();
此示例仅返回1000项。这可能是一个解决方法,但解决了我的问题:
ObjectListing listing = s3.listObjects( bucketName, prefix );
List<S3ObjectSummary> summaries = listing.getObjectSummaries();
while (listing.isTruncated()) {
listing = s3.listNextBatchOfObjects (listing);
summaries.addAll (listing.getObjectSummaries());
}
ObjectListing=s3.listObjects(bucketName,前缀);
List summaries=listing.getObjectSummaries();
while(listing.isTruncated()){
listing=s3.listenxtbatchofobjects(listing);
summaries.addAll(listing.getObjectSummaries());
}
格雷你的解决方案很奇怪,但你看起来是个好人
AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials( ....
ObjectListing images = s3Client.listObjects(bucketName);
List<S3ObjectSummary> list = images.getObjectSummaries();
for(S3ObjectSummary image: list) {
S3Object obj = s3Client.getObject(bucketName, image.getKey());
writeToFile(obj.getObjectContent());
}
AmazonS3Client s3Client=新的AmazonS3Client(新的BasicAWSCredentials(。。。。
ObjectListing images=s3Client.listObjects(bucketName);
List=images.getObjectSummaries();
用于(S3ObjectSummary图像:列表){
S3Object obj=s3Client.getObject(bucketName,image.getKey());
writeToFile(obj.getObjectContent());
}
当S3对象可能被截断时,作为列出S3对象的更简洁的解决方案:
ListObjectsRequest request = new ListObjectsRequest().withBucketName(bucketName);
ObjectListing listing = null;
while((listing == null) || (request.getMarker() != null)) {
listing = s3Client.listObjects(request);
// do stuff with listing
request.setMarker(listing.getNextMarker());
}
试试这个
public void getObjectList(){
System.out.println("Listing objects");
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("ads"));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
}
}
您可以使用特定的前缀来查看bucket中的所有对象。我正在处理系统生成的大量对象集合;我们更改了存储数据的格式,需要检查每个文件,确定哪些文件是旧格式的,并对其进行转换。还有其他方法可以做到这一点,但此方法与您的问题有关n
ObjectListing list = amazonS3Client.listObjects(contentBucketName, contentKeyPrefix);
do {
List<S3ObjectSummary> summaries = list.getObjectSummaries();
for (S3ObjectSummary summary : summaries) {
String summaryKey = summary.getKey();
/* Retrieve object */
/* Process it */
}
list = amazonS3Client.listNextBatchOfObjects(list);
}while (list.isTruncated());
ObjectListing list=amazonS3Client.listObjects(contentBucketName,contentKeyPrefix);
做{
List summaries=List.getObjectSummaries();
对于(S3ObjectSummary摘要:摘要){
字符串summaryKey=summary.getKey();
/*检索对象*/
/*处理它*/
}
列表=amazonS3Client.ListNextBatchOfObject(列表);
}while(list.isTruncated());
这直接来自AWS文档:
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("m");
ObjectListing objectListing;
do {
objectListing = s3client.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary :
objectListing.getObjectSummaries()) {
System.out.println( " - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() +
")");
}
listObjectsRequest.setMarker(objectListing.getNextMarker());
} while (objectListing.isTruncated());
我知道这是一篇老文章,但这可能对任何人都有用:2.1版的Java/Android SDK提供了一个名为setMaxKeys的方法。如下所示:
s3objects.setMaxKeys(arg0)
您现在可能已经找到了解决方案,但请检查其中一个答案是否正确,以便将来对其他人有所帮助。这对我很有效
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
List<String> listing = getObjectNamesForBucket(bucket, s3Client);
Log.e(TAG, "listing "+ listing);
}
catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Exception found while listing "+ e);
}
}
});
thread.start();
private List<String> getObjectNamesForBucket(String bucket, AmazonS3 s3Client) {
ObjectListing objects=s3Client.listObjects(bucket);
List<String> objectNames=new ArrayList<String>(objects.getObjectSummaries().size());
Iterator<S3ObjectSummary> oIter=objects.getObjectSummaries().iterator();
while (oIter.hasNext()) {
objectNames.add(oIter.next().getKey());
}
while (objects.isTruncated()) {
objects=s3Client.listNextBatchOfObjects(objects);
oIter=objects.getObjectSummaries().iterator();
while (oIter.hasNext()) {
objectNames.add(oIter.next().getKey());
}
}
return objectNames;
}
Thread-Thread=新线程(new Runnable()){
@凌驾
公开募捐{
试一试{
List listing=getObjectNamesForBucket(bucket,s3Client);
Log.e(标记“listing”+listing);
}
捕获(例外e){
e、 printStackTrace();
Log.e(标记“在列出“+e”时发现异常);
}
}
});
thread.start();
私有列表getObjectNamesForBucket(字符串bucket,AmazonS3 s3Client){
ObjectListing objects=s3Client.listObjects(bucket);
List objectNames=new ArrayList(objects.getObjectSummaries().size());
迭代器oIter=objects.getObjectSummaries().Iterator();
while(oIter.hasNext()){
add(oIter.next().getKey());
}
while(objects.isTruncated()){
objects=s3Client.listNextBatchOfObjects(objects);
oIter=objects.getObjectSummaries().iterator();
while(oIter.hasNext()){
add(oIter.next().getKey());
}
}
返回对象名称;
}
使用AWS Java SDK列出密钥
对于那些在2018+阅读本文的人来说,有两个新的分页无障碍API可用:一个在AWS SDK For Java 1.x中,另一个在2.x中 1.x Java SDK中有一个允许您迭代S3 bucket中的对象而不必处理分页的功能:
AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
S3Objects.inBucket(s3, "the-bucket").forEach((S3ObjectSummary objectSummary) -> {
// TODO: Consume `objectSummary` the way you need
System.out.println(objectSummary.key);
});
此迭代是惰性的:
根据需要,将一次一页地惰性地获取s列表。可以使用该方法控制页面的大小
2.x
API已更改,因此以下是SDK 2.x版本:
S3Client client = S3Client.builder().region(Region.US_EAST_1).build();
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket("the-bucket").prefix("the-prefix").build();
ListObjectsV2Iterable response = client.listObjectsV2Paginator(request);
for (ListObjectsV2Response page : response) {
page.contents().forEach((S3Object object) -> {
// TODO: Consume `object` the way you need
System.out.println(object.key());
});
}
他也很懒:
调用该操作时,将返回该类的一个实例。此时,尚未进行任何服务调用,因此无法保证该请求有效。当您迭代iterable时,SDK将开始通过进行服务调用延迟加载响应页,直到没有剩余页或迭代停止。如果存在在您的请求中,只有在开始迭代iterable之后才能看到失败
您不希望一次列出bucket中的所有1000个对象。更可靠的解决方案是一次最多获取10个对象。您可以使用该方法来实现这一点 下面的代码创建一个S3客户端,一次获取10个或更少的对象,并基于前缀进行筛选,并为获取的对象生成一个:
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import java.net.URL;
import java.util.Date;
/**
* @author shabab
* @since 21 Sep, 2020
*/
public class AwsMain {
static final String ACCESS_KEY = "";
static final String SECRET = "";
static final Regions BUCKET_REGION = Regions.DEFAULT_REGION;
static final String BUCKET_NAME = "";
public static void main(String[] args) {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET);
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder
.standard()
.withRegion(BUCKET_REGION)
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(BUCKET_NAME).withMaxKeys(10);
ListObjectsV2Result result;
do {
result = s3Client.listObjectsV2(req);
result.getObjectSummaries()
.stream()
.filter(s3ObjectSummary -> {
return s3ObjectSummary.getKey().contains("Market-subscriptions/")
&& !s3ObjectSummary.getKey().equals("Market-subscriptions/");
})
.forEach(s3ObjectSummary -> {
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(BUCKET_NAME, s3ObjectSummary.getKey())
.withMethod(HttpMethod.GET)
.withExpiration(getExpirationDate());
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(s3ObjectSummary.getKey() + " Pre-Signed URL: " + url.toString());
});
String token = result.getNextContinuationToken();
req.setContinuationToken(token);
} while (result.isTruncated());
} catch (SdkClientException e) {
e.printStackTrace();
}
}
private static Date getExpirationDate() {
Date expiration = new java.util.Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60;
expiration.setTime(expTimeMillis);
return expiration;
}
}
这个问题应该进行编辑,以提供您正在使用的S3包和版本。这对我很有用:他们如何能够硬编码1000个文件。这是一个很容易出现错误的问题,我现在需要修复我的产品。这也是一个很棒的链接-在我看来,这似乎不是API的预期用途。有人建议编辑您的answ呃,如果您感兴趣,3.listObjects的默认限制是每个列表包含1000个元素,因此@JoachimSauer说这是API的预期用途。是否可以同时获取版本信息?这使得危险的假设是,
getObjectSummaries()返回的List
)
是可变的。据我所知,此解决方案将只获取前1000 kyes/个文件并打印它们。但不会进一步迭代更多文件。不,只有1000个文件限制,您没有阅读上面的内容,您的解决方案也有相同的问题。我们的一些答案帮助很大,但我想询问更多信息。我想像Sp一样迭代页面可分页,例如,请求前20个对象,如果需要,我可以请求第二个页面和下一个20个对象。是否可能?
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import java.net.URL;
import java.util.Date;
/**
* @author shabab
* @since 21 Sep, 2020
*/
public class AwsMain {
static final String ACCESS_KEY = "";
static final String SECRET = "";
static final Regions BUCKET_REGION = Regions.DEFAULT_REGION;
static final String BUCKET_NAME = "";
public static void main(String[] args) {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET);
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder
.standard()
.withRegion(BUCKET_REGION)
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(BUCKET_NAME).withMaxKeys(10);
ListObjectsV2Result result;
do {
result = s3Client.listObjectsV2(req);
result.getObjectSummaries()
.stream()
.filter(s3ObjectSummary -> {
return s3ObjectSummary.getKey().contains("Market-subscriptions/")
&& !s3ObjectSummary.getKey().equals("Market-subscriptions/");
})
.forEach(s3ObjectSummary -> {
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(BUCKET_NAME, s3ObjectSummary.getKey())
.withMethod(HttpMethod.GET)
.withExpiration(getExpirationDate());
URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(s3ObjectSummary.getKey() + " Pre-Signed URL: " + url.toString());
});
String token = result.getNextContinuationToken();
req.setContinuationToken(token);
} while (result.isTruncated());
} catch (SdkClientException e) {
e.printStackTrace();
}
}
private static Date getExpirationDate() {
Date expiration = new java.util.Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60;
expiration.setTime(expTimeMillis);
return expiration;
}
}