Search in sources :

Example 1 with DownloadImpl

use of com.amazonaws.services.s3.transfer.internal.DownloadImpl in project herd by FINRAOS.

the class MockS3OperationsImpl method download.

@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);
    }
    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;
}
Also used : FileOutputStream(java.io.FileOutputStream) MultipleFileDownloadImpl(com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl) DownloadImpl(com.amazonaws.services.s3.transfer.internal.DownloadImpl) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) TransferProgress(com.amazonaws.services.s3.transfer.TransferProgress)

Example 2 with DownloadImpl

use of com.amazonaws.services.s3.transfer.internal.DownloadImpl in project herd by FINRAOS.

the class MockS3OperationsImpl method downloadDirectory.

/**
 * {@inheritDoc}
 * <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);
                // Create any directory in the path that does not exist.
                file.getParentFile().mkdirs();
                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;
}
Also used : ArrayList(java.util.ArrayList) MultipleFileDownloadImpl(com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl) DownloadImpl(com.amazonaws.services.s3.transfer.internal.DownloadImpl) MultipleFileDownloadImpl(com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl) IOException(java.io.IOException) TransferProgress(com.amazonaws.services.s3.transfer.TransferProgress) FileOutputStream(java.io.FileOutputStream) Download(com.amazonaws.services.s3.transfer.Download) MultipleFileDownload(com.amazonaws.services.s3.transfer.MultipleFileDownload) File(java.io.File) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Aggregations

GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 TransferProgress (com.amazonaws.services.s3.transfer.TransferProgress)2 DownloadImpl (com.amazonaws.services.s3.transfer.internal.DownloadImpl)2 MultipleFileDownloadImpl (com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Download (com.amazonaws.services.s3.transfer.Download)1 MultipleFileDownload (com.amazonaws.services.s3.transfer.MultipleFileDownload)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1