Search in sources :

Example 1 with TransferProgress

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

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

the class S3DaoTest method testPerformTransferAssertLogWhenStepCountGt300.

@Test
public void testPerformTransferAssertLogWhenStepCountGt300() throws Exception {
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    // Shorten the sleep interval for faster tests
    long originalSleepIntervalsMillis = (long) ReflectionTestUtils.getField(s3Dao, "sleepIntervalsMillis");
    ReflectionTestUtils.setField(s3Dao, "sleepIntervalsMillis", 1l);
    try {
        S3FileCopyRequestParamsDto s3FileCopyRequestParamsDto = new S3FileCopyRequestParamsDto();
        s3FileCopyRequestParamsDto.setSourceBucketName("sourceBucketName");
        s3FileCopyRequestParamsDto.setSourceObjectKey("sourceObjectKey");
        s3FileCopyRequestParamsDto.setTargetBucketName("targetBucketName");
        s3FileCopyRequestParamsDto.setTargetObjectKey("targetObjectKey");
        s3FileCopyRequestParamsDto.setKmsKeyId("kmsKeyId");
        when(mockS3Operations.copyFile(any(), any())).then(new Answer<Copy>() {

            @Override
            public Copy answer(InvocationOnMock invocation) throws Throwable {
                Copy mockTransfer = mock(Copy.class);
                when(mockTransfer.getProgress()).thenReturn(new TransferProgress());
                when(mockTransfer.getState()).thenReturn(TransferState.Completed);
                when(mockTransfer.isDone()).then(new Answer<Boolean>() {

                    int callCount = 0;

                    @Override
                    public Boolean answer(InvocationOnMock invocation) throws Throwable {
                        return callCount++ > 600;
                    }
                });
                return mockTransfer;
            }
        });
        s3Dao.copyFile(s3FileCopyRequestParamsDto);
    } finally {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        ReflectionTestUtils.setField(s3Dao, "sleepIntervalsMillis", originalSleepIntervalsMillis);
    }
}
Also used : Answer(org.mockito.stubbing.Answer) S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) Copy(com.amazonaws.services.s3.transfer.Copy) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransferProgress(com.amazonaws.services.s3.transfer.TransferProgress) Test(org.junit.Test)

Example 3 with TransferProgress

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

the class S3DaoTest method testPerformTransferAssertHandleFailedWithAmazonClientException.

@Test
public void testPerformTransferAssertHandleFailedWithAmazonClientException() throws Exception {
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    // Shorten the sleep interval for faster tests
    long originalSleepIntervalsMillis = (long) ReflectionTestUtils.getField(s3Dao, "sleepIntervalsMillis");
    ReflectionTestUtils.setField(s3Dao, "sleepIntervalsMillis", 1l);
    try {
        S3FileCopyRequestParamsDto s3FileCopyRequestParamsDto = new S3FileCopyRequestParamsDto();
        s3FileCopyRequestParamsDto.setSourceBucketName("sourceBucketName");
        s3FileCopyRequestParamsDto.setSourceObjectKey("sourceObjectKey");
        s3FileCopyRequestParamsDto.setTargetBucketName("targetBucketName");
        s3FileCopyRequestParamsDto.setTargetObjectKey("targetObjectKey");
        s3FileCopyRequestParamsDto.setKmsKeyId("kmsKeyId");
        when(mockS3Operations.copyFile(any(), any())).then(new Answer<Copy>() {

            @Override
            public Copy answer(InvocationOnMock invocation) throws Throwable {
                Copy mockTransfer = mock(Copy.class);
                when(mockTransfer.getProgress()).thenReturn(new TransferProgress());
                when(mockTransfer.getState()).thenReturn(TransferState.Failed);
                when(mockTransfer.isDone()).thenReturn(true);
                when(mockTransfer.waitForException()).thenReturn(new AmazonClientException("message"));
                return mockTransfer;
            }
        });
        try {
            s3Dao.copyFile(s3FileCopyRequestParamsDto);
            fail();
        } catch (Exception e) {
            assertEquals(AmazonClientException.class, e.getClass());
            assertEquals("message", e.getMessage());
        }
    } finally {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        ReflectionTestUtils.setField(s3Dao, "sleepIntervalsMillis", originalSleepIntervalsMillis);
    }
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) Copy(com.amazonaws.services.s3.transfer.Copy) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AmazonClientException(com.amazonaws.AmazonClientException) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) TransferProgress(com.amazonaws.services.s3.transfer.TransferProgress) Test(org.junit.Test)

Example 4 with TransferProgress

use of com.amazonaws.services.s3.transfer.TransferProgress in project aws-doc-sdk-examples by awsdocs.

the class XferMgrProgress method showMultiUploadProgress.

// Prints progress of a multiple file upload while waiting for it to finish.
public static void showMultiUploadProgress(MultipleFileUpload multi_upload) {
    // print the upload's human-readable description
    System.out.println(multi_upload.getDescription());
    // snippet-start:[s3.java1.s3_xfer_mgr_progress.substranferes]
    Collection<? extends Upload> sub_xfers = new ArrayList<Upload>();
    sub_xfers = multi_upload.getSubTransfers();
    do {
        System.out.println("\nSubtransfer progress:\n");
        for (Upload u : sub_xfers) {
            System.out.println("  " + u.getDescription());
            if (u.isDone()) {
                TransferState xfer_state = u.getState();
                System.out.println("  " + xfer_state);
            } else {
                TransferProgress progress = u.getProgress();
                double pct = progress.getPercentTransferred();
                printProgressBar(pct);
                System.out.println();
            }
        }
        // wait a bit before the next update.
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            return;
        }
    } while (multi_upload.isDone() == false);
    // print the final state of the transfer.
    TransferState xfer_state = multi_upload.getState();
    System.out.println("\nMultipleFileUpload " + xfer_state);
// snippet-end:[s3.java1.s3_xfer_mgr_progress.substranferes]
}
Also used : ArrayList(java.util.ArrayList) TransferState(com.amazonaws.services.s3.transfer.Transfer.TransferState)

Example 5 with TransferProgress

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

the class MockS3OperationsImpl method upload.

@Override
public Upload upload(PutObjectRequest putObjectRequest, TransferManager transferManager) {
    LOGGER.debug("upload(): putObjectRequest.getBucketName() = " + putObjectRequest.getBucketName() + ", putObjectRequest.getKey() = " + putObjectRequest.getKey());
    putObject(putObjectRequest, transferManager.getAmazonS3Client());
    long contentLength = putObjectRequest.getFile().length();
    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(contentLength);
    progress.updateProgress(contentLength);
    UploadImpl upload = new UploadImpl(null, progress, null, null);
    upload.setState(TransferState.Completed);
    return upload;
}
Also used : MultipleFileUploadImpl(com.amazonaws.services.s3.transfer.internal.MultipleFileUploadImpl) UploadImpl(com.amazonaws.services.s3.transfer.internal.UploadImpl) TransferProgress(com.amazonaws.services.s3.transfer.TransferProgress)

Aggregations

TransferProgress (com.amazonaws.services.s3.transfer.TransferProgress)9 IOException (java.io.IOException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 AmazonServiceException (com.amazonaws.AmazonServiceException)3 Copy (com.amazonaws.services.s3.transfer.Copy)3 TransferState (com.amazonaws.services.s3.transfer.Transfer.TransferState)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)2 MultipartUpload (com.amazonaws.services.s3.model.MultipartUpload)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 MultipleFileDownload (com.amazonaws.services.s3.transfer.MultipleFileDownload)2 MultipleFileUpload (com.amazonaws.services.s3.transfer.MultipleFileUpload)2 Upload (com.amazonaws.services.s3.transfer.Upload)2 DownloadImpl (com.amazonaws.services.s3.transfer.internal.DownloadImpl)2 MultipleFileDownloadImpl (com.amazonaws.services.s3.transfer.internal.MultipleFileDownloadImpl)2 MultipleFileUploadImpl (com.amazonaws.services.s3.transfer.internal.MultipleFileUploadImpl)2