gpt4 book ai didi

java - aws将目录从一个位置递归复制到同一s3存储桶中的另一个位置

转载 作者:行者123 更新时间:2023-12-02 12:12:24 24 4
gpt4 key购买 nike

如标题所述,如何将目录从一个位置递归复制到同一 s3 存储桶中的另一个位置?我正在使用 java api,我注意到类 TransferMnager可以帮助从 s3 复制到 s3,但我找不到允许递归复制 dir 的 api。有人可以帮助我吗?谢谢

最佳答案

没有任何用于递归复制的 Java API。您可以将 AWS CLI 与 --recursive 标志一起使用,但它会遍历并复制幕后的所有文件。

此外,您可以实现类似的东西;

public void run() throws ExecutionException {
String bucketName = "someBucket";
String folderPath = "folder/path";
String sse = "AES256";
setEncryptionForBucket(bucketName, folderPath, sse);
}

private void setEncryptionForBucket(String bucketName, String folderPath, String sse) throws ExecutionException {
LOGGER.info("Setting encryption for: [" + bucketName + "] with encryption type: [" + sse + "] and " + (folderPath != null ? "with folder path: /" + folderPath : ""));
AmazonS3 s3Client = factory.create(AmazonS3Client.class);
TransferManager transferManager = TransferManagerBuilder.standard().build();
List<String> failedKeys = new ArrayList<>();

StopWatch stopWatch = new StopWatch();
stopWatch.start();
ParallelTaskExecutor parallelTaskExecutor = new ParallelTaskExecutor(THREAD_COUNT);
s3Client.listObjectsV2(bucketName, folderPath)
.getObjectSummaries().stream().map(S3ObjectSummary::getKey).forEach(key ->
parallelTaskExecutor.submit(() -> setEncryptionForKey(transferManager, bucketName, key, failedKeys))
);

parallelTaskExecutor.waitForTasks();
transferManager.shutdownNow();
LOGGER.info("Task executed in " + stopWatch.getTime() / 1000 + " sec.");

if (!failedKeys.isEmpty()) {
LOGGER.error("Some of the keys cannot be encrypted: " + failedKeys);
throw new RuntimeException("Some of the keys cannot be encrypted: " + failedKeys);
}

LOGGER.info("All objects encrypted successfully.");
}

private void setEncryptionForKey(TransferManager transferManager, String bucketName, String key, List<String> failedKeys) {
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, key, bucketName, key);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
copyObjectRequest.setNewObjectMetadata(objectMetadata);

StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
Copy xfer = transferManager.copy(copyObjectRequest);
xfer.waitForCompletion();
LOGGER.info("Xfer setting encryption successfully done for: " + key + " in " + stopWatch.getTime() + " ms.");
} catch (Exception e) {
LOGGER.error("Xfer failed for: " + key, e);
failedKeys.add(key);
}
}

关于java - aws将目录从一个位置递归复制到同一s3存储桶中的另一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46441008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com