예제 #1
0
  @Override
  public F.Promise<Void> store(Path path, String key) {
    Promise<Void> promise = Futures.promise();

    TransferManager transferManager = new TransferManager(credentials);
    Upload upload = transferManager.upload(bucketName, key, path.toFile());

    upload.addProgressListener(
        (ProgressListener)
            progressEvent -> {
              if (progressEvent.getEventType().isTransferEvent()) {
                if (progressEvent
                    .getEventType()
                    .equals(ProgressEventType.TRANSFER_COMPLETED_EVENT)) {
                  transferManager.shutdownNow();
                  promise.success(null);
                } else if (progressEvent
                    .getEventType()
                    .equals(ProgressEventType.TRANSFER_FAILED_EVENT)) {
                  transferManager.shutdownNow();
                  promise.failure(new Exception("Upload failed"));
                }
              }
            });

    return F.Promise.wrap(promise.future());
  }
예제 #2
0
    @Override
    public void progressChanged(ProgressEvent e) {
      if (null == upload) return;

      TransferProgress xProgress = upload.getProgress();

      System.out.print(
          "\r  "
              + String.format("%.2f", xProgress.getPercentTransfered())
              + "% "
              + asNumber(xProgress.getBytesTransfered())
              + "/"
              + asNumber(contentLen)
              + BLANK_LINE);

      switch (e.getEventCode()) {
        case ProgressEvent.COMPLETED_EVENT_CODE:
          {
            System.out.println("Done");
            break;
          }
        case ProgressEvent.FAILED_EVENT_CODE:
          {
            try {
              AmazonClientException exc = upload.waitForException();

              System.err.println("Unable to upload file: " + exc.getMessage());
            } catch (InterruptedException ignored) {
            }
            break;
          }
      }
    }
예제 #3
0
 @Override
 protected UploadResult doInBackground(File... files) {
   try {
     for (File file : files) {
       TransferManager transferManager = new TransferManager(amazonS3);
       Upload upload = transferManager.upload(BUCKET, file.getName(), file);
       return upload.waitForUploadResult();
     }
   } catch (InterruptedException e) {
     Log.wtf("UPLOAD", e);
   }
   return null;
 }
예제 #4
0
    private void uploadObject() throws IOException {
      try {
        log.debug(
            "Starting upload for host: %s, key: %s, file: %s, size: %s",
            host, key, tempFile, tempFile.length());
        Upload upload = transferManager.upload(host, key, tempFile);

        if (log.isDebugEnabled()) {
          upload.addProgressListener(createProgressListener(upload));
        }

        upload.waitForCompletion();
        log.debug("Completed upload for host: %s, key: %s", host, key);
      } catch (AmazonClientException e) {
        throw new IOException(e);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new InterruptedIOException();
      }
    }
  private static void moveToS3(String outputFilePath, String existingBucketName)
      throws InterruptedException {
    System.setProperty("aws.accessKeyId", AWS_ACCESS_KEY_ID);
    System.setProperty("aws.secretKey", AWS_ACCESS_SECRET_KEY);
    File outputFile = new File(outputFilePath);
    String keyName = outputFile.getName();

    TransferManager tm = new TransferManager(new SystemPropertiesCredentialsProvider());
    // TransferManager processes all transfers asynchronously,
    // so this call will return immediately.
    Upload upload = tm.upload(existingBucketName, keyName, new File(outputFilePath));

    try {
      // Or you can block and wait for the upload to finish
      upload.waitForCompletion();
      System.out.println("Upload complete.");
    } catch (AmazonClientException amazonClientException) {
      System.out.println("Unable to upload file, upload was aborted.");
      amazonClientException.printStackTrace();
    }
  }
 @Override
 public ListenableFuture<Void> upload(
     String bucketName, String name, InputStream input, ObjectMetadata meta) throws IOException {
   SettableFuture<Void> future = SettableFuture.create();
   LOG.info("Starting upload, bucket={}, key={}", bucketName, name);
   final Upload up = tx.upload(bucketName, name, input, meta);
   up.addProgressListener(
       (ProgressEvent progressEvent) -> {
         switch (progressEvent.getEventType()) {
           case CLIENT_REQUEST_FAILED_EVENT:
             future.setException(
                 new IOException(
                     "Failed to complete upload after "
                         + progressEvent.getBytesTransferred()
                         + " bytes"));
           case CLIENT_REQUEST_SUCCESS_EVENT:
             LOG.info("Upload completed, bucket={}, key={}", bucketName, name);
             future.set(null);
         }
       });
   return future;
 }