1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.s3;
17
18 import static com.amazonaws.services.s3.Headers.CONTENT_TYPE;
19 import static com.google.common.base.Stopwatch.createStarted;
20 import static com.google.common.collect.Lists.newArrayList;
21 import static com.google.common.collect.Maps.newHashMap;
22 import static com.google.common.collect.Ordering.natural;
23 import static java.lang.String.format;
24 import static java.util.Arrays.asList;
25 import static org.kuali.common.core.io.Files.deleteCreateFile;
26 import static org.kuali.common.core.io.Files.getCanonicalFile;
27 import static org.kuali.common.util.FormatUtils.getTime;
28 import static org.kuali.common.util.log.LoggerUtils.logTable;
29 import static org.kuali.common.util.log.Loggers.newLogger;
30
31 import java.io.File;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.kuali.common.aws.model.ImmutableAWSCredentials;
38 import org.kuali.common.aws.s3.model.ObjectMetadata;
39 import org.kuali.common.aws.s3.model.ObjectSummary;
40 import org.kuali.common.aws.s3.model.PutFileRequest;
41 import org.kuali.common.core.base.TimedInterval;
42 import org.kuali.common.util.encrypt.Encryption;
43 import org.kuali.common.util.encrypt.Encryptor;
44 import org.slf4j.Logger;
45
46 import com.amazonaws.auth.AWSCredentials;
47 import com.google.common.base.Stopwatch;
48
49 public class DefaultS3ServiceTest {
50
51 private static final Logger logger = newLogger();
52 private final DefaultS3Service service = DefaultS3Service.build(getCredentials());
53
54 @Test
55 public void testYo() {
56 try {
57 String bucket = "archive.kuali.org";
58 String key = "foo/bar2/";
59 String prefix = "foo";
60 List<ObjectSummary> summaries = service.getCompleteList(bucket, prefix);
61 for (ObjectSummary summary : summaries) {
62 logger.info(summary.getKey());
63 }
64 ObjectMetadata meta = service.getMetadata(bucket, key);
65 show(meta.getRawMetadata());
66 show(meta.getUserMetadata());
67 Map<String, String> user = newHashMap(meta.getUserMetadata());
68 Map<String, Object> aws = newHashMap();
69 aws.put(CONTENT_TYPE, "application/x-directory");
70 ObjectMetadata omd = ObjectMetadata.builder().withUserMetadata(user).withRawMetadata(aws).build();
71 File scratch = deleteCreateFile(getCanonicalFile("./target/s3/foo.txt"), "");
72 String fooDirKey = "foo/bar2/";
73 PutFileRequest request = PutFileRequest.builder().withBucket(bucket).withKey(fooDirKey).withFile(scratch).withMetadata(omd).build();
74 service.putFile(request);
75 } catch (Throwable e) {
76 e.printStackTrace();
77 }
78 }
79
80 @Test
81 public void testS3FuseDir() {
82 try {
83 String bucket = "archive.kuali.org";
84 String key = "foo/bar/";
85 String prefix = "foo/bar";
86 List<ObjectSummary> summaries = service.getCompleteList(bucket, prefix);
87 for (ObjectSummary summary : summaries) {
88 logger.info(summary.getKey());
89 }
90 ObjectMetadata meta = service.getMetadata(bucket, key);
91 show(meta.getRawMetadata());
92 show(meta.getUserMetadata());
93 Map<String, String> user = newHashMap(meta.getUserMetadata());
94 Map<String, Object> aws = newHashMap();
95 aws.put(CONTENT_TYPE, "application/x-directory");
96 ObjectMetadata omd = ObjectMetadata.builder().withUserMetadata(user).withRawMetadata(aws).build();
97 File scratch = deleteCreateFile(getCanonicalFile("./target/s3/foo.txt"), "");
98 String fooDirKey = "foo/bar2/";
99 PutFileRequest request = PutFileRequest.builder().withBucket(bucket).withKey(fooDirKey).withFile(scratch).withMetadata(omd).build();
100 service.putFile(request);
101 } catch (Throwable e) {
102 e.printStackTrace();
103 }
104 }
105
106 @Test
107 @Ignore
108 public void testS3FuseFile() {
109 try {
110 String bucket = "archive.kuali.org";
111 String key = "org/kuali/testci/home/";
112 ObjectMetadata meta = service.getMetadata(bucket, key);
113
114 show(meta.getUserMetadata());
115 System.out.format("s3://%s/%s", bucket, key);
116 String fooKey = "org/kuali/testci/home/tomcat7/.jenkins/jobs/kdo-show-environment/builds/2014-08-20_19-33-08/foo.txt";
117 File scratch = deleteCreateFile(getCanonicalFile("./target/s3/foo.txt"), "bar");
118 Map<String, String> map = newHashMap(meta.getUserMetadata());
119 map.put("mtime", scratch.lastModified() + "");
120 ObjectMetadata omd = ObjectMetadata.builder().withUserMetadata(map).build();
121 PutFileRequest request = PutFileRequest.builder().withBucket(bucket).withKey(fooKey).withFile(scratch).withMetadata(omd).build();
122 service.putFile(request);
123 } catch (Throwable e) {
124 e.printStackTrace();
125 }
126 }
127
128 private void show(Map<String, ?> map) {
129 List<String> columns = asList("key", "value");
130 List<Object[]> rows = newArrayList();
131 List<String> keys = natural().immutableSortedCopy(map.keySet());
132 for (String key : keys) {
133 Object[] row = { key, map.get(key) };
134 rows.add(row);
135 }
136 logTable(columns, rows);
137 }
138
139 @Test
140 @Ignore
141 public void testObjectListing() {
142 try {
143 String bucket = "archive.kuali.org";
144 String prefix = "org/kuali/testci/home/tomcat7/.jenkins/jobs";
145 Stopwatch sw = createStarted();
146 List<ObjectSummary> summaries = service.getCompleteList(bucket, prefix);
147 TimedInterval timing = TimedInterval.build(sw);
148 info("summaries -> %s", summaries.size());
149 info("elapsed -> %s", getTime(timing.getElapsed()));
150 } catch (Throwable e) {
151 e.printStackTrace();
152 }
153 }
154
155 @Test
156 @Ignore
157 public void test() {
158 try {
159 info("hello world");
160 service.writeStringToObject("site.origin.kuali.org", "test/foo.txt", "bar");
161 } catch (Throwable e) {
162 e.printStackTrace();
163 }
164 }
165
166 private static final void info(String msg, Object... args) {
167 logger.info(args == null ? msg : format(msg, args));
168 }
169
170 private static AWSCredentials getCredentials() {
171 Encryptor encryptor = Encryption.getDefaultEncryptor();
172 String accessKey = encryptor.decrypt("U2FsdGVkX18ZhU341cfBkTGC+rNi22YhGUDMtBlB/PNaV94dH4RpLwAPl8UCzDgL");
173 String secretKey = encryptor.decrypt("U2FsdGVkX1/K6kgTQIYZHUd0NQpbTIzel8Q0ypnNmIBKRzIP4zR86HQk6IEd0Xv0Y7XtIpgSkRWuw6qtVIgHiw==");
174 return new ImmutableAWSCredentials(accessKey, secretKey);
175 }
176 }