You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AmazonS3Client.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package org.eclipse.jgit.pgm;
  12. import static java.lang.Integer.valueOf;
  13. import java.io.EOFException;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.net.URLConnection;
  21. import java.text.MessageFormat;
  22. import java.util.Properties;
  23. import org.eclipse.jgit.pgm.internal.CLIText;
  24. import org.eclipse.jgit.transport.AmazonS3;
  25. import org.kohsuke.args4j.Argument;
  26. @Command(name = "amazon-s3-client", common = false, usage = "usage_CommandLineClientForamazonsS3Service")
  27. class AmazonS3Client extends TextBuiltin {
  28. @Argument(index = 0, metaVar = "metaVar_connProp", required = true)
  29. private File propertyFile;
  30. @Argument(index = 1, metaVar = "metaVar_op", required = true)
  31. private String op;
  32. @Argument(index = 2, metaVar = "metaVar_bucket", required = true)
  33. private String bucket;
  34. @Argument(index = 3, metaVar = "metaVar_KEY", required = true)
  35. private String key;
  36. /** {@inheritDoc} */
  37. @Override
  38. protected final boolean requiresRepository() {
  39. return false;
  40. }
  41. /** {@inheritDoc} */
  42. @Override
  43. protected void run() throws Exception {
  44. final AmazonS3 s3 = new AmazonS3(properties());
  45. if (op == null) {
  46. throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
  47. }
  48. switch (op) {
  49. case "get": //$NON-NLS-1$
  50. final URLConnection c = s3.get(bucket, key);
  51. int len = c.getContentLength();
  52. try (InputStream in = c.getInputStream()) {
  53. outw.flush();
  54. final byte[] tmp = new byte[2048];
  55. while (len > 0) {
  56. final int n = in.read(tmp);
  57. if (n < 0)
  58. throw new EOFException(MessageFormat.format(
  59. CLIText.get().expectedNumberOfbytes,
  60. valueOf(len)));
  61. outs.write(tmp, 0, n);
  62. len -= n;
  63. }
  64. outs.flush();
  65. }
  66. break;
  67. case "ls": //$NON-NLS-1$
  68. case "list": //$NON-NLS-1$
  69. for (String k : s3.list(bucket, key))
  70. outw.println(k);
  71. break;
  72. case "rm": //$NON-NLS-1$
  73. case "delete": //$NON-NLS-1$
  74. s3.delete(bucket, key);
  75. break;
  76. case "put": //$NON-NLS-1$
  77. try (OutputStream os = s3.beginPut(bucket, key, null, null)) {
  78. final byte[] tmp = new byte[2048];
  79. int n;
  80. while ((n = ins.read(tmp)) > 0)
  81. os.write(tmp, 0, n);
  82. }
  83. break;
  84. default:
  85. throw die(MessageFormat.format(CLIText.get().unsupportedOperation, op));
  86. }
  87. }
  88. private Properties properties() {
  89. try {
  90. try (InputStream in = new FileInputStream(propertyFile)) {
  91. final Properties p = new Properties();
  92. p.load(in);
  93. return p;
  94. }
  95. } catch (FileNotFoundException e) {
  96. throw die(MessageFormat.format(CLIText.get().noSuchFile, propertyFile), e);
  97. } catch (IOException e) {
  98. throw die(MessageFormat.format(CLIText.get().cannotReadBecause, propertyFile, e.getMessage()), e);
  99. }
  100. }
  101. }