Example usage for com.amazonaws.services.s3.transfer.internal DownloadImpl DownloadImpl

List of usage examples for com.amazonaws.services.s3.transfer.internal DownloadImpl DownloadImpl

Introduction

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

Prototype

public DownloadImpl(String description, TransferProgress transferProgress,
            ProgressListenerChain progressListenerChain, S3Object s3Object, TransferStateChangeListener listener,
            GetObjectRequest getObjectRequest, File file, ObjectMetadata objectMetadata,
            boolean isDownloadParallel) 

Source Link

Usage

From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java

License:Apache License

@Override
public Download download(String bucket, String key, File file, TransferManager transferManager) {
    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucket);
    MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key);
    try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
        fileOutputStream.write(mockS3Object.getData());
    } catch (IOException e) {
        throw new RuntimeException("Error writing to file " + file, e);
    }/*from w w w  .j  a v a 2s  .  com*/

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(mockS3Object.getData().length);
    progress.updateProgress(mockS3Object.getData().length);

    DownloadImpl download = new DownloadImpl(null, progress, null, null, null,
            new GetObjectRequest(bucket, key), file, mockS3Object.getObjectMetadata(), false);
    download.setState(TransferState.Completed);

    return download;
}

From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  w  w .  j av a2s.com*/
 * <p/>
 * This implementation creates any directory that does not exist in the path to the destination directory.
 */
@Override
public MultipleFileDownload downloadDirectory(String bucketName, String keyPrefix, File destinationDirectory,
        TransferManager transferManager) {
    LOGGER.debug("downloadDirectory(): bucketName = " + bucketName + ", keyPrefix = " + keyPrefix
            + ", destinationDirectory = " + destinationDirectory);

    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucketName);

    List<Download> downloads = new ArrayList<>();
    long totalBytes = 0;

    if (mockS3Bucket != null) {
        for (MockS3Object mockS3Object : mockS3Bucket.getObjects().values()) {
            if (mockS3Object.getKey().startsWith(keyPrefix)) {
                String filePath = destinationDirectory.getAbsolutePath() + "/" + mockS3Object.getKey();
                File file = new File(filePath);
                file.getParentFile().mkdirs(); // Create any directory in the path that does not exist.
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    LOGGER.debug("downloadDirectory(): Writing file " + file);
                    fileOutputStream.write(mockS3Object.getData());
                    totalBytes += mockS3Object.getData().length;
                    downloads.add(new DownloadImpl(null, null, null, null, null,
                            new GetObjectRequest(bucketName, mockS3Object.getKey()), file,
                            mockS3Object.getObjectMetadata(), false));
                } catch (IOException e) {
                    throw new RuntimeException("Error writing to file " + file, e);
                }
            }
        }
    }

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(totalBytes);
    progress.updateProgress(totalBytes);

    MultipleFileDownloadImpl multipleFileDownload = new MultipleFileDownloadImpl(null, progress, null,
            keyPrefix, bucketName, downloads);
    multipleFileDownload.setState(TransferState.Completed);
    return multipleFileDownload;
}