Search in sources :

Example 11 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class AWSS3StorageUploadInputStreamOperation method start.

@SuppressLint("SyntheticAccessor")
@Override
public void start() {
    // Only start if it hasn't already been started
    if (transferObserver != null) {
        return;
    }
    // Grab the inputStream to upload...
    InputStream inputStream = getRequest().getLocal();
    // Set up the metadata
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setUserMetadata(getRequest().getMetadata());
    objectMetadata.setContentType(getRequest().getContentType());
    ServerSideEncryption storageServerSideEncryption = getRequest().getServerSideEncryption();
    if (!ServerSideEncryption.NONE.equals(storageServerSideEncryption)) {
        objectMetadata.setSSEAlgorithm(storageServerSideEncryption.getName());
    }
    awsS3StoragePluginConfiguration.getAWSS3PluginPrefixResolver(cognitoAuthProvider).resolvePrefix(getRequest().getAccessLevel(), getRequest().getTargetIdentityId(), prefix -> {
        try {
            String serviceKey = prefix.concat(getRequest().getKey());
            transferObserver = storageService.uploadInputStream(serviceKey, inputStream, objectMetadata);
            transferObserver.setTransferListener(new UploadTransferListener());
        } catch (IOException ioException) {
            onError.accept(new StorageException("Issue uploading inputStream.", ioException, "See included exception for more details and suggestions to fix."));
        }
    }, onError);
}
Also used : InputStream(java.io.InputStream) ServerSideEncryption(com.amplifyframework.storage.s3.ServerSideEncryption) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) SuppressLint(android.annotation.SuppressLint)

Example 12 with StorageException

use of com.amplifyframework.storage.StorageException 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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageDownloadFileResult(com.amplifyframework.storage.result.StorageDownloadFileResult) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 13 with StorageException

use of com.amplifyframework.storage.StorageException 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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadFileResult(com.amplifyframework.storage.result.StorageUploadFileResult) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.junit.Test)

Example 14 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class StorageComponentTest method testListObject.

/**
 * Test that calling list method from Storage category correctly
 * invokes the registered AWSS3StoragePlugin instance and returns a
 * {@link StorageListResult} with list of stored items.
 *
 * @throws StorageException when an error is encountered while listing
 *                          files inside storage
 */
@Test
public void testListObject() throws StorageException {
    final String path = RandomString.string();
    final StorageItem item = new StorageItem(RandomString.string(), 0L, new Date(), RandomString.string(), null);
    when(storageService.listFiles(anyString(), anyString())).thenReturn(Collections.singletonList(item));
    StorageListResult result = Await.<StorageListResult, StorageException>result((onResult, onError) -> storage.list(path, onResult, onError));
    assertEquals(item, result.getItems().get(0));
}
Also used : StorageListResult(com.amplifyframework.storage.result.StorageListResult) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) Date(java.util.Date) StorageItem(com.amplifyframework.storage.StorageItem) Test(org.junit.Test)

Example 15 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class StorageComponentTest method testUploadInputStreamGetsKey.

/**
 * Test that calling upload inputStream 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 testUploadInputStreamGetsKey() throws Exception {
    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.onStateChanged(0, TransferState.COMPLETED);
        return null;
    }).when(observer).setTransferListener(any(TransferListener.class));
    StorageUploadInputStreamResult result = Await.<StorageUploadInputStreamResult, StorageException>result((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
    assertEquals(toRemoteKey, result.getKey());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadInputStreamResult(com.amplifyframework.storage.result.StorageUploadInputStreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Aggregations

StorageException (com.amplifyframework.storage.StorageException)19 Test (org.junit.Test)15 RandomString (com.amplifyframework.testutils.random.RandomString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)6 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)6 File (java.io.File)5 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)4 SuppressLint (android.annotation.SuppressLint)3 InputStream (java.io.InputStream)3 StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)2 StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)2 StorageUploadInputStreamResult (com.amplifyframework.storage.result.StorageUploadInputStreamResult)2 ServerSideEncryption (com.amplifyframework.storage.s3.ServerSideEncryption)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 Region (com.amazonaws.regions.Region)1 StorageItem (com.amplifyframework.storage.StorageItem)1 StorageDownloadFileOptions (com.amplifyframework.storage.options.StorageDownloadFileOptions)1