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 package org.eclipse.jgit.pgm;
46
47 import static java.lang.Integer.valueOf;
48
49 import java.io.EOFException;
50 import java.io.File;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.io.OutputStream;
56 import java.net.URLConnection;
57 import java.text.MessageFormat;
58 import java.util.Properties;
59
60 import org.eclipse.jgit.pgm.internal.CLIText;
61 import org.eclipse.jgit.transport.AmazonS3;
62 import org.kohsuke.args4j.Argument;
63
64 @Command(name = "amazon-s3-client", common = false, usage = "usage_CommandLineClientForamazonsS3Service")
65 class AmazonS3Client extends TextBuiltin {
66 @Argument(index = 0, metaVar = "metaVar_connProp", required = true)
67 private File propertyFile;
68
69 @Argument(index = 1, metaVar = "metaVar_op", required = true)
70 private String op;
71
72 @Argument(index = 2, metaVar = "metaVar_bucket", required = true)
73 private String bucket;
74
75 @Argument(index = 3, metaVar = "metaVar_KEY", required = true)
76 private String key;
77
78
79 @Override
80 protected final boolean requiresRepository() {
81 return false;
82 }
83
84
85 @Override
86 protected void run() throws Exception {
87 final AmazonS3 s3 = new AmazonS3(properties());
88
89 if ("get".equals(op)) {
90 final URLConnection c = s3.get(bucket, key);
91 int len = c.getContentLength();
92 try (InputStream in = c.getInputStream()) {
93 outw.flush();
94 final byte[] tmp = new byte[2048];
95 while (len > 0) {
96 final int n = in.read(tmp);
97 if (n < 0)
98 throw new EOFException(MessageFormat.format(
99 CLIText.get().expectedNumberOfbytes,
100 valueOf(len)));
101 outs.write(tmp, 0, n);
102 len -= n;
103 }
104 outs.flush();
105 }
106
107 } else if ("ls".equals(op) || "list".equals(op)) {
108 for (String k : s3.list(bucket, key))
109 outw.println(k);
110
111 } else if ("rm".equals(op) || "delete".equals(op)) {
112 s3.delete(bucket, key);
113
114 } else if ("put".equals(op)) {
115 try (OutputStream os = s3.beginPut(bucket, key, null, null)) {
116 final byte[] tmp = new byte[2048];
117 int n;
118 while ((n = ins.read(tmp)) > 0)
119 os.write(tmp, 0, n);
120 }
121 } else {
122 throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
123 }
124 }
125
126 private Properties properties() {
127 try {
128 try (InputStream in = new FileInputStream(propertyFile)) {
129 final Properties p = new Properties();
130 p.load(in);
131 return p;
132 }
133 } catch (FileNotFoundException e) {
134 throw die(MessageFormat.format(CLIText.get().noSuchFile, propertyFile), e);
135 } catch (IOException e) {
136 throw die(MessageFormat.format(CLIText.get().cannotReadBecause, propertyFile, e.getMessage()), e);
137 }
138 }
139 }