1
1
package com .aol .micro .server .s3 ;
2
2
3
+ import java .io .ByteArrayInputStream ;
4
+ import java .io .File ;
5
+ import java .io .IOException ;
6
+ import java .io .InputStream ;
7
+ import java .nio .file .FileSystems ;
8
+ import java .nio .file .Files ;
3
9
import java .util .ArrayList ;
4
10
import java .util .List ;
5
11
import java .util .function .Function ;
12
+ import java .util .function .Supplier ;
6
13
import java .util .stream .Stream ;
14
+ import java .util .stream .StreamSupport ;
7
15
16
+ import org .apache .commons .io .FileUtils ;
17
+ import org .jooq .lambda .tuple .Tuple ;
8
18
import org .springframework .beans .factory .annotation .Autowired ;
19
+ import org .springframework .beans .factory .annotation .Value ;
9
20
import org .springframework .stereotype .Component ;
10
21
22
+ import com .amazonaws .AmazonClientException ;
23
+ import com .amazonaws .AmazonServiceException ;
11
24
import com .amazonaws .services .s3 .AmazonS3Client ;
12
25
import com .amazonaws .services .s3 .model .DeleteObjectsRequest ;
13
26
import com .amazonaws .services .s3 .model .DeleteObjectsRequest .KeyVersion ;
14
27
import com .amazonaws .services .s3 .model .ListObjectsRequest ;
15
28
import com .amazonaws .services .s3 .model .ObjectListing ;
16
29
import com .amazonaws .services .s3 .model .S3ObjectSummary ;
30
+ import com .amazonaws .services .s3 .transfer .Download ;
31
+ import com .amazonaws .services .s3 .transfer .TransferManager ;
17
32
import com .aol .cyclops .control .ReactiveSeq ;
33
+ import com .aol .cyclops .util .ExceptionSoftener ;
18
34
19
35
@ Component
20
36
public class S3Utils {
21
37
22
38
private final AmazonS3Client client ;
39
+ private final TransferManager transferManager ;
40
+ private final String tmpDirectory ;
23
41
24
42
@ Autowired
25
- public S3Utils (AmazonS3Client client ) {
43
+ public S3Utils (AmazonS3Client client , TransferManager transferManager ,
44
+ @ Value ("${s3.tmp.dir:#{null}}" ) String tmpDirectory ) {
26
45
this .client = client ;
46
+ this .transferManager = transferManager ;
47
+ this .tmpDirectory = tmpDirectory ;
27
48
}
28
49
29
50
public List <S3ObjectSummary > getAllSummaries (ListObjectsRequest req ) {
@@ -36,14 +57,13 @@ public List<S3ObjectSummary> getAllSummaries(ListObjectsRequest req) {
36
57
marker = listing .getNextMarker ();
37
58
result .addAll (listing .getObjectSummaries ());
38
59
} while (listing .isTruncated ());
60
+
39
61
return result ;
40
62
}
41
63
42
- /*
43
- * TODO implement smarter mechanism to reduce number of queries
44
- */
45
64
public <T > Stream <T > getSummariesStream (ListObjectsRequest req , Function <S3ObjectSummary , T > processor ) {
46
- return getAllSummaries (req ).stream ().map (processor );
65
+ Iterable <S3ObjectSummary > iterable = () -> new S3ObjectSummaryIterator (client , req );
66
+ return StreamSupport .stream (iterable .spliterator (), false ).map (processor );
47
67
}
48
68
49
69
public void delete (String bucketName , List <KeyVersion > objects ) {
@@ -54,4 +74,23 @@ public void delete(String bucketName, List<KeyVersion> objects) {
54
74
});
55
75
}
56
76
77
+ public InputStream getInputStream (String bucketName , String key , Supplier <File > tempFileSupplier )
78
+ throws AmazonServiceException , AmazonClientException , InterruptedException , IOException {
79
+ File file = tempFileSupplier .get ();
80
+ try {
81
+ Download download = transferManager .download (bucketName , key , file );
82
+ download .waitForCompletion ();
83
+ return new ByteArrayInputStream (FileUtils .readFileToByteArray (file ));
84
+ } finally {
85
+ file .delete ();
86
+ }
87
+ }
88
+
89
+ public InputStream getInputStream (String bucketName , String key )
90
+ throws AmazonServiceException , AmazonClientException , InterruptedException , IOException {
91
+ Supplier <File > tempFileSupplier = ExceptionSoftener .softenSupplier (() -> Files
92
+ .createTempFile (FileSystems .getDefault ().getPath (tmpDirectory ), "micro-s3" , "file" ).toFile ());
93
+ return getInputStream (bucketName , key , tempFileSupplier );
94
+ }
95
+
57
96
}
0 commit comments