xref: /aosp_15_r20/external/aws-sdk-java-v2/services-custom/s3-transfer-manager/README.md (revision 8a52c7834d808308836a99fc2a6e0ed8db339086)
1*8a52c783SCole Faust## Overview
2*8a52c783SCole Faust
3*8a52c783SCole FaustThe S3 Transfer Manager is a high-level transfer utility built on top of the asynchronous S3 client.
4*8a52c783SCole FaustIt provides a simple API to allow you to transfer files and directories between your application
5*8a52c783SCole Faustand Amazon S3. The S3 Transfer Manager also enables you to monitor a transfer's progress in real-time,
6*8a52c783SCole Faustas well as pause the transfer for execution at a later time.
7*8a52c783SCole Faust
8*8a52c783SCole Faust## Getting Started
9*8a52c783SCole Faust
10*8a52c783SCole Faust### Add a dependency for the S3 Transfer Manager
11*8a52c783SCole Faust
12*8a52c783SCole FaustFirst, you need to include `s3-transfer-manager` and `aws-crt` in your project.
13*8a52c783SCole Faust
14*8a52c783SCole Faust```xml
15*8a52c783SCole Faust<dependency>
16*8a52c783SCole Faust  <groupId>software.amazon.awssdk</groupId>
17*8a52c783SCole Faust  <artifactId>s3-transfer-manager</artifactId>
18*8a52c783SCole Faust  <version>${awsjavasdk.version}</version>
19*8a52c783SCole Faust</dependency>
20*8a52c783SCole Faust<dependency>
21*8a52c783SCole Faust  <groupId>software.amazon.awssdk.crt</groupId>
22*8a52c783SCole Faust  <artifactId>aws-crt</artifactId>
23*8a52c783SCole Faust  <version>${awscrt.version}</version>
24*8a52c783SCole Faust</dependency>
25*8a52c783SCole Faust```
26*8a52c783SCole Faust
27*8a52c783SCole FaustNote that you need to replace `${awsjavasdk.version}` and `${awscrt.version}` with the latest
28*8a52c783SCole Faustversion.
29*8a52c783SCole Faust
30*8a52c783SCole Faust### Instantiate the S3 Transfer Manager
31*8a52c783SCole Faust
32*8a52c783SCole FaustYou can instantiate the transfer manager easily using the default settings:
33*8a52c783SCole Faust
34*8a52c783SCole Faust```java
35*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
36*8a52c783SCole Faust```
37*8a52c783SCole Faust
38*8a52c783SCole FaustIf you wish to configure settings, or use an underlying CRT-based S3 client you have already constructed,
39*8a52c783SCole Faustwe recommend using the builder instead:
40*8a52c783SCole Faust
41*8a52c783SCole Faust
42*8a52c783SCole Faust```java
43*8a52c783SCole FaustS3AsyncClient s3AsyncClient =
44*8a52c783SCole Faust    S3AsyncClient.crtBuilder()
45*8a52c783SCole Faust                 .credentialsProvider(DefaultCredentialsProvider.create())
46*8a52c783SCole Faust                 .region(Region.US_WEST_2)
47*8a52c783SCole Faust                 .targetThroughputInGbps(20.0)
48*8a52c783SCole Faust                 .minimumPartSizeInBytes(8 * MB)
49*8a52c783SCole Faust                 .build();
50*8a52c783SCole Faust
51*8a52c783SCole FaustS3TransferManager transferManager =
52*8a52c783SCole Faust    S3TransferManager.builder()
53*8a52c783SCole Faust                     .s3Client(s3AsyncClient)
54*8a52c783SCole Faust                     .build();
55*8a52c783SCole Faust```
56*8a52c783SCole Faust
57*8a52c783SCole Faust### Transfer a single object
58*8a52c783SCole Faust
59*8a52c783SCole Faust#### Upload a file to S3 and log the upload’s progress with a TransferListener
60*8a52c783SCole FaustTo upload a file to Amazon S3, you need to provide the source file path and a PutObjectRequest specifying the target bucket and key.
61*8a52c783SCole FaustOptionally, you can monitor the progress of the transfer by attaching a TransferListener. The provided LoggingTransferListener
62*8a52c783SCole Faustlogs a basic progress bar; users can also implement their own listeners.
63*8a52c783SCole Faust
64*8a52c783SCole Faust```java
65*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
66*8a52c783SCole Faust
67*8a52c783SCole FaustUploadFileRequest uploadFileRequest =
68*8a52c783SCole Faust    UploadFileRequest.builder()
69*8a52c783SCole Faust                     .putObjectRequest(req -> req.bucket("bucket").key("key"))
70*8a52c783SCole Faust                      // attaching a LoggingTransferListener that will log the progress
71*8a52c783SCole Faust                     .addTransferListener(LoggingTransferListener.create())
72*8a52c783SCole Faust                     .source(Paths.get("myFile.txt"))
73*8a52c783SCole Faust                     .build();
74*8a52c783SCole Faust
75*8a52c783SCole FaustFileUpload upload = transferManager.uploadFile(uploadFileRequest);
76*8a52c783SCole Faust
77*8a52c783SCole Faust    // Wait for the transfer to complete
78*8a52c783SCole Faust    upload.completionFuture().join();
79*8a52c783SCole Faust```
80*8a52c783SCole Faust
81*8a52c783SCole Faust#### Download an S3 object to a local file and log the download’s progress with a TransferListener
82*8a52c783SCole Faust
83*8a52c783SCole FaustTo download an object, you need to provide the destination file path and a GetObjectRequest specifying the source bucket and key.
84*8a52c783SCole FaustSame as upload, you can monitor the progress of the transfer by attaching a TransferListener.
85*8a52c783SCole Faust
86*8a52c783SCole Faust```java
87*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
88*8a52c783SCole Faust
89*8a52c783SCole FaustDownloadFileRequest downloadFileRequest =
90*8a52c783SCole FaustDownloadFileRequest.builder()
91*8a52c783SCole Faust                   .getObjectRequest(req -> req.bucket("bucket").key("key"))
92*8a52c783SCole Faust                   .destination(Paths.get("myFile.txt"))
93*8a52c783SCole Faust                    // attaching a LoggingTransferListener that will log the progress
94*8a52c783SCole Faust                   .addTransferListener(LoggingTransferListener.create())
95*8a52c783SCole Faust                   .build();
96*8a52c783SCole Faust
97*8a52c783SCole FaustFileDownload download = transferManager.downloadFile(downloadFileRequest);
98*8a52c783SCole Faust
99*8a52c783SCole Faust// Wait for the transfer to complete
100*8a52c783SCole Faustdownload.completionFuture().join();
101*8a52c783SCole Faust```
102*8a52c783SCole Faust
103*8a52c783SCole Faust#### Copy an S3 object from one location to another
104*8a52c783SCole FaustTo copy an object, you need to provide a CopyObjectRequest with source and destination location.
105*8a52c783SCole Faust
106*8a52c783SCole Faust```
107*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
108*8a52c783SCole FaustCopyObjectRequest copyObjectRequest = CopyObjectRequest.builder()
109*8a52c783SCole Faust                                                       .sourceBucket("source_bucket")
110*8a52c783SCole Faust                                                       .sourceKey("source_key")
111*8a52c783SCole Faust                                                       .destinationBucket("dest_bucket")
112*8a52c783SCole Faust                                                       .destinationKey("dest_key")
113*8a52c783SCole Faust                                                       .build();
114*8a52c783SCole FaustCopyRequest copyRequest = CopyRequest.builder()
115*8a52c783SCole Faust                                     .copyObjectRequest(copyObjectRequest)
116*8a52c783SCole Faust                                     .build();
117*8a52c783SCole Faust
118*8a52c783SCole FaustCopy copy = transferManager.copy(copyRequest);
119*8a52c783SCole Faust
120*8a52c783SCole Faust// Wait for the transfer to complete
121*8a52c783SCole FaustCompletedCopy completedCopy = copy.completionFuture().join();
122*8a52c783SCole Faust```
123*8a52c783SCole Faust
124*8a52c783SCole Faust### Transfer multiple objects in the same directory
125*8a52c783SCole Faust
126*8a52c783SCole Faust#### Upload a local directory to an S3 bucket
127*8a52c783SCole Faust
128*8a52c783SCole FaustTo upload a local directory recursively to an S3 bucket, you need to provide the source directory and the target bucket.
129*8a52c783SCole Faust
130*8a52c783SCole Faust```java
131*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
132*8a52c783SCole FaustDirectoryUpload directoryUpload = transferManager.uploadDirectory(UploadDirectoryRequest.builder()
133*8a52c783SCole Faust                                                 .sourceDirectory(Paths.get("source/directory"))
134*8a52c783SCole Faust                                                 .bucket("bucket")
135*8a52c783SCole Faust                                                 .build());
136*8a52c783SCole Faust
137*8a52c783SCole Faust// Wait for the transfer to complete
138*8a52c783SCole FaustCompletedDirectoryUpload completedDirectoryUpload = directoryUpload.completionFuture().join();
139*8a52c783SCole Faust
140*8a52c783SCole Faust// Print out any failed uploads
141*8a52c783SCole FaustcompletedDirectoryUpload.failedTransfers().forEach(System.out::println);
142*8a52c783SCole Faust```
143*8a52c783SCole Faust
144*8a52c783SCole Faust#### Download S3 objects within the same bucket to a local directory
145*8a52c783SCole Faust
146*8a52c783SCole FaustTo download all S3 objects within the same bucket, you need to provide the destination directory and the source bucket.
147*8a52c783SCole Faust
148*8a52c783SCole Faust```java
149*8a52c783SCole FaustS3TransferManager transferManager = S3TransferManager.create();
150*8a52c783SCole Faust         DirectoryDownload directoryDownload =
151*8a52c783SCole Faust             transferManager.downloadDirectory(
152*8a52c783SCole Faust                  DownloadDirectoryRequest.builder()
153*8a52c783SCole Faust                                          .destination(Paths.get("destination/directory"))
154*8a52c783SCole Faust                                          .bucket("bucket")
155*8a52c783SCole Faust                                          // only download objects with prefix "photos"
156*8a52c783SCole Faust                                          .listObjectsV2RequestTransformer(l -> l.prefix("photos"))
157*8a52c783SCole Faust                                          .build());
158*8a52c783SCole Faust// Wait for the transfer to complete
159*8a52c783SCole FaustCompletedDirectoryDownload completedDirectoryDownload = directoryDownload.completionFuture().join();
160*8a52c783SCole Faust
161*8a52c783SCole Faust// Print out any failed downloads
162*8a52c783SCole FaustcompletedDirectoryDownload.failedTransfers().forEach(System.out::println);
163*8a52c783SCole Faust```
164