Skip to content

Commit c02c7c7

Browse files
committed
Merge pull request #25 from rpmoore/master
Adding in the code samples from the readme
2 parents 5dff199 + bb5d85a commit c02c7c7

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.spectralogic.ds3client.samples;
2+
3+
import com.spectralogic.ds3client.Ds3Client;
4+
import com.spectralogic.ds3client.Ds3ClientBuilder;
5+
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers;
6+
import com.spectralogic.ds3client.helpers.FileObjectPutter;
7+
import com.spectralogic.ds3client.models.Credentials;
8+
import com.spectralogic.ds3client.models.bulk.Ds3Object;
9+
import com.spectralogic.ds3client.serializer.XmlProcessingException;
10+
11+
import java.io.IOException;
12+
import java.nio.file.FileSystems;
13+
import java.nio.file.Path;
14+
import java.security.SignatureException;
15+
16+
public class BulkPutExample {
17+
18+
public static void main(final String args[]) throws IOException, SignatureException, XmlProcessingException {
19+
final Ds3Client client = Ds3ClientBuilder.create("endpoint:8080",
20+
new Credentials("accessId", "secretKey"))
21+
.withHttpSecure(false)
22+
.build();
23+
24+
// Wrap the Ds3Client with the helper functions
25+
final Ds3ClientHelpers helper = Ds3ClientHelpers.wrap(client);
26+
27+
// The bucket that we will be writing to
28+
final String bucketName = "my_bucket";
29+
30+
// Make sure that the bucket exists, if it does not this will create it
31+
helper.ensureBucketExists(bucketName);
32+
33+
// Our input path which contains all the files that we want to transfer
34+
final Path inputPath = FileSystems.getDefault().getPath("input");
35+
36+
// Get the list of files that are contained in the inputPath
37+
final Iterable<Ds3Object> objects = helper.listObjectsForDirectory(inputPath);
38+
39+
// Create the write job with the bucket we want to write to and the list
40+
// of objects that will be written
41+
final Ds3ClientHelpers.WriteJob job = helper.startWriteJob(bucketName, objects);
42+
43+
// Start the write job using an Object Putter that will read the files
44+
// from the local file system.
45+
job.write(new FileObjectPutter(inputPath));
46+
}
47+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.GetServiceRequest;
6+
import com.spectralogic.ds3client.commands.GetServiceResponse;
7+
import com.spectralogic.ds3client.models.Credentials;
8+
import com.spectralogic.ds3client.models.Bucket;
9+
10+
import java.io.IOException;
11+
import java.security.SignatureException;
12+
13+
public class Ds3ServiceListExample {
14+
15+
public static void main(final String args[]) throws IOException, SignatureException {
16+
17+
// Get a client builder and then build a client instance. This is the main entry point to the SDK.
18+
final Ds3Client client = Ds3ClientBuilder.create("ds3Endpoint:8080",
19+
new Credentials("accessKey", "secretKey")).withHttpSecure(false).build();
20+
21+
// Tell the client to get us a list of all buckets, this is called a service list.
22+
try (final GetServiceResponse response = client.getService(new GetServiceRequest())) {
23+
24+
// Iterate through all the buckets and print them to the console.
25+
for (final Bucket bucket : response.getResult().getBuckets()) {
26+
System.out.println(bucket.getName());
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)