[英]Returning list of objects inside S3 bucket using S3AsynClient and SdkPublisher
我正在嘗試使用aws-java sdk 2
和S3 async client
檢索S3 bucket
的內容列表。 然而,在運行下面的代碼后,我沒有看到任何 output,即使桶中有一個 object。 我在這里錯過了什么嗎?
ListObjectsV2Request request = ListObjectsV2Request .builder().bucket("my-bucket").build();
ListObjectsV2Publisher response = s3AsyncClient.listObjectsV2Paginator(request);
response.contents().subscribe(s3object -> System.out.println(s3object));
要使用S3AsyncClient客戶端使用listObjectsV2方法列出存儲桶中的對象,您可以使用此代碼(您的 Paginator 調用對我也不起作用)。
public static void listObjects(S3AsyncClient s3AsyncClient, String bucketName) {
try {
ListObjectsV2Request listReq = ListObjectsV2Request.builder()
.bucket(bucketName)
.build();
CompletableFuture<ListObjectsV2Response> future = s3AsyncClient.listObjectsV2(listReq);
future.whenComplete((resp, err) -> {
try {
if (resp != null) {
List<S3Object> objects = resp.contents();
for (S3Object myValue : objects) {
System.out.print("\n The name of the key is " + myValue.key());
}
} else {
// Handle error.
err.printStackTrace();
}
} finally {
// Only close the client when you are completely done with it.
s3AsyncClient.close();
}
});
future.join();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.