Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Recursively upload a local directory to an Amazon Simple Storage Service (Amazon S3) bucket
The following code example shows how to upload a local directory recursively to an Amazon Simple Storage Service (Amazon S3) bucket.
- Java
-
- SDK for Java 2.x
-
Use an S3TransferManager to upload a local directory. View the complete file and test.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.CompletedDirectoryUpload;
import software.amazon.awssdk.transfer.s3.model.DirectoryUpload;
import software.amazon.awssdk.transfer.s3.model.UploadDirectoryRequest;
import java.net.URL;
import java.nio.file.Paths;
import java.util.UUID;
public Integer uploadDirectory(S3TransferManager transferManager,
String sourceDirectory, String bucketName){
DirectoryUpload directoryUpload =
transferManager.uploadDirectory(UploadDirectoryRequest.builder()
.source(Paths.get(sourceDirectory))
.bucket(bucketName)
.build());
CompletedDirectoryUpload completedDirectoryUpload = directoryUpload.completionFuture().join();
completedDirectoryUpload.failedTransfers().forEach(fail ->
logger.warn("Object [{}] failed to transfer", fail.toString()));
return completedDirectoryUpload.failedTransfers().size();
}
For a complete list of Amazon SDK developer guides and code examples, see
Using this service with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.