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 @Override
79 protected final boolean requiresRepository() {
80 return false;
81 }
82
83 @Override
84 protected void run() throws Exception {
85 final AmazonS3 s3 = new AmazonS3(properties());
86
87 if ("get".equals(op)) {
88 final URLConnection c = s3.get(bucket, key);
89 int len = c.getContentLength();
90 final InputStream in = c.getInputStream();
91 try {
92 outw.flush();
93 final byte[] tmp = new byte[2048];
94 while (len > 0) {
95 final int n = in.read(tmp);
96 if (n < 0)
97 throw new EOFException(MessageFormat.format(
98 CLIText.get().expectedNumberOfbytes,
99 valueOf(len)));
100 outs.write(tmp, 0, n);
101 len -= n;
102 }
103 outs.flush();
104 } finally {
105 in.close();
106 }
107
108 } else if ("ls".equals(op) || "list".equals(op)) {
109 for (final String k : s3.list(bucket, key))
110 outw.println(k);
111
112 } else if ("rm".equals(op) || "delete".equals(op)) {
113 s3.delete(bucket, key);
114
115 } else if ("put".equals(op)) {
116 final OutputStream os = s3.beginPut(bucket, key, null, null);
117 final byte[] tmp = new byte[2048];
118 int n;
119 while ((n = ins.read(tmp)) > 0)
120 os.write(tmp, 0, n);
121 os.close();
122
123 } else {
124 throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
125 }
126 }
127
128 private Properties properties() {
129 try {
130 final InputStream in = new FileInputStream(propertyFile);
131 try {
132 final Properties p = new Properties();
133 p.load(in);
134 return p;
135 } finally {
136 in.close();
137 }
138 } catch (FileNotFoundException e) {
139 throw die(MessageFormat.format(CLIText.get().noSuchFile, propertyFile), e);
140 } catch (IOException e) {
141 throw die(MessageFormat.format(CLIText.get().cannotReadBecause, propertyFile, e.getMessage()), e);
142 }
143 }
144 }