@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()); }
@Override public PutObjectResult putObject(PutObjectRequest req) throws AmazonClientException, AmazonServiceException { if (!multipartUpload) return super.putObject(req); final long contentLen = TransferManagerUtils.getContentLength(req); String tempFilename = req.getKey() + ".tmp"; String origFilename = req.getKey(); req.setKey(tempFilename); XProgressListener progressListener = new XProgressListener(); req.setProgressListener(new ProgressListenerChain(progressListener)); progressListener.setContentLen(contentLen); progressListener.setUpload(transferManager.upload(req)); try { progressListener.getUpload().waitForCompletion(); } catch (InterruptedException e) { throw new AmazonClientException(e.getMessage(), e); } CopyObjectRequest copyReq = new CopyObjectRequest(req.getBucketName(), tempFilename, req.getBucketName(), origFilename); copyObject(copyReq); deleteObject(new DeleteObjectRequest(req.getBucketName(), tempFilename)); return null; }
@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; }
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; }