use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.
the class AWSS3StorageService method uploadInputStream.
/**
* Begin uploading an inputStream.
* @param serviceKey S3 service key
* @param inputStream Target InputStream
* @param metadata Object metadata to associate with upload
* @return A transfer observer
* @throws IOException An IOException thrown during the process writing an InputStream into a file
*/
@NonNull
public TransferObserver uploadInputStream(@NonNull String serviceKey, @NonNull InputStream inputStream, @NonNull ObjectMetadata metadata) throws IOException {
startServiceIfNotAlreadyStarted();
UploadOptions uploadOptions = UploadOptions.builder().bucket(bucket).objectMetadata(metadata).build();
return transferUtility.upload(serviceKey, inputStream, uploadOptions);
}
use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadFileGetsKey.
/**
* Test that calling upload file method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageUploadFileResult} with correct remote key.
*
* @throws Exception when an error is encountered while uploading
*/
@Test
public void testUploadFileGetsKey() throws Exception {
final String toRemoteKey = RandomString.string();
final File fromLocalFile = new RandomTempFile(FILE_SIZE);
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadFile(anyString(), any(File.class), any(ObjectMetadata.class))).thenReturn(observer);
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageUploadFileResult result = Await.<StorageUploadFileResult, StorageException>result((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(toRemoteKey, result.getKey());
}
use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.
the class StorageComponentTest method testInputStreamError.
/**
* Test that calling upload inputStream method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the upload file cannot be created
*/
@Test
public void testInputStreamError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
final String toRemoteKey = RandomString.string();
final InputStream inputStream = new ByteArrayInputStream(RandomBytes.bytes());
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadInputStream(anyString(), any(InputStream.class), any(ObjectMetadata.class))).thenReturn(observer);
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onError(0, testError);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageException error = Await.<StorageUploadInputStreamResult, StorageException>error((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
assertEquals(testError, error.getCause());
}
use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.
the class StorageComponentTest method testDownloadToFileGetsFile.
/**
* Test that calling download file method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageDownloadFileResult} with correct file path.
*
* @throws Exception when an error is encountered while downloading
*/
@Test
public void testDownloadToFileGetsFile() throws Exception {
final String fromRemoteKey = RandomString.string();
final File toLocalFile = new RandomTempFile();
// Since we use a mock StorageService, it will return a null
// result by default. We need a non-null transfer observer.
// One option is to mock that, too.
TransferObserver observer = mock(TransferObserver.class);
when(storageService.downloadToFile(anyString(), any(File.class))).thenReturn(observer);
// Since we use a mock TransferObserver, it has no internal logic
// to know to call back the listener! So, we simulate the success
// callback, as part of our "happy path" test.
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageDownloadFileResult result = Await.<StorageDownloadFileResult, StorageException>result((onResult, onError) -> storage.downloadFile(fromRemoteKey, toLocalFile, onResult, onError));
assertEquals(toLocalFile.getAbsolutePath(), result.getFile().toString());
}
use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadFileError.
/**
* Test that calling upload file method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the upload file cannot be created
*/
@Test
public void testUploadFileError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
final String toRemoteKey = RandomString.string();
final File fromLocalFile = new RandomTempFile(FILE_SIZE);
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadFile(anyString(), any(File.class), any(ObjectMetadata.class))).thenReturn(observer);
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onError(0, testError);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageException error = Await.<StorageUploadFileResult, StorageException>error((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(testError, error.getCause());
}
Aggregations