Search in sources :

Example 1 with Download

use of com.amazonaws.services.s3.transfer.Download in project deeplearning4j by deeplearning4j.

the class S3Downloader method download.

public void download(String bucket, String key, OutputStream to) throws IOException {
    AmazonS3 s3 = getClient();
    S3Object obj = s3.getObject(bucket, key);
    InputStream is = obj.getObjectContent();
    BufferedOutputStream bos = new BufferedOutputStream(to);
    IOUtils.copy(is, bos);
    bos.close();
    is.close();
    obj.close();
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Example 2 with Download

use of com.amazonaws.services.s3.transfer.Download in project deeplearning4j by deeplearning4j.

the class S3Downloader method download.

public void download(String bucket, String key, File to) throws IOException {
    AmazonS3 s3 = getClient();
    S3Object obj = s3.getObject(bucket, key);
    InputStream is = obj.getObjectContent();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(to));
    IOUtils.copy(is, bos);
    bos.close();
    is.close();
    obj.close();
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Example 3 with Download

use of com.amazonaws.services.s3.transfer.Download in project YCSB by brianfrankcooper.

the class S3Client method readFromStorage.

/**
  * Download an object from S3.
  *
  * @param bucket
  *            The name of the bucket
  * @param key
  *            The file key of the object to upload/update.
  * @param result
  *            The Hash map where data from the object are written
  *
  */
protected Status readFromStorage(String bucket, String key, HashMap<String, ByteIterator> result, SSECustomerKey ssecLocal) {
    try {
        Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
        //consuming the stream
        InputStream objectData = objectAndMetadata.getKey().getObjectContent();
        // writing the stream to bytes and to results
        int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
        byte[] inputStreamToByte = new byte[sizeOfFile];
        objectData.read(inputStreamToByte, 0, sizeOfFile);
        result.put(key, new ByteArrayByteIterator(inputStreamToByte));
        objectData.close();
        objectAndMetadata.getKey().close();
    } catch (Exception e) {
        System.err.println("Not possible to get the object " + key);
        e.printStackTrace();
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) HashMap(java.util.HashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DBException(com.yahoo.ycsb.DBException)

Example 4 with Download

use of com.amazonaws.services.s3.transfer.Download in project incubator-gobblin by apache.

the class AWSSdkClient method downloadS3Object.

/**
 * Download a S3 object to local directory
 *
 * @param s3ObjectSummary S3 object summary for the object to download
 * @param targetDirectory Local target directory to download the object to
 * @throws IOException If any errors were encountered in downloading the object
 */
public void downloadS3Object(S3ObjectSummary s3ObjectSummary, String targetDirectory) throws IOException {
    final AmazonS3 amazonS3 = getS3Client();
    final GetObjectRequest getObjectRequest = new GetObjectRequest(s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());
    final S3Object s3Object = amazonS3.getObject(getObjectRequest);
    final String targetFile = StringUtils.removeEnd(targetDirectory, File.separator) + File.separator + s3Object.getKey();
    FileUtils.copyInputStreamToFile(s3Object.getObjectContent(), new File(targetFile));
    LOGGER.info("S3 object downloaded to file: " + targetFile);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) File(java.io.File)

Example 5 with Download

use of com.amazonaws.services.s3.transfer.Download 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)

Aggregations

File (java.io.File)12 IOException (java.io.IOException)10 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)9 S3Object (com.amazonaws.services.s3.model.S3Object)9 AmazonServiceException (com.amazonaws.AmazonServiceException)8 AmazonS3 (com.amazonaws.services.s3.AmazonS3)8 AmazonClientException (com.amazonaws.AmazonClientException)6 InputStream (java.io.InputStream)5 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)4 FileOutputStream (java.io.FileOutputStream)4 SdkClientException (com.amazonaws.SdkClientException)3 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)3 Regions (com.amazonaws.regions.Regions)3 Download (com.amazonaws.services.s3.transfer.Download)3 MultipleFileDownload (com.amazonaws.services.s3.transfer.MultipleFileDownload)3 TransferProgress (com.amazonaws.services.s3.transfer.TransferProgress)3 Date (java.util.Date)3 ProgressEvent (com.amazonaws.event.ProgressEvent)2 ProgressListener (com.amazonaws.event.ProgressListener)2