Example usage for com.amazonaws.event ProgressEvent getBytesTransferred

List of usage examples for com.amazonaws.event ProgressEvent getBytesTransferred

Introduction

In this page you can find the example usage for com.amazonaws.event ProgressEvent getBytesTransferred.

Prototype

public long getBytesTransferred() 

Source Link

Document

Convenient method to returns the number of bytes transferred in this event, or the number of bytes reset (or discarded) if negative.

Usage

From source file:aws.example.s3.XferMgrProgress.java

License:Open Source License

public static void uploadFileWithListener(String file_path, String bucket_name, String key_prefix,
        boolean pause) {
    System.out.println("file: " + file_path + (pause ? " (pause)" : ""));

    String key_name = null;/*w  w w. j av  a 2  s. c  o m*/
    if (key_prefix != null) {
        key_name = key_prefix + '/' + file_path;
    } else {
        key_name = file_path;
    }

    File f = new File(file_path);
    TransferManager xfer_mgr = new TransferManager();
    try {
        Upload u = xfer_mgr.upload(bucket_name, key_name, f);
        // print an empty progress bar...
        printProgressBar(0.0);
        u.addProgressListener(new ProgressListener() {
            public void progressChanged(ProgressEvent e) {
                double pct = e.getBytesTransferred() * 100.0 / e.getBytes();
                eraseProgressBar();
                printProgressBar(pct);
            }
        });
        // block with Transfer.waitForCompletion()
        XferMgrProgress.waitForCompletion(u);
        // print the final state of the transfer.
        TransferState xfer_state = u.getState();
        System.out.println(": " + xfer_state);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    xfer_mgr.shutdownNow();
}

From source file:com.streamsets.datacollector.lib.emr.S3Manager.java

License:Apache License

String uploadToS3(String name, File file) throws IOException {
    long start = System.currentTimeMillis();
    long fileLength = file.length() / (1000 * 1000);
    String bucket = getBucket(pipelineEmrConfigs.getS3StagingUri());
    String path = getPath(pipelineEmrConfigs.getS3StagingUri()) + "/" + pipelineId + "/" + uniquePrefix;
    String s3Uri = "s3://" + bucket + "/" + path + "/" + name;
    try {//from   w  w  w  .  j av  a  2  s  .  c om

        // Upload
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, path + "/" + name, file);
        putObjectRequest.setGeneralProgressListener(new ProgressListener() {
            long counter;
            long tick = -1;

            @Override
            public void progressChanged(ProgressEvent progressEvent) {
                counter += progressEvent.getBytesTransferred();
                if (counter / (100 * 1000000) > tick) {
                    tick++;
                    LOG.debug("Uploading '{}' {}/{} MB, {} secs", s3Uri, counter / (1000 * 1000), fileLength,
                            (System.currentTimeMillis() - start) / 1000);
                }
            }
        });

        getS3TransferManager().upload(putObjectRequest).waitForCompletion();

        LOG.info("Uploaded file at: {}", s3Uri);
        return s3Uri;
    } catch (SdkBaseException | InterruptedException ex) {
        throw new IOException(ex);
    }
}

From source file:com.zahdoo.android.extension.GCM.DownloadModel.java

License:Open Source License

public DownloadModel(Context context, String key, String downloadType) {
    super(context, Uri.parse(key));

    this.context = context;
    strFileName = key;//key is the name of the file;
    fileDownloadType = downloadType;//from   ww w .  j  ava  2 s .c o  m

    mStatus = Status.IN_PROGRESS;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            Log.d("CADIE S3",
                    "Download Progress - " + (int) Global.mDownload.getProgress().getPercentTransferred());

            try {

                //                    if(!fileDownloadType.contentEquals("THUMB_DOWNLOAD"))
                //                    {
                if ((int) Global.mDownload.getProgress().getPercentTransferred() != 0
                        && progressVal != (int) Global.mDownload.getProgress().getPercentTransferred()) {
                    progressVal = (int) Global.mDownload.getProgress().getPercentTransferred();
                    Log.d("CADIE S3", "Download Progress Event Dispatch - " + progressVal);
                    try {
                        FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                "TRANSFER_PROGRESS^" + progressVal);
                    } catch (Exception e) {
                        Log.d("CADIE S3", "Download err - " + e.toString());
                    }
                }
                //}

                if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                    Global.mDownload.removeProgressListener(mListener);
                    mStatus = Status.COMPLETED;

                    if (fileDownloadType.contentEquals("THUMB_DOWNLOAD")) {
                        Log.d("CADIE S3", "Thumb Downloaded");
                        try {
                            FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                    "THUMB_DOWNLOAD");
                        } catch (Exception e) {
                            Log.d("CADIE S3", "Thumb Download err - " + e.toString());
                        }
                    } else {
                        Log.d("CADIE S3", "File Downloaded");
                        try {
                            FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                    "DOWNLOAD_SUCCESSFUL");
                        } catch (Exception e) {
                            Log.d("CADIE S3", "File Download err - " + e.toString());
                        }
                    }
                    ((FileTransferService) DownloadModel.this.context).stopSelf();
                } else if (event.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
                    Global.mDownload.removeProgressListener(mListener);

                    try {
                        AmazonClientException e = Global.mDownload.waitForException();
                        Log.e("CADIE ",
                                "CADIE S3 Exception - " + e.toString() + " " + event.getBytesTransferred());

                        try {
                            FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED", "ERROR ");
                        } catch (Exception e1) {
                            Log.d("CADIE S3", "CADIE S3 Exception 100 - " + e1.toString());
                        }

                        Global.mTransferManager.shutdownNow();
                        Global.mTransferManager = null;
                        ((FileTransferService) DownloadModel.this.context).stopSelf();
                    } catch (InterruptedException e) {
                    }
                }

            } catch (Exception e) {
                Log.d("CADIE S3", "S3 Download Exc - " + e.toString());
            }
        }
    };
}

From source file:com.zahdoo.android.extension.GCM.UploadModel.java

License:Open Source License

public UploadModel(Context context, Uri uri, String fileName, final String uploadType,
        final String serverResponse) {
    super(context, uri);

    this.context = context;

    strFileName = fileName;/*from   w w  w  . j  a  v  a 2s  .c  om*/

    isTempFile = false;
    fileUploadType = uploadType;

    try {
        if (uploadType.contentEquals("THUMB")) {
            Log.d("CADIE GCM", "UPLOADING THUMB");
            filePath = "/data/data/air.com.zahdoo.cadie/com.zahdoo.cadie/Local Store/thumbnails/" + fileName;
        } else {
            filePath = Environment.getExternalStorageDirectory() + "/cadie/" + fileName;

            if (!(new File(filePath).exists())) {
                filePath = "/data/data/air.com.zahdoo.cadie/com.zahdoo.cadie/Local Store/cadie/" + fileName;
            }
        }

        mFile = new File(filePath);

        if (mFile.exists()) {
            Log.d("CADIE S3", "File Exists");
        } else {
            isTempFile = true;
            Log.d("CADIE S3", "File does not exist");
        }

        int i = fileName.lastIndexOf('.');

        if (i >= 0) {
            mExtension = fileName.substring(i + 1);
        }

        Log.d("CADIE S3", "File Extension - " + mExtension);

        mListener = new ProgressListener() {
            @Override
            public void progressChanged(ProgressEvent event) {
                Log.d("CADIE S3",
                        "Upload Progress - " + (int) Global.mUpload.getProgress().getPercentTransferred());

                if (fileUploadType.contentEquals("ALL_FILES")) {
                    if ((int) Global.mUpload.getProgress().getPercentTransferred() != 0
                            && progressVal != (int) Global.mUpload.getProgress().getPercentTransferred()) {
                        progressVal = (int) Global.mUpload.getProgress().getPercentTransferred();
                        Log.d("CADIE S3", "Upload Progress Event Dispatch - " + progressVal);
                        try {
                            FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                    "TRANSFER_PROGRESS^" + progressVal);
                        } catch (Exception e) {
                            Log.d("CADIE S3", "Upload Progress Event Dispatch Error - " + e.toString());
                        }
                    }
                }

                if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                    Log.d("CADIE S3", "File Uploaded");

                    Global.mUpload.removeProgressListener(mListener);

                    if (mFile != null) {
                        if (isTempFile)
                            mFile.delete();

                        Log.d("CADIE S3", "File Deleted");

                        if (fileUploadType.contentEquals("ALL_FILES")) {
                            String[] vStrings = serverResponse.split("\\^");

                            try {
                                if (vStrings[0].contentEquals("FILE_UPLOADED")) {
                                    FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                            "FILE_UPLOADED^" + vStrings[1]);
                                } else {
                                    FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                            "ERROR ");
                                }
                            } catch (Exception e) {
                                Log.d("CADIE S3", "File Upload Error - " + e.toString());
                            }
                        } else//THUMB
                        {
                            Log.d(CommonUtilities.TAG, "THUMB UPLOADED");
                            try {
                                FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED",
                                        "THUMB_UPLOADED");
                            } catch (Exception e) {
                                Log.d(CommonUtilities.TAG, "THUMB UPLOADED Error - " + e.toString());
                            }
                        }

                        //Global.mTransferManager.shutdownNow(false);
                        //Global.mTransferManager = null;
                        ((FileTransferService) UploadModel.this.context).stopSelf();
                    }
                } else if (event.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
                    Global.mUpload.removeProgressListener(mListener);
                    //upload();

                    try {
                        AmazonClientException e = Global.mUpload.waitForException();
                        Log.e("CADIE ",
                                "CADIE S3 Exception - " + e.toString() + " " + event.getBytesTransferred());

                        try {
                            FileTransferFunction.gcmCon.dispatchStatusEventAsync("REGISTERED", "ERROR ");
                        } catch (Exception e1) {
                            Log.d(CommonUtilities.TAG, "CADIE S3 Exception - " + e1.toString());
                        }

                        Global.mTransferManager.shutdownNow(false);
                        Global.mTransferManager = null;
                        ((FileTransferService) UploadModel.this.context).stopSelf();
                    } catch (InterruptedException e) {
                    }
                }
            }
        };
    } catch (Exception e) {
        Log.d(CommonUtilities.TAG, "UPLOAD EXCEPTIOn -  " + e.toString());
    }
}

From source file:io.dockstore.common.FileProvisioning.java

License:Apache License

public void provisionOutputFile(FileInfo file, String cwlOutputPath) {
    File sourceFile = new File(cwlOutputPath);
    long inputSize = sourceFile.length();
    if (file.getUrl().startsWith("s3://")) {
        AmazonS3 s3Client = FileProvisioning.getAmazonS3Client(config);
        String trimmedPath = file.getUrl().replace("s3://", "");
        List<String> splitPathList = Lists.newArrayList(trimmedPath.split("/"));
        String bucketName = splitPathList.remove(0);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, Joiner.on("/").join(splitPathList),
                sourceFile);/*ww w  .  ja v  a2s  . co m*/
        putObjectRequest.setGeneralProgressListener(new ProgressListener() {
            ProgressPrinter printer = new ProgressPrinter();
            long runningTotal = 0;

            @Override
            public void progressChanged(ProgressEvent progressEvent) {
                if (progressEvent.getEventType() == ProgressEventType.REQUEST_BYTE_TRANSFER_EVENT) {
                    runningTotal += progressEvent.getBytesTransferred();
                }
                printer.handleProgress(runningTotal, inputSize);
            }
        });
        try {
            s3Client.putObject(putObjectRequest);
        } finally {
            System.out.println();
        }
    } else {
        try {
            FileSystemManager fsManager;
            // trigger a copy from the URL to a local file path that's a UUID to avoid collision
            fsManager = VFS.getManager();
            // check for a local file path
            FileObject dest = fsManager.resolveFile(file.getUrl());
            FileObject src = fsManager.resolveFile(sourceFile.getAbsolutePath());
            copyFromInputStreamToOutputStream(src.getContent().getInputStream(), inputSize,
                    dest.getContent().getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("Could not provision output files", e);
        }
    }
}

From source file:msv_upload_tool.FXMLDocumentController.java

private void uploadObject() {

    final Long max = file.length();

    task = new Task<Void>() {
        @Override/*from  w w  w  .ja  va2 s  . c o  m*/
        protected Void call() {

            boolean doLoop = true;
            long total = 0;

            while (doLoop) {

                lock.readLock().lock();

                try {
                    total = totalBytes;
                } finally {
                    lock.readLock().unlock();
                }

                updateProgress(total, max);
                if (total == max)
                    doLoop = false;

                try {
                    Thread.sleep(50); //1000 milliseconds is one second.
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }

            }

            updateProgress(-1, max);

            this.succeeded();
            return null;

        }
    };

    uploadProgress.progressProperty().bind(task.progressProperty());
    task.setOnSucceeded(new EventHandler() {

        @Override
        public void handle(Event event) {

            label.setText("");

            label2.setText("");
            button.setDisable(true);
            button2.setDisable(false);

        }

    });

    Thread th = new Thread(task);

    th.setDaemon(true);

    //disable the buttons
    button.setDisable(true);
    button2.setDisable(true);

    th.start();

    String existingBucketName = "mstargeneralfiles";
    String keyName = "duh/" + file.getName();
    String filePath = file.getAbsolutePath();

    TransferManager tm = new TransferManager(new ProfileCredentialsProvider());

    // For more advanced uploads, you can create a request object 
    // and supply additional request parameters (ex: progress listeners,
    // canned ACLs, etc.)
    PutObjectRequest request = new PutObjectRequest(existingBucketName, keyName, new File(filePath));

    // You can ask the upload for its progress, or you can 
    // add a ProgressListener to your request to receive notifications 
    // when bytes are transferred.
    request.setGeneralProgressListener(new ProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent) {

            System.out.println(progressEvent.toString());

            lock.writeLock().lock();

            try {
                totalBytes += progressEvent.getBytesTransferred();
            } finally {
                lock.writeLock().unlock();
            }

        }
    });

    // TransferManager processes all transfers asynchronously, 
    // so this call will return immediately.
    Upload upload = tm.upload(request);

}