-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
TransferManager.java
2452 lines (2273 loc) · 115 KB
/
TransferManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.s3.transfer;
import static com.amazonaws.services.s3.internal.ServiceUtils.APPEND_MODE;
import static com.amazonaws.services.s3.internal.ServiceUtils.OVERWRITE_MODE;
import com.amazonaws.AbortedException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.SdkClientException;
import com.amazonaws.annotation.SdkInternalApi;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.event.ProgressListenerChain;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3Encryption;
import com.amazonaws.services.s3.AmazonS3EncryptionV2;
import com.amazonaws.services.s3.internal.FileLocks;
import com.amazonaws.services.s3.internal.Mimetypes;
import com.amazonaws.services.s3.internal.RequestCopyUtils;
import com.amazonaws.services.s3.internal.ServiceUtils;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.GetObjectMetadataRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListMultipartUploadsRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.MultipartUpload;
import com.amazonaws.services.s3.model.MultipartUploadListing;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.ObjectTagging;
import com.amazonaws.services.s3.model.PresignedUrlDownloadConfig;
import com.amazonaws.services.s3.model.PresignedUrlDownloadRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.s3.transfer.Transfer.TransferState;
import com.amazonaws.services.s3.transfer.exception.FileLockException;
import com.amazonaws.services.s3.transfer.internal.CopyCallable;
import com.amazonaws.services.s3.transfer.internal.CopyImpl;
import com.amazonaws.services.s3.transfer.internal.CopyMonitor;
import com.amazonaws.services.s3.transfer.internal.DownloadImpl;
import com.amazonaws.services.s3.transfer.internal.DownloadMonitor;
import com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl;
import com.amazonaws.services.s3.transfer.internal.MultipleFileTransferMonitor;
import com.amazonaws.services.s3.transfer.internal.MultipleFileUploadImpl;
import com.amazonaws.services.s3.transfer.internal.PreparedDownloadContext;
import com.amazonaws.services.s3.transfer.internal.PresignUrlDownloadCallable;
import com.amazonaws.services.s3.transfer.internal.PresignedUrlDownloadImpl;
import com.amazonaws.services.s3.transfer.internal.S3ProgressListener;
import com.amazonaws.services.s3.transfer.internal.S3ProgressListenerChain;
import com.amazonaws.services.s3.transfer.internal.TransferManagerUtils;
import com.amazonaws.services.s3.transfer.internal.TransferProgressUpdatingListener;
import com.amazonaws.services.s3.transfer.internal.TransferStateChangeListener;
import com.amazonaws.services.s3.transfer.internal.UploadCallable;
import com.amazonaws.services.s3.transfer.internal.UploadImpl;
import com.amazonaws.services.s3.transfer.internal.UploadMonitor;
import com.amazonaws.util.IOUtils;
import com.amazonaws.util.VersionInfoUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* High level utility for managing transfers to Amazon S3.
* <p>
* <code>TransferManager</code> provides a simple API for uploading content to
* Amazon S3, and makes extensive use of Amazon S3 multipart uploads to achieve
* enhanced throughput, performance and reliability.
* <p>
* When possible, <code>TransferManager</code> attempts to use multiple threads
* to upload multiple parts of a single upload at once. When dealing with large
* content sizes and high bandwidth, this can have a significant increase on
* throughput.
* <p>
* <code>TransferManager</code> is responsible for managing resources such as
* connections and threads; share a single instance of
* <code>TransferManager</code> whenever possible. <code>TransferManager</code>,
* like all the client classes in the Amazon Web Services SDK for Java, is thread safe. Call
* <code> TransferManager.shutdownNow()</code> to release the resources once the
* transfer is complete.
* <p>
* Using <code>TransferManager</code> to upload objects to Amazon S3 is easy:
*
* <pre class="brush: java">
* DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
* TransferManager tx = new TransferManager(
* credentialProviderChain.getCredentials());
* Upload myUpload = tx.upload(myBucket, myFile.getName(), myFile);
*
* // You can poll your transfer's status to check its progress
* if (myUpload.isDone() == false) {
* System.out.println("Transfer: " + myUpload.getDescription());
* System.out.println(" - State: " + myUpload.getState());
* System.out.println(" - Progress: "
* + myUpload.getProgress().getBytesTransferred());
* }
*
* // Transfers also allow you to set a <code>ProgressListener</code> to receive
* // asynchronous notifications about your transfer's progress.
* myUpload.addProgressListener(myProgressListener);
*
* // Or you can block the current thread and wait for your transfer to
* // to complete. If the transfer fails, this method will throw an
* // AmazonClientException or AmazonServiceException detailing the reason.
* myUpload.waitForCompletion();
*
* // After the upload is complete, call shutdownNow to release the resources.
* tx.shutdownNow();
* </pre>
* <p>
* Transfers can be paused and resumed at a later time. It can also survive JVM
* crash, provided the information that is required to resume the transfer is
* given as input to the resume operation. For more information on pause and resume,
* @see Upload#pause()
* @see Download#pause()
* @see TransferManager#resumeUpload(PersistableUpload)
* @see TransferManager#resumeDownload(PersistableDownload)
*/
public class TransferManager {
/** The low level client we use to make the actual calls to Amazon S3. */
private final AmazonS3 s3;
/** Configuration for how TransferManager processes requests. */
private TransferManagerConfiguration configuration;
/** The thread pool in which transfers are uploaded or downloaded. */
private final ExecutorService executorService;
/**
* Thread used for periodically checking transfers and updating their state, as well as enforcing
* timeouts.
*/
private final ScheduledExecutorService timedThreadPool = new ScheduledThreadPoolExecutor(1, daemonThreadFactory);
private static final Log log = LogFactory.getLog(TransferManager.class);
private final boolean shutDownThreadPools;
/**
* Flag indicating whether the transfer manager is mutable or not. Legacy managers built via the
* constructors are mutable. TransferManagers built with the fluent builders are immutable.
*/
private final boolean isImmutable;
/**
* Constructs a new <code>TransferManager</code> and Amazon S3 client using
* the credentials from <code>DefaultAWSCredentialsProviderChain</code>
* <p>
* <code>TransferManager</code> and client objects may pool connections and
* threads. Reuse <code>TransferManager</code> and client objects and share
* them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
* @deprecated use {@link TransferManagerBuilder#defaultTransferManager()}
*/
@Deprecated
public TransferManager(){
this(new AmazonS3Client(new DefaultAWSCredentialsProviderChain()));
}
/**
* Constructs a new <code>TransferManager</code> and Amazon S3 client using
* the specified Amazon Web Services security credentials provider.
* <p>
* <code>TransferManager</code> and client objects may pool connections and
* threads. Reuse <code>TransferManager</code> and client objects and share
* them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
*
* @param credentialsProvider
* The Amazon Web Services security credentials provider to use when making
* authenticated requests.
* @deprecated use {@link TransferManagerBuilder#withS3Client(AmazonS3)} for example:
* {@code TransferManagerBuilder.standard().withS3Client(AmazonS3ClientBuilder.standard.withCredentials(credentialsProvider).build()).build(); }
*/
@Deprecated
public TransferManager(AWSCredentialsProvider credentialsProvider) {
this(new AmazonS3Client(credentialsProvider));
}
/**
* Constructs a new <code>TransferManager</code> and Amazon S3 client using
* the specified Amazon Web Services security credentials.
* <p>
* <code>TransferManager</code> and client objects
* may pool connections and threads.
* Reuse <code>TransferManager</code> and client objects
* and share them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
*
* @param credentials
* The Amazon Web Services security credentials to use when making authenticated
* requests.
* @deprecated use {@link TransferManagerBuilder#withS3Client(AmazonS3)} for example:
* {@code TransferManagerBuilder.standard().withS3Client(AmazonS3ClientBuilder.standard.withCredentials(credentials).build()).build(); }
*/
@Deprecated
public TransferManager(AWSCredentials credentials) {
this(new AmazonS3Client(credentials));
}
/**
* Constructs a new <code>TransferManager</code>,
* specifying the client to use when making
* requests to Amazon S3.
* <p>
* <code>TransferManager</code> and client objects
* may pool connections and threads.
* Reuse <code>TransferManager</code> and client objects
* and share them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
* </p>
*
* @param s3
* The client to use when making requests to Amazon S3.
* @deprecated use {@link TransferManagerBuilder#withS3Client(AmazonS3)}
*/
@Deprecated
public TransferManager(AmazonS3 s3) {
this(s3, TransferManagerUtils.createDefaultExecutorService());
}
/**
* Constructs a new <code>TransferManager</code> specifying the client and
* thread pool to use when making requests to Amazon S3.
* <p>
* <code>TransferManager</code> and client objects may pool connections and
* threads. Reuse <code>TransferManager</code> and client objects and share
* them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
* <p>
* By default, the thread pool will shutdown when the transfer manager
* instance is garbage collected.
*
* @param s3
* The client to use when making requests to Amazon S3.
* @param executorService
* The ExecutorService to use for the TransferManager. It is not recommended to
* use a single threaded executor or a thread pool with a bounded work queue as
* control tasks may submit subtasks that can't complete until all sub tasks
* complete. Using an incorrectly configured thread pool may cause a deadlock (I.E.
* the work queue is filled with control tasks that can't finish until subtasks
* complete but subtasks can't execute because the queue is filled).
*
* @see TransferManager#TransferManager(AmazonS3 s3, ExecutorService
* executorService, boolean shutDownThreadPools)
* @deprecated use {@link TransferManagerBuilder#withS3Client(AmazonS3)} and
* {@link TransferManagerBuilder#withExecutorFactory(com.amazonaws.client.builder.ExecutorFactory)}
*/
@Deprecated
public TransferManager(AmazonS3 s3, ExecutorService executorService) {
this(s3, executorService, true);
}
/**
* Constructs a new <code>TransferManager</code> specifying the client and
* thread pool to use when making requests to Amazon S3.
* <p>
* <code>TransferManager</code> and client objects may pool connections and
* threads. Reuse <code>TransferManager</code> and client objects and share
* them throughout applications.
* <p>
* TransferManager and all Amazon Web Services client objects are thread safe.
*
* @param s3
* The client to use when making requests to Amazon S3.
* @param executorService
* The ExecutorService to use for the TransferManager. It is not recommended to
* use a single threaded executor or a thread pool with a bounded work queue as
* control tasks may submit subtasks that can't complete until all sub tasks
* complete. Using an incorrectly configured thread pool may cause a deadlock (I.E.
* the work queue is filled with control tasks that can't finish until subtasks
* complete but subtasks can't execute because the queue is filled).
* @param shutDownThreadPools
* If set to true, the thread pool will be shutdown when transfer
* manager instance is garbage collected.
* @deprecated use {@link TransferManagerBuilder#withS3Client(AmazonS3)} and
* {@link TransferManagerBuilder#withExecutorFactory(com.amazonaws.client.builder.ExecutorFactory)} and
* {@link TransferManagerBuilder#withShutDownThreadPools(Boolean)}
*/
@Deprecated
public TransferManager(AmazonS3 s3, ExecutorService executorService, boolean shutDownThreadPools) {
this.s3 = s3;
this.executorService = executorService;
this.configuration = new TransferManagerConfiguration();
this.shutDownThreadPools = shutDownThreadPools;
this.isImmutable = false;
}
@SdkInternalApi
TransferManager(TransferManagerParams params) {
this.s3 = params.getS3Client();
this.executorService = params.getExecutorService();
this.configuration = params.getConfiguration();
this.shutDownThreadPools = params.getShutDownThreadPools();
this.isImmutable = true;
}
/**
* Constructor for use by classes that need to extend the TransferManager.
*
* @param builder - transfer manager builder with the required configuration
*/
protected TransferManager(TransferManagerBuilder builder) {
this(builder.getParams());
}
/**
* Sets the configuration which specifies how
* this <code>TransferManager</code> processes requests.
*
* @param configuration
* The new configuration specifying how
* this <code>TransferManager</code>
* processes requests.
* @deprecated use appropriate method on the {@link TransferManagerBuilder} for example:
* {@code TransferManagerBuilder.standard().withMinimumUploadPartSize(100L).build(); }
*/
@Deprecated
public void setConfiguration(TransferManagerConfiguration configuration) {
checkMutability();
this.configuration = configuration;
}
/**
* Returns the configuration which specifies how
* this <code>TransferManager</code> processes requests.
*
* @return The configuration settings for this <code>TransferManager</code>.
*/
public TransferManagerConfiguration getConfiguration() {
return configuration;
}
/**
* Returns the underlying Amazon S3 client used to make requests to
* Amazon S3.
*
* @return The underlying Amazon S3 client used to make requests to
* Amazon S3.
*/
public AmazonS3 getAmazonS3Client() {
return s3;
}
/**
* <p>
* Schedules a new transfer to upload data to Amazon S3. This method is
* non-blocking and returns immediately (i.e. before the upload has
* finished).
* </p>
* <p>
* When uploading options from a stream, callers <b>must</b> supply the size of
* options in the stream through the content length field in the
* <code>ObjectMetadata</code> parameter.
* If no content length is specified for the input
* stream, then TransferManager will attempt to buffer all the stream
* contents in memory and upload the options as a traditional, single part
* upload. Because the entire stream contents must be buffered in memory,
* this can be very expensive, and should be avoided whenever possible.
* </p>
* <p>
* Use the returned <code>Upload</code> object to query the progress of the
* transfer, add listeners for progress events, and wait for the upload to
* complete.
* </p>
* <p>
* If resources are available, the upload will begin immediately.
* Otherwise, the upload is scheduled and started as soon as
* resources become available.
* </p>
* <p>
* If you are uploading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param bucketName
* The name of the bucket to upload the new object to.
* @param key
* The key in the specified bucket by which to store the new
* object.
* @param input
* The input stream containing the options to upload to Amazon S3.
* @param objectMetadata
* Additional information about the object being uploaded,
* including the size of the options, content type, additional
* custom user metadata, etc.
*
* @return A new <code>Upload</code> object to use to check
* the state of the upload, listen for progress notifications,
* and otherwise manage the upload.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Upload upload(final String bucketName, final String key, final InputStream input, ObjectMetadata objectMetadata)
throws AmazonServiceException, AmazonClientException {
return upload(new PutObjectRequest(bucketName, key, input, objectMetadata));
}
/**
* Schedules a new transfer to upload data to Amazon S3. This method is
* non-blocking and returns immediately (i.e. before the upload has
* finished).
* <p>
* The returned Upload object allows you to query the progress of the
* transfer, add listeners for progress events, and wait for the upload to
* complete.
* <p>
* If resources are available, the upload will begin immediately, otherwise
* it will be scheduled and started as soon as resources become available.
* <p>
* If you are uploading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param bucketName
* The name of the bucket to upload the new object to.
* @param key
* The key in the specified bucket by which to store the new
* object.
* @param file
* The file to upload.
*
* @return A new Upload object which can be used to check state of the
* upload, listen for progress notifications, and otherwise manage
* the upload.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Upload upload(final String bucketName, final String key, final File file)
throws AmazonServiceException, AmazonClientException {
return upload(new PutObjectRequest(bucketName, key, file));
}
/**
* <p>
* Schedules a new transfer to upload data to Amazon S3. This method is
* non-blocking and returns immediately (i.e. before the upload has
* finished).
* </p>
* <p>
* Use the returned <code>Upload</code> object to query the progress of the
* transfer, add listeners for progress events, and wait for the upload to
* complete.
* </p>
* <p>
* If resources are available, the upload will begin immediately.
* Otherwise, the upload is scheduled and started as soon as
* resources become available.
* </p>
* <p>
* If you are uploading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param putObjectRequest
* The request containing all the parameters for the upload.
*
* @return A new <code>Upload</code> object to use to check
* the state of the upload, listen for progress notifications,
* and otherwise manage the upload.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Upload upload(final PutObjectRequest putObjectRequest)
throws AmazonServiceException, AmazonClientException {
return doUpload(putObjectRequest, null, null, null);
}
/**
* <p>
* Schedules a new transfer to upload data to Amazon S3. This method is
* non-blocking and returns immediately (i.e. before the upload has
* finished).
* </p>
* <p>
* Use the returned <code>Upload</code> object to query the progress of the
* transfer, add listeners for progress events, and wait for the upload to
* complete.
* </p>
* <p>
* If resources are available, the upload will begin immediately. Otherwise,
* the upload is scheduled and started as soon as resources become
* available.
* </p>
* <p>
* If you are uploading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param putObjectRequest
* The request containing all the parameters for the upload.
* @param progressListener
* An optional callback listener to receive the progress of the
* upload.
*
* @return A new <code>Upload</code> object to use to check the state of the
* upload, listen for progress notifications, and otherwise manage
* the upload.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Upload upload(final PutObjectRequest putObjectRequest,
final S3ProgressListener progressListener)
throws AmazonServiceException, AmazonClientException {
return doUpload(putObjectRequest, null, progressListener, null);
}
/**
* <p>
* Schedules a new transfer to upload data to Amazon S3. This method is
* non-blocking and returns immediately (i.e. before the upload has
* finished).
* </p>
* <p>
* Use the returned <code>Upload</code> object to query the progress of the
* transfer, add listeners for progress events, and wait for the upload to
* complete.
* </p>
* <p>
* If resources are available, the upload will begin immediately. Otherwise,
* the upload is scheduled and started as soon as resources become
* available.
* </p>
* <p>
* If you are uploading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param putObjectRequest
* The request containing all the parameters for the upload.
* @param stateListener
* The transfer state change listener to monitor the upload.
* @param progressListener
* An optional callback listener to receive the progress of the
* upload.
*
* @return A new <code>Upload</code> object to use to check the state of the
* upload, listen for progress notifications, and otherwise manage
* the upload.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
private Upload doUpload(final PutObjectRequest putObjectRequest,
final TransferStateChangeListener stateListener,
final S3ProgressListener progressListener,
final PersistableUpload persistableUpload) throws AmazonServiceException,
AmazonClientException {
assertNotObjectLambdaArn(putObjectRequest.getBucketName(), "upload");
appendSingleObjectUserAgent(putObjectRequest);
String multipartUploadId = persistableUpload != null ? persistableUpload
.getMultipartUploadId() : null;
if (putObjectRequest.getMetadata() == null)
putObjectRequest.setMetadata(new ObjectMetadata());
ObjectMetadata metadata = putObjectRequest.getMetadata();
File file = TransferManagerUtils.getRequestFile(putObjectRequest);
if ( file != null ) {
// Always set the content length, even if it's already set
metadata.setContentLength(file.length());
// Only set the content type if it hasn't already been set
if ( metadata.getContentType() == null ) {
metadata.setContentType(Mimetypes.getInstance().getMimetype(file));
}
} else {
if (multipartUploadId != null) {
throw new IllegalArgumentException(
"Unable to resume the upload. No file specified.");
}
}
String description = "Uploading to " + putObjectRequest.getBucketName()
+ "/" + putObjectRequest.getKey();
TransferProgress transferProgress = new TransferProgress();
transferProgress.setTotalBytesToTransfer(TransferManagerUtils
.getContentLength(putObjectRequest));
S3ProgressListenerChain listenerChain = new S3ProgressListenerChain(
new TransferProgressUpdatingListener(transferProgress),
putObjectRequest.getGeneralProgressListener(), progressListener);
putObjectRequest.setGeneralProgressListener(listenerChain);
UploadImpl upload = new UploadImpl(description, transferProgress,
listenerChain, stateListener);
/**
* Since we use the same thread pool for uploading individual parts and
* complete multi part upload, there is a possibility that the tasks for
* complete multi-part upload will be added to end of queue in case of
* multiple parallel uploads submitted. This may result in a delay for
* processing the complete multi part upload request.
*/
UploadCallable uploadCallable = new UploadCallable(this, executorService,
upload, putObjectRequest, listenerChain, multipartUploadId,
transferProgress);
UploadMonitor watcher = UploadMonitor.create(this, upload, executorService,
uploadCallable, putObjectRequest, listenerChain);
upload.setMonitor(watcher);
return upload;
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param bucket
* The name of the bucket containing the object to download.
* @param key
* The key under which the object to download is stored.
* @param file
* The file to download the object's data to.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(String bucket, String key, File file) {
return download(bucket, key, file, 0);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param bucket
* The name of the bucket containing the object to download.
* @param key
* The key under which the object to download is stored.
* @param file
* The file to download the object's data to.
* @param timeoutMillis
* Timeout, in milliseconds, for waiting for this download to
* complete. Note that the timeout time will be approximate
* and is not strictly guaranteed. As a result this timeout
* should not be relied on in cases where exact precision is
* required.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(String bucket, String key,
File file, long timeoutMillis) {
return download(new GetObjectRequest(bucket, key), file,
timeoutMillis);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param getObjectRequest
* The request containing all the parameters for the download.
* @param file
* The file to download the object data to.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(final GetObjectRequest getObjectRequest, final File file) {
return download(getObjectRequest, file, 0);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param getObjectRequest
* The request containing all the parameters for the download.
* @param file
* The file to download the object data to.
* @param timeoutMillis
* Timeout, in milliseconds, for waiting for this download to
* complete. Note that the timeout time will be approximate
* and is not strictly guaranteed. As a result this timeout
* should not be relied on in cases where exact precision is
* required.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(final GetObjectRequest getObjectRequest,
final File file, long timeoutMillis) {
return doDownload(getObjectRequest, file, null, null, OVERWRITE_MODE,
timeoutMillis, null);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param getObjectRequest
* The request containing all the parameters for the download.
* @param file
* The file to download the object data to.
* @param progressListener
* An optional callback listener to get the progress of the
* download.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(final GetObjectRequest getObjectRequest,
final File file, final S3ProgressListener progressListener) {
return doDownload(getObjectRequest, file, null, progressListener,
OVERWRITE_MODE, 0, null);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param getObjectRequest
* The request containing all the parameters for the download.
* @param file
* The file to download the object data to.
* @param progressListener
* An optional callback listener to get the progress of the
* download.
* @param timeoutMillis
* Timeout, in milliseconds, for waiting for this download to
* complete. Note that the timeout time will be approximate
* and is not strictly guaranteed. As a result this timeout
* should not be relied on in cases where exact precision is
* required.
*
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/
public Download download(final GetObjectRequest getObjectRequest,
final File file, final S3ProgressListener progressListener,
final long timeoutMillis) {
return doDownload(getObjectRequest, file, null, progressListener,
OVERWRITE_MODE, timeoutMillis, null);
}
/**
* Schedules a new transfer to download data from Amazon S3 and save it to
* the specified file. This method is non-blocking and returns immediately
* (i.e. before the data has been fully downloaded).
* <p>
* Use the returned Download object to query the progress of the transfer,
* add listeners for progress events, and wait for the download to complete.
* </p>
* <p>
* If you are downloading <a href="http://aws.amazon.com/kms/">Amazon Web Services
* KMS</a>-encrypted objects, you need to specify the correct region of the
* bucket on your client and configure Amazon Web Services Signature Version 4 for added
* security. For more information on how to do this, see
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#
* specify-signature-version
* </p>
*
* @param getObjectRequest
* The request containing all the parameters for the download.
* @param file
* The file to download the object data to.
* @param progressListener
* An optional callback listener to get the progress of the
* download.
* @param timeoutMillis
* Timeout, in milliseconds, for waiting for this download to
* complete. Note that the timeout time will be approximate
* and is not strictly guaranteed. As a result this timeout
* should not be relied on in cases where exact precision is
* required.
* @param resumeOnRetry
* If set to true, upon an immediate retry of a failed object
* download, the <code>TransferManager</code> will resume the
* download from the current end of the file on disk.
* @return A new <code>Download</code> object to use to check the state of
* the download, listen for progress notifications, and otherwise
* manage the download.
*
* @throws AmazonClientException
* If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException
* If any errors occurred in Amazon S3 while processing the
* request.
*/