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:doug.iotdemo.mojo.deployer.Deployer.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    AmazonS3 s3 = new AmazonS3Client();

    if (!s3.doesBucketExist(bucketName)) {
        throw new MojoExecutionException("bucket " + bucketName + " does not exist");
    }/*w w w. j a  va2s.c om*/

    getLog().info("Uploading " + source.getName() + " to s3://" + bucketName + "/" + bucketKey);

    TransferManager manager = new TransferManager(s3);
    Transfer transfer;
    if (source.isFile()) {
        transfer = manager.upload(bucketName, bucketKey, source);
    } else if (source.isDirectory()) {
        transfer = manager.uploadDirectory(bucketName, bucketKey, source, true);
    } else {
        throw new MojoExecutionException("Unknown file type " + source.getAbsolutePath());
    }

    try {
        transfer.waitForCompletion();
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Upload to S3 failed", e);
    }
}

From source file:hu.mta.sztaki.lpds.cloud.entice.imageoptimizer.iaashandler.amazontarget.Storage.java

License:Apache License

/**
 * @param file Local file to upload// ww  w. j  a va2 s .  co m
 * @param endpoint S3 endpoint URL
 * @param accessKey Access key
 * @param secretKey Secret key
 * @param bucket Bucket name 
 * @param path Key name (path + file name)
 * @throws Exception On any error
 */
public static void upload(File file, String endpoint, String accessKey, String secretKey, String bucket,
        String path) throws Exception {
    AmazonS3Client amazonS3Client = null;
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(MAX_CONNECTIONS);
        clientConfiguration.setMaxErrorRetry(PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY);
        clientConfiguration.setConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);
        amazonS3Client = new AmazonS3Client(awsCredentials, clientConfiguration);
        S3ClientOptions clientOptions = new S3ClientOptions().withPathStyleAccess(true);
        amazonS3Client.setS3ClientOptions(clientOptions);
        amazonS3Client.setEndpoint(endpoint);
        //         amazonS3Client.putObject(new PutObjectRequest(bucket, path, file)); // up to 5GB
        TransferManager tm = new TransferManager(amazonS3Client); // up to 5TB
        Upload upload = tm.upload(bucket, path, file);
        // while (!upload.isDone()) { upload.getProgress().getBytesTransferred(); Thread.sleep(1000); } // to get progress
        upload.waitForCompletion();
        tm.shutdownNow();
    } catch (AmazonServiceException x) {
        Shrinker.myLogger.info("upload error: " + x.getMessage());
        throw new Exception("upload exception", x);
    } catch (AmazonClientException x) {
        Shrinker.myLogger.info("upload error: " + x.getMessage());
        throw new Exception("upload exception", x);
    } finally {
        if (amazonS3Client != null) {
            try {
                amazonS3Client.shutdown();
            } catch (Exception e) {
            }
        }
    }
}

From source file:jetbrains.buildServer.runner.codedeploy.AWSClient.java

License:Apache License

@NotNull
private UploadResult doUploadWithTransferManager(@NotNull final File revision,
        @NotNull final String s3BucketName, @NotNull final String s3ObjectKey) throws Throwable {
    return S3Util.withTransferManager(myS3Client, new S3Util.WithTransferManager<Upload>() {
        @NotNull//from  w w  w . j a va  2s.  co m
        @Override
        public Collection<Upload> run(@NotNull TransferManager manager) throws Throwable {
            return Collections.singletonList(manager.upload(s3BucketName, s3ObjectKey, revision));
        }
    }).iterator().next().waitForUploadResult();
}

From source file:modules.storage.AmazonS3Storage.java

License:Open Source License

@Override
public F.Promise<Void> store(Path path, String key, String name) {
    Promise<Void> promise = Futures.promise();

    TransferManager transferManager = new TransferManager(credentials);
    try {/*from  w  ww .jav a2  s. co  m*/
        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();
                    logger.error(progressEvent.toString());
                    promise.failure(new Exception(progressEvent.toString()));
                }
            }
        });
    } catch (AmazonServiceException ase) {
        logAmazonServiceException(ase);
    } catch (AmazonClientException ace) {
        logAmazonClientException(ace);
    }

    return F.Promise.wrap(promise.future());
}

From source file:org.ow2.proactive.scheduler.examples.S3ConnectorUploader.java

License:Open Source License

/**
 * Upload a local file to S3. <br>
 * Requires a bucket name. <br>/*  w  ww. j  a va 2  s.  co m*/
 *
 * @param filePath
 * @param bucketName
 * @param keyPrefix
 * @param pause
 * @param s3Client
 */
private void uploadFile(String filePath, String bucketName, String keyPrefix, boolean pause,
        AmazonS3 s3Client) {
    getOut().println("file: " + filePath + (pause ? " (" + PAUSE + ")" : ""));

    File file = new File(filePath);

    String keyName = (keyPrefix != null) ? Paths.get(keyPrefix, file.getName()).toString() : file.getName();

    TransferManager transferManager = TransferManagerBuilder.standard().withS3Client(s3Client).build();
    try {
        Upload uploader = transferManager.upload(bucketName, keyName, file);
        // loop with Transfer.isDone()
        SchedulerExamplesUtils.showTransferProgress(uploader);
        //  or block with Transfer.waitForCompletion()
        SchedulerExamplesUtils.waitForCompletion(uploader);
    } catch (AmazonServiceException e) {
        getErr().println(e.getErrorMessage());
        System.exit(1);
    }
    transferManager.shutdownNow();
}

From source file:org.vocefiscal.amazonaws.AWSPictureUpload.java

License:Open Source License

public void upload() {
    if (sleep > 0) {
        try {/*w  w  w  .  j a va2 s.  c o m*/
            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);
    }
}