forked from TritonDataCenter/java-manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientEncryptionServerMultipart.java
84 lines (70 loc) · 3.68 KB
/
ClientEncryptionServerMultipart.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
/*
* Copyright (c) 2017, Joyent, Inc. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import com.joyent.manta.client.MantaClient;
import com.joyent.manta.client.MantaMetadata;
import com.joyent.manta.client.multipart.*;
import com.joyent.manta.config.*;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Stream;
/*
* Usage: set the mantaUsername, privateKeyPath, publicKeyId, and multipartServer with your own values.
*/
public class ClientEncryptionServerMultipart {
private static String mantaUsername = "USERNAME";
public static void main(String... args) {
String privateKeyPath = "PATH/.ssh/id_rsa";
String publicKeyId = "04:92:7b:23:bc:08:4f:d7:3b:5a:38:9e:4a:17:2e:df";
String multipartServer = "https://MANTA_MULTIPART_SERVER";
ConfigContext config = new ChainedConfigContext(
new DefaultsConfigContext(),
new EnvVarConfigContext(),
new MapConfigContext(System.getProperties()))
.setMantaURL(multipartServer)
.setMantaUser(mantaUsername)
.setMantaKeyPath(privateKeyPath)
.setMantaKeyId(publicKeyId)
.setClientEncryptionEnabled(true)
.setEncryptionAlgorithm("AES256/CTR/NoPadding")
.setEncryptionAuthenticationMode(EncryptionAuthenticationMode.Optional)
.setPermitUnencryptedDownloads(false)
.setEncryptionKeyId("simple/example")
.setEncryptionPrivateKeyBytes(Base64.getDecoder().decode("RkZGRkZGRkJEOTY3ODNDNkM5MUUyMjIyMTExMTIyMjI="));
try (MantaClient client = new MantaClient(config)) {
EncryptedServerSideMultipartManager multipart = new EncryptedServerSideMultipartManager(client);
multipartUpload(multipart);
}
}
private static void multipartUpload(EncryptedServerSideMultipartManager multipart) {
String uploadObject = "/" + mantaUsername + "/stor/multipart";
// We catch network errors and handle them here
try {
MantaMetadata metadata = new MantaMetadata();
metadata.put("e-secretkey", "My Secret Value");
EncryptedMultipartUpload<ServerSideMultipartUpload> upload =
multipart.initiateUpload(uploadObject, metadata);
MantaMultipartUploadPart part1 = multipart.uploadPart(upload, 1, RandomUtils.nextBytes(5242880));
MantaMultipartUploadPart part2 = multipart.uploadPart(upload, 2, RandomUtils.nextBytes(1000000));
// Complete the process by instructing Manta to assemble the final object from its parts
MantaMultipartUploadTuple[] parts = new MantaMultipartUploadTuple[] { part1, part2 };
Stream<MantaMultipartUploadTuple> partsStream = Arrays.stream(parts);
multipart.complete(upload, partsStream);
System.out.println(uploadObject + " is now assembled!");
} catch (IOException e) {
// This catch block is for general network failures
// For example, ServerSideMultipartUpload.initiateUpload can throw an IOException
ContextedRuntimeException exception = new ContextedRuntimeException(
"A network error occurred when doing a multipart upload to Manta.");
exception.setContextValue("path", uploadObject);
throw exception;
}
}
}