-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDynamicAuthentication.java
More file actions
64 lines (51 loc) · 2.33 KB
/
DynamicAuthentication.java
File metadata and controls
64 lines (51 loc) · 2.33 KB
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
/*
* Copyright (c) 2016-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.config.AuthAwareConfigContext;
import com.joyent.manta.config.BaseChainedConfigContext;
import com.joyent.manta.config.ChainedConfigContext;
import com.joyent.manta.config.DefaultsConfigContext;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class DynamicAuthentication {
public static void main(String... args) throws IOException {
// start with an unauthenticated user
BaseChainedConfigContext config = new ChainedConfigContext(
new DefaultsConfigContext())
.setMantaURL("https://us-east.manta.joyent.com")
.setMantaUser("user/subuser")
.setNoAuth(true);
AuthAwareConfigContext authConfig = new AuthAwareConfigContext(config);
try (MantaClient client = new MantaClient(authConfig)) {
// read a public file
String publicFile = "/user/public/foo";
String privateFile = "/user/stor/bar";
// Print out every line from file streamed real-time from Manta
try (InputStream is = client.getAsInputStream(publicFile);
Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
// enable authentication
config.setNoAuth(false);
config.setMantaUser("user");
config.setMantaKeyPath("src/test/java/data/id_rsa");
config.setMantaKeyId("04:92:7b:23:bc:08:4f:d7:3b:5a:38:9e:4a:17:2e:df");
authConfig.reload();
// Load file into memory as a string directly from Manta
String publicData = client.getAsString(publicFile);
System.out.println(publicData);
// Load a file in the private home folder
String privateData = client.getAsString(privateFile);
System.out.println(privateData);
}
}
}