1+ package com .spectralogic .ds3client .samples ;
2+
3+ import com .spectralogic .ds3client .Ds3Client ;
4+ import com .spectralogic .ds3client .Ds3ClientBuilder ;
5+ import com .spectralogic .ds3client .commands .*;
6+ import com .spectralogic .ds3client .models .Contents ;
7+ import com .spectralogic .ds3client .models .Credentials ;
8+ import com .spectralogic .ds3client .models .bulk .BulkObject ;
9+ import com .spectralogic .ds3client .models .bulk .Ds3Object ;
10+ import com .spectralogic .ds3client .models .bulk .MasterObjectList ;
11+ import com .spectralogic .ds3client .models .bulk .Objects ;
12+ import com .spectralogic .ds3client .serializer .XmlProcessingException ;
13+
14+ import org .apache .commons .io .IOUtils ;
15+
16+ import java .io .IOException ;
17+ import java .io .InputStream ;
18+ import java .io .OutputStream ;
19+ import java .nio .file .FileSystems ;
20+ import java .nio .file .Files ;
21+ import java .nio .file .Path ;
22+ import java .security .SignatureException ;
23+ import java .util .ArrayList ;
24+ import java .util .List ;
25+
26+ public class Ds3BulkGetExample {
27+
28+ public static void main (final String args []) throws IOException , SignatureException , XmlProcessingException {
29+
30+ // Get a client builder and then build a client instance. This is the main entry point to the SDK.
31+ final Ds3Client client = Ds3ClientBuilder .create ("ds3Endpoint:8080" ,
32+ new Credentials ("accessKey" , "secretKey" )).withHttpSecure (false ).build ();
33+
34+ final String bucket = "bucketName" ; //The bucket we are interested in getting objects from.
35+
36+ // Get the list of objects from the bucket that you want to perform the bulk get with.
37+ final GetBucketResponse response = client .getBucket (new GetBucketRequest (bucket ));
38+
39+ // We now need to generate the list of Ds3Objects that we want to get from DS3.
40+ final List <Ds3Object > objectList = new ArrayList <>();
41+ for (final Contents contents : response .getResult ().getContentsList ()){
42+ objectList .add (new Ds3Object (contents .getKey (), contents .getSize ()));
43+ }
44+
45+ // We are writing all the objects out to the directory output
46+ final Path dirPath = FileSystems .getDefault ().getPath ("output" );
47+
48+ // Check to make sure output exists, if not create the directory
49+ if (!Files .exists (dirPath )) {
50+ Files .createDirectory (dirPath );
51+ }
52+
53+ // Prime DS3 with the BulkGet command so that it can start to get objects off of tape.
54+ // All Response objects to the SDK implement the Closeable interface and can be used in try-with-resource blocks
55+ try (final BulkGetResponse bulkResponse = client .bulkGet (new BulkGetRequest (bucket , objectList ))) {
56+
57+ // The bulk response returns a list of lists which is designed to optimize data transmission from DS3.
58+ final MasterObjectList list = bulkResponse .getResult ();
59+ for (final Objects objects : list .getObjects ()) {
60+ for (final BulkObject obj : objects ) {
61+
62+ // Perform the operation to get the object from DS3.
63+ try (final GetObjectResponse getObjectResponse = client .getObject (
64+ new GetObjectRequest (bucket , obj .getName (), obj .getOffset (), list .getJobId ()))) {
65+
66+ final Path filePath = dirPath .resolve (obj .getName ());
67+ // Here we are using automatic resource cleanup to make sure the streams we use are cleaned up after use.
68+ try (final InputStream objStream = getObjectResponse .getContent ();
69+ final OutputStream fileOut = Files .newOutputStream (filePath )) {
70+ IOUtils .copy (objStream , fileOut ); //Using IOUtils to copy the object contents to a file.
71+ }
72+ }
73+ }
74+ }
75+ }
76+ }
77+ }
0 commit comments