Example usage for com.amazonaws.services.s3.transfer TransferManager upload

List of usage examples for com.amazonaws.services.s3.transfer TransferManager upload

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.transfer TransferManager upload.

Prototype

public Upload upload(final String bucketName, final String key, final File file)
        throws AmazonServiceException, AmazonClientException 

Source Link

Document

Schedules a new transfer to upload data to Amazon S3.

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;/*from  w ww .j  av  a2  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:aws.example.s3.XferMgrUpload.java

License:Open Source License

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

    String key_name = null;//from  w w  w  .  j a  v a 2s.  co  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 xfer = xfer_mgr.upload(bucket_name, key_name, f);
        // loop with Transfer.isDone()
        XferMgrProgress.showTransferProgress(xfer);
        //  or block with Transfer.waitForCompletion()
        XferMgrProgress.waitForCompletion(xfer);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    xfer_mgr.shutdownNow();
}

From source file:biz.neustar.webmetrics.plugins.neustar_s3_maven_plugin.S3UploadMojo.java

License:Apache License

/** */
private boolean upload(AmazonS3 s3, String bucketName, File sourceFile, String targetPath)
        throws MojoExecutionException {

    TransferManager mgr = new TransferManager(s3);
    Transfer transfer = null;/*from   w  ww .  j  av  a 2  s .c om*/
    transfer = mgr.upload(bucketName, targetPath, sourceFile);

    try {
        transfer.waitForCompletion();
        getLog().info("Transferred " + transfer.getProgress().getBytesTransfered() + " bytes.");
    } catch (AmazonServiceException e) {
        e.printStackTrace();
        return false;
    } catch (AmazonClientException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:br.org.fiscal65.amazonaws.AWSPictureUpload.java

License:Open Source License

@SuppressWarnings("deprecation")
public void upload() {
    if (sleep > 0) {
        try {//  www.j a  va2 s. c om
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
        }
    }

    boolean hasInternet = CommunicationUtils.verifyConnectivity(getContext());

    if (hasInternet) {
        boolean isOnWiFi = CommunicationUtils.isWifi(getContext());

        if (isOnWiFi || (AWSPictureUpload.this.podeEnviarRedeDados != null
                && AWSPictureUpload.this.podeEnviarRedeDados.equals(1))) {
            pictureFile = new File(picturePath);

            pictureName = ImageHandler.nomeDaMidia(pictureFile) + ".jpg";

            if (pictureFile != null) {
                try {
                    TransferManager mTransferManager = getTransferManager();

                    mUpload = mTransferManager.upload(CommunicationConstants.PICTURE_BUCKET_NAME,
                            AWSUtil.getPrefix(getContext()) + AWSPictureUpload.this.slugFiscalizacao + "/zona-"
                                    + AWSPictureUpload.this.zonaFiscalizacao + "/" + pictureName,
                            pictureFile);
                    mUpload.addProgressListener(progressListener);
                } catch (Exception e) {
                    mStatus = Status.CANCELED;

                    if (mUpload != null) {
                        mUpload.abort();
                    }

                    uploadListener.finishedPictureUploadS3ComError(slugFiscalizacao, zonaFiscalizacao,
                            idFiscalizacao, posicaoFoto);
                }
            }
        } else {
            mStatus = Status.CANCELED;

            if (mUpload != null) {
                mUpload.abort();
            }

            uploadListener.finishedPictureUploadS3ComError(slugFiscalizacao, zonaFiscalizacao, idFiscalizacao,
                    posicaoFoto);
        }
    } else {
        mStatus = Status.CANCELED;

        if (mUpload != null) {
            mUpload.abort();
        }

        uploadListener.finishedPictureUploadS3ComError(slugFiscalizacao, zonaFiscalizacao, idFiscalizacao,
                posicaoFoto);
    }
}

From source file:com.emc.vipr.s3.sample._08_CreateLargeObject.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve object key/value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();
    System.out.println("Enter the file location (C:\\Users\\vandrk\\EMC\\NameSpaceList.zip) :");
    String filePath = new BufferedReader(new InputStreamReader(System.in)).readLine();

    TransferManager manager = new TransferManager(s3);
    manager.upload(ViPRS3Factory.S3_BUCKET, key, new File(filePath)).waitForUploadResult();

    // print bucket key/value and content for validation
    System.out.println(String.format("completed mulit-part upload for object [%s/%s] with file path: [%s]",
            ViPRS3Factory.S3_BUCKET, key, filePath));
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public boolean uploadDirectoryOrFileAndListenProgress(final String bucketName, final File source,
        final String virtualDirectoryKeyPrefix)
        throws AmazonClientException, AmazonServiceException, FileNotFoundException {
    LOGGER.info("uploadDirectoryOrFileAndWaitForCompletion invoked, bucketName: {} , Source: {}", bucketName,
            source.getAbsolutePath());/*w w w. j  av a  2  s.co  m*/
    Transfer transfer = null;
    final TransferManager transferMgr = new TransferManager(s3client);
    if (source.isFile()) {
        transfer = transferMgr.upload(bucketName, source.getPath(), source);
    } else if (source.isDirectory()) {
        //upload recursively
        transfer = transferMgr.uploadDirectory(bucketName, virtualDirectoryKeyPrefix, source, true);
    } else {
        throw new FileNotFoundException("File is neither a regular file nor a directory " + source);
    }

    // You can poll your transfer's status to check its progress
    if (transfer.isDone()) {
        LOGGER.info("Start: {}  , State: {} and Progress (%): {}", transfer.getDescription(),
                transfer.getState(), transfer.getProgress().getPercentTransferred());

    }

    // Add progressListener to listen asynchronous notifications about your transfer's progress
    // Uncomment below code snippet during development
    /*transfer.addProgressListener(new ProgressListener() {
       public void progressChanged(ProgressEvent event) {
    if (LOGGER.isDebugEnabled()) {
       LOGGER.debug("Transferred bytes: " + (long) event.getBytesTransferred());
    }
     }
    });*/

    try {
        //Block the current thread and wait for completion
        //If the transfer fails AmazonClientException will be thrown
        transfer.waitForCompletion();
    } catch (AmazonClientException | InterruptedException excp) {
        LOGGER.error("Exception occured while waiting for transfer: ", excp);
    }

    LOGGER.info("End: {} , State: {} , Progress (%): {}", transfer.getDescription(), transfer.getState(),
            transfer.getProgress().getPercentTransferred());
    return transfer.isDone();
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public Transfer uploadDirectoryOrFile(final String bucketName, final File source,
        final String virtualDirectoryKeyPrefix)
        throws AmazonClientException, AmazonServiceException, IOException {
    LOGGER.info("uploadDirectoryOrFile invoked, bucketName: {} , Source: {}", bucketName,
            source.getAbsolutePath());//from w w w.j  a  va 2s  . c  o  m
    Transfer transfer = null;
    final TransferManager trMgr = new TransferManager(s3client);
    if (source.isFile()) {
        transfer = trMgr.upload(bucketName, source.getPath(), source);
    } else if (source.isDirectory()) {
        //Upload recursively
        //virtualDirectoryKeyPrefix could be virtual directory name inside the bucket
        transfer = trMgr.uploadDirectory(bucketName, virtualDirectoryKeyPrefix, source, true);
    } else {
        throw new FileNotFoundException("Source is neither a regular file nor a directory " + source);
    }
    return transfer;
}

From source file:com.github.kaklakariada.aws.sam.task.S3UploadTask.java

License:Open Source License

private void transferFileToS3(final String key) {
    final long fileSizeMb = file.length() / (1024 * 1024);
    getLogger().lifecycle("Uploading {} MB from file {} to {}...", fileSizeMb, file, getS3Url());
    final TransferManager transferManager = createTransferManager();
    final Instant start = Instant.now();
    final Upload upload = transferManager.upload(config.getDeploymentBucket(), key, file);
    try {//w w  w.  j a va2  s  .c  o  m
        upload.waitForCompletion();
        final Duration uploadDuration = Duration.between(start, Instant.now());
        getLogger().lifecycle("Uploaded {} to {} in {}", file, getS3Url(), uploadDuration);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new AssertionError("Upload interrupted", e);
    }
}

From source file:com.projectlaver.service.ListingService.java

License:Open Source License

/**
 * Public methods//from   w w w  .  j  av a 2s  . co m
 */

@Transactional(readOnly = false)
public Listing create(Listing listing) throws Exception {

    // set the expiration for one year from now if unset
    if (listing.getExpires() == null) {
        listing.setExpires(this.addDays(new Date(), 365));
    }

    // merge the user (reattach to DB)
    User user = listing.getSeller();
    User mergedUser = this.em.merge(user);
    listing.setSeller(mergedUser);

    if (listing.getImageAsFile() != null) {
        // upload the image preview to the public S3 bucket
        AWSCredentials myCredentials = new BasicAWSCredentials(this.s3accessKey, this.s3secretKey);
        TransferManager tx = new TransferManager(myCredentials);
        Upload myUpload = tx.upload(this.s3publicBucketName, listing.getImageFilename(),
                listing.getImageAsFile());
        myUpload.waitForCompletion();
    }

    if (listing.getContentFiles() != null && listing.getContentFiles().size() > 0) {

        Set<ContentFile> contentFiles = listing.getContentFiles();

        AWSCredentials myCredentials = new BasicAWSCredentials(this.s3accessKey, this.s3secretKey);
        TransferManager tx = new TransferManager(myCredentials);

        for (ContentFile file : contentFiles) {
            // upload the digital content to the private S3 bucket
            Upload myUpload = tx.upload(this.s3privateBucketName, file.getContentFilename(),
                    file.getDigitalContentAsFile());
            myUpload.waitForCompletion();
        }
    }

    return this.listingRepository.save(listing);
}

From source file:com.projectlaver.util.VoucherService.java

License:Open Source License

/**
 * Uploads the pdf file to the private S3 bucket.
 * //from   www.  jav a2  s .co m
 * @param pdfFilename
 * @param pdf
 */
void savePdf(String pdfFilename, File pdf) {
    AWSCredentials myCredentials = new BasicAWSCredentials(this.s3accessKey, this.s3secretKey);
    TransferManager tx = new TransferManager(myCredentials);
    Upload myUpload = tx.upload(this.s3privateBucketName, pdfFilename, pdf);

    try {
        myUpload.waitForCompletion();
    } catch (Exception e) {
        this.logger.error(String.format(
                "Exception while trying to upload to S3 the PDF using the temp file with path: %s",
                pdf.getPath()), e);
        throw new RuntimeException("Unable to upload the PDF file to S3.", e);
    }
}