[英]How to copy large amount of files from S3 folder to another
我正在嘗試將大量文件(每個文件最大大小300Kb)從S3文件夾移動到另一個文件夾。
我正在使用AWS sdk for java,並嘗試移動1500個文件。
花了太多時間,文件數量可能會增加到10,000。
對於每個文件副本,需要從源文件夾中刪除,因為沒有移動文件的方法。
這是我試過的:
public void moveFiles(String fromKey, String toKey) {
Stream<S3ObjectSummary> objectSummeriesStream = this.getObjectSummeries(fromKey);
objectSummeriesStream.forEach(file ->
{
this.s3Bean.copyObject(bucketName, file.getKey(), bucketName, toKey);
this.s3Bean.deleteObject(bucketName, file.getKey());
});
}
private Stream<S3ObjectSummary> getObjectSummeries(String key) {
// get the files that their prefix is "key" (can be consider as Folders).
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(this.bucketName)
.withPrefix(key);
ObjectListing outFilesList = this.s3Bean.listObjects(listObjectsRequest);
return outFilesList.getObjectSummaries()
.stream()
.filter(x -> !x.getKey()
.equals(key));
}
如果您使用的是Java應用程序,則可以嘗試使用多個線程來復制文件:
private ExecutorService executorService = Executors.fixed(20);
public void moveFiles(String fromKey, String toKey) {
Stream<S3ObjectSummary> objectSummeriesStream =
this.getObjectSummeries(fromKey);
objectSummeriesStream.forEach(file ->
{
executorService.submit(() ->
this.s3Bean.copyObject(bucketName, file.getKey(), bucketName, toKey);
this.s3Bean.deleteObject(bucketName, file.getKey());
)};
});
}
這應該加快這個過程。
另一種方法可能是使用AWS-lambda。 一旦文件出現在源存儲桶中,您就可以將事件放入SQS FIFO隊列中。 lambda將通過此事件啟動單個文件副本。 如果我沒有並行錯誤,你可以啟動多達500個lambdas實例。 應該快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.