View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.pgm;
13  
14  import static java.lang.Integer.valueOf;
15  
16  import java.io.EOFException;
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.net.URLConnection;
24  import java.text.MessageFormat;
25  import java.util.Properties;
26  
27  import org.eclipse.jgit.pgm.internal.CLIText;
28  import org.eclipse.jgit.transport.AmazonS3;
29  import org.kohsuke.args4j.Argument;
30  
31  @Command(name = "amazon-s3-client", common = false, usage = "usage_CommandLineClientForamazonsS3Service")
32  class AmazonS3Client extends TextBuiltin {
33  	@Argument(index = 0, metaVar = "metaVar_connProp", required = true)
34  	private File propertyFile;
35  
36  	@Argument(index = 1, metaVar = "metaVar_op", required = true)
37  	private String op;
38  
39  	@Argument(index = 2, metaVar = "metaVar_bucket", required = true)
40  	private String bucket;
41  
42  	@Argument(index = 3, metaVar = "metaVar_KEY", required = true)
43  	private String key;
44  
45  	/** {@inheritDoc} */
46  	@Override
47  	protected final boolean requiresRepository() {
48  		return false;
49  	}
50  
51  	/** {@inheritDoc} */
52  	@Override
53  	protected void run() throws Exception {
54  		final AmazonS3 s3 = new AmazonS3(properties());
55  
56  		if (op == null) {
57  			throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
58  		}
59  		switch (op) {
60  		case "get": //$NON-NLS-1$
61  			final URLConnection c = s3.get(bucket, key);
62  			int len = c.getContentLength();
63  			try (InputStream in = c.getInputStream()) {
64  				outw.flush();
65  				final byte[] tmp = new byte[2048];
66  				while (len > 0) {
67  					final int n = in.read(tmp);
68  					if (n < 0)
69  						throw new EOFException(MessageFormat.format(
70  								CLIText.get().expectedNumberOfbytes,
71  								valueOf(len)));
72  					outs.write(tmp, 0, n);
73  					len -= n;
74  				}
75  				outs.flush();
76  			}
77  			break;
78  		case "ls": //$NON-NLS-1$
79  		case "list": //$NON-NLS-1$
80  			for (String k : s3.list(bucket, key))
81  				outw.println(k);
82  			break;
83  		case "rm": //$NON-NLS-1$
84  		case "delete": //$NON-NLS-1$
85  			s3.delete(bucket, key);
86  			break;
87  		case "put": //$NON-NLS-1$
88  			try (OutputStream os = s3.beginPut(bucket, key, null, null)) {
89  				final byte[] tmp = new byte[2048];
90  				int n;
91  				while ((n = ins.read(tmp)) > 0)
92  					os.write(tmp, 0, n);
93  			}
94  			break;
95  		default:
96  			throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
97  		}
98  	}
99  
100 	private Properties properties() {
101 		try {
102 			try (InputStream in = new FileInputStream(propertyFile)) {
103 				final Properties p = new Properties();
104 				p.load(in);
105 				return p;
106 			}
107 		} catch (FileNotFoundException e) {
108 			throw die(MessageFormat.format(CLIText.get().noSuchFile, propertyFile), e);
109 		} catch (IOException e) {
110 			throw die(MessageFormat.format(CLIText.get().cannotReadBecause, propertyFile, e.getMessage()), e);
111 		}
112 	}
113 }